var blankgrid = []; for(var x = 0; x < 50; x++){ blankgrid[x] = []; for(var y = 0; y < 6; y++){ blankgrid[x][y] = 0; } } rect=(A, B, grid)=>(grid.map((col, x)=>(col.map((pixel, y)=>( ((x < A) && (y < B)) ? (1) : (pixel) ))))); rotateRow=(row, moves, grid)=>(grid.map((col, x)=>(col.map((pixel, y)=>( (y == row) ? (grid[(x-moves+50)%50][y]) : (pixel) ))))) rotateColumn=(colnum, moves, grid)=>(grid.map((col, x)=>(col.map((pixel, y)=>( (x == colnum) ? (grid[x][(y-moves+6)%6]) : (pixel) ))))) var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, ""); var newgrid = input.split("\n").filter((a)=>(a)).reduce((grid, str)=>(eval(str.replace(/rect ([0-9]+)x([0-9]+)/, "rect($1,$2,grid)").replace(/rotate row y=([0-9]+) by ([0-9]+)/, "rotateRow($1,$2,grid)").replace(/rotate column x=([0-9]+) by ([0-9]+)/, "rotateColumn($1,$2,grid)"))), blankgrid); console.log(newgrid.reduce((a, b)=>(a + b.reduce((c, d)=>(d + c), 0)), 0))