Skip to content

Instantly share code, notes, and snippets.

@constxd

constxd/d24.js Secret

Created December 24, 2020 15:09
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 constxd/0d1e596a5360d5568cee8d8753a11b73 to your computer and use it in GitHub Desktop.
Save constxd/0d1e596a5360d5568cee8d8753a11b73 to your computer and use it in GitHub Desktop.
AoC day 24
let dir = {
'ne': [ 1, 1 ],
'nw': [ -1, 1 ],
'se': [ 1, -1 ],
'sw': [ -1, -1 ],
'e': [ 2, 0 ],
'w': [ -2, 0 ],
' ': [ 0, 0 ]
};
function coords(t) {
let steps = t.matches(/ne|nw|se|sw|e|w/);
return [dir[s] for s in steps].fold((a, b) -> a.zip(b, < + >));
}
function nbh(t) {
return { t.zip(d, < + >) for _, d in dir };
}
let black = { g[0] for g in slurp().lines().map(coords).sort().group() | g.len() % 2 };
/* part 1 */
print(black.len());
for _ in ..100 {
let check, new = {}, {};
for t in black { check += nbh(t); }
for t in check {
let nc = nbh(t).keys().countBy(|black ? #|);
if (nc == 2 || ((black ? t) && nc == 3))
new << t;
}
black = new;
}
/* part 2 */
print(black.len());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment