nextline = (line)=>(line.split("").map((char, i, arr)=>([arr[i-1], arr[i], arr[i+1]]).map((char)=>(char ? char : ".")).join("")).map((str)=>(["^^.", ".^^", "..^", "^.."].indexOf(str) != -1)).map((trap)=>(trap ? "^" : ".")).join("")); var lines = [".^^^^^.^^^..^^^^^...^.^..^^^.^^....^.^...^^^...^^^^..^...^...^^.^.^.......^..^^...^.^.^^..^^^^^...^."]; while(lines.length < 400000){ lines.push(nextline(lines[lines.length - 1])); } console.log(lines.map((line)=>(line.replace(/\^/g, ""))).join("").length);