Skip to content

Instantly share code, notes, and snippets.

@gnarf
Created December 20, 2021 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnarf/d510db51de25bc751d1f7cdb1894f065 to your computer and use it in GitHub Desktop.
Save gnarf/d510db51de25bc751d1f7cdb1894f065 to your computer and use it in GitHub Desktop.
#AdventOfCode - Day 20 Part 2 (pre-golfing, but still ugly...)
// comment out top line if you are running it first time... don't blast that fetch!
// I=(await(await fetch("https://adventofcode.com/2021/day/20/input")).text());
// I=`..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
// #..#.
// #....
// ##..#
// ..#..
// ..###`;
[key,...rest]=I.split(/\n+/)
l=(x,y,z)=>((rest[y]&&rest[y][x]||key[(count%2)&&511])=='#')<<z
for(count=50;count--;console.log(rest=output))for(output=[],y=0;line=rest[y]||[],y<rest.length+4;y++)for(x=0;x<rest[0].length+4;x++){
out=l(x-2,y-2,8)+l(x-1,y-2,7)+l(x,y-2,6)
+l(x-2,y-1,5)+l(x-1,y-1,4)+l(x,y-1,3)
+l(x-2,y,2)+l(x-1,y,1)+l(x,y,0);
(output[y]??=[])[x]=key[out];
}
output.reduce((m,line,y)=>m+line.reduce((m,c,x)=>m+l(x,y,0),0),0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment