var maps = {}; for(var map of require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>(a))){ map = map.split(" => "); maps[map[0]] = map[1]; } for(var i = 0; i < 1; i++){ for(var input of Object.keys(maps)){ var lines = input.split("/"); rotated = input.split("/").map((line,y)=>(line.split("").map((a, x)=>(lines[lines.length-1-x][y])).join(""))).join("/"); maps[rotated] = maps[input]; } } for(var input of Object.keys(maps)){ maps[input.split("/").reverse().join("/")] = maps[input]; } for(var input of Object.keys(maps)){ maps[input.split("/").map(line=>(line.split("").reverse().join(""))).join("/")] = maps[input]; } function runRound(textInput){ var oldGrid = textInput.split("/").map((a)=>(a.split(""))); var sideLen = oldGrid[0].length; if(sideLen%2 == 0){ var sideSquares = sideLen/2; var squareLen = 2; var newSquareLen = 3; } else if(sideLen%3 == 0){ var sideSquares = sideLen/3; var squareLen = 3; var newSquareLen = 4; } var newGrid = []; for(var i = 0; i < (sideSquares * newSquareLen); i++){ newGrid[i] = []; } for(var x = 0; x < sideSquares; x++){ for(var y = 0; y < sideSquares; y++){ var section = []; for(var i = 0; i < squareLen; i++){ section.push(oldGrid[(y*squareLen) + i].slice(x*squareLen, (x+1)*squareLen)); } var sectionText = section.map((a)=>(a.join(""))).join("/"); var newSectionText = maps[sectionText]; var newSection = newSectionText.split("/").map(a=>(a.split(""))); for(var x1 = 0; x1 < newSquareLen; x1++){ for(var y1= 0; y1 < newSquareLen; y1++){ newGrid[(y*newSquareLen) + y1][(x*newSquareLen) + x1] = newSection[y1][x1]; } } } } return newGrid.map(a=>(a.join(""))).join("/") } var currentGrid = ".#./..#/###"; var iters = 5; for(var i = 0; i < iters; i++){ currentGrid = runRound(currentGrid); } console.log(currentGrid.split("").filter(a=>(a=="#")).length);