Skip to content

Instantly share code, notes, and snippets.

@towc
Created December 18, 2018 10:29
Show Gist options
  • Save towc/6147e4ac5a34c1cbad98ac071f58da5f to your computer and use it in GitHub Desktop.
Save towc/6147e4ac5a34c1cbad98ac071f58da5f to your computer and use it in GitHub Desktop.
const fs = require('fs');
const [DEATH, LIFE, VOID] = [1, 2, 3];
const board = fs.readFileSync('aoc-input-18', 'utf-8')
.trim()
.split('\n')
.map(line => line.split('')
.map(c =>
c === '|' ? LIFE
: c === '#' ? DEATH
: VOID));
const show = () => {
console.log(board.map(l => l.map(c =>
c === LIFE ? '|'
: c === DEATH ? '#'
: '.').join('')).join('\n'))
}
const cell = (b, x, y) => b[y] && b[y][x];
const count = (arr, type) => arr.reduce((acc, v) => acc + (v === type), 0);
const neighbours = (b, x, y) => [
cell(b, x-1, y-1), cell(b, x, y-1), cell(b, x+1, y-1),
cell(b, x-1, y), cell(b, x+1, y),
cell(b, x-1, y+1), cell(b, x, y+1), cell(b, x+1, y+1),
].filter(Boolean);
const checksum = (b) => board.reduce((acc, row, y) => acc +
row.reduce((bcc, v, x) => bcc + (x+1) * (y+1) * v % 3123471, 0), 0);
console.log('init state');
show();
const values = [];
const sums = [];
let gen = 0;
while(++gen <= 1e9) {
const prev = board.map(row => [...row]);
for(let y = 0; y < board.length; ++y) {
const row = board[y];
for(let x = 0; x < row.length; ++x) {
const neighs = neighbours(prev, x, y);
const v = cell(prev, x, y);
let n;
switch (v) {
case VOID:
n = count(neighs, LIFE) >= 3 ? LIFE : VOID;
break;
case LIFE:
n = count(neighs, DEATH) >= 3 ? DEATH : LIFE;
break;
case DEATH:
n = (count(neighs, LIFE) >= 1 && count(neighs, DEATH) >= 1) ? DEATH : VOID;
break;
}
board[y][x] = n;
}
}
console.log(gen);
//show();
const allCells = [].concat(...board);
const lifecount = count(allCells, LIFE);
const deathcount = count(allCells, DEATH);
const value = lifecount * deathcount;
//console.log('alive: ' + lifecount);
//console.log('dead: ' + deathcount);
console.log('value: ' + value);
const sum = checksum(board);
const index = sums.indexOf(sum);
if (index > -1) {
console.log('found duplicate at ' + index);
console.log('duplicate is ' + value);
console.log('duplicate sum is ' + sum);
break;
}
sums.push(sum);
values.push(value);
// found dupe from 496 (index) in 525 (cycle)
// lookup value at 497 + (1e9-497)%(525-497)
show();
}
||.|..#|.|.....|..|........|..#..|#...|.........#.
...#....|#..|......#.|||...#...|...#....|.......#.
##.#|.#..|.#|....||.|#.#....#.#.###.|..|...|#.#|..
..||.#|#|#..#.#|...|....||.|...##.....|.....#...#|
#|..#...#.##|.|.##||..|...|...#||.|...#.##.###|.|.
#.#||#...#.|#.....|#.......|#.||..|..|#..#.##...#.
|.||.|.|........|.|.#.|...##.|.||..#....#..|.|#..#
....#...|#...#....|#.|.|||#.||#....||.......|.#|..
|#..#..#.|.|#.||#...|.|...|#.||...#...##....|....|
..#...|......#.#...|#...#|.|......#.|||#.|#.|#.#.|
..##.#|#|...||....##|.|||.|##|#.....#|...#...###||
...||......#...|....#.....#|#......#......#..|.|#.
..........#|.|#..|..#.##.#...#|...#..#...|...#|.||
|.#..#|###..||...|#|....|...........|...#|#.#..#|.
|.#..##.........#||.....|#|.###.#.|.....#.|..|.##.
....#||..#|...|.#...#.|.#.|..#............#.|.....
...|#...##.#.#.|.||.......#...|.....###.||...##..|
..|...|#.|.#..|..#|..||.#.....#||.|..#..|..|.||.#.
#.##||#.#.....||.|.#.......#.|.##|##|#..#|.....|#.
...#..#.#..#.|.....|.#...|.....###||||..#..##...#.
...|...|.|.##|..#.#|#....#.#.|#||||....#...|||.#.#
..|...##..|#||..||#|#..|..|##.|..##...#...#|.##.#|
|#...#.#.......|...||...##.|.||#.....#.||.......#|
..#.....#|.#...#...|.#|.#.#|.....#.##.#|.....#....
......#|..|||.#.#..||..|.|..|...##.....#..#.###...
....##..#..|..||..##.||#.|..|.#...#..||#...#..|...
.||......||#.#|#.##.||.#..|..#..#..#.|..|#|..||...
|...#|...|....|.|.|.....||.|.|..|#.#.#..|.#..##..#
..|..#..#.|.##.....|#..|..#|.|..#.#.|..|..#|..|...
....|...#..|#...#.###.|..####..||#..|.|.|......||.
.....||.#.|##|.....|##.||.##..#.#.|...#.#...||.#..
.|#..#.|...||#..|.#..#....###.#...#.....||......##
||...#||..#...##.|..|...|#.|#|.|##|#|##....#..#|..
.|..#.##..|......#|.....||..|||||||#.#.|#..|.|.|..
|#.##.|............##||##.#...#.#........|..#.....
.||....|.#||#|.#..|..........#....##.#.|...#.||..|
......|...#.......##.#.#|#......#||##...#..#||.|#.
..||.#..#.#.......|.|.##.#.|...#...###..|...|..|..
.#....|..||##.|.|..#.|.#|..#|......#....#.#.|..||.
.........|....|.#..|...|.|#....#..|.....#...|#|#.|
.|.||..|#.|...##.|.#...#..|.||||..##.##|#.##.|##.#
.|#.#.....#||#.#....||.|..##......|.#.|..#...##||.
|.#........|..|........#..#.#....|.|#.......#.|...
#..##.|.#......#...#..#.|.....#|.|.|#.##..#|...|..
..#....####....|..||...||..||.|.|...##.#..|||.||.|
...|..#.|||.||.||.#|...|..#.##..|#..........#.##|.
..|..||.#|.|.#..|.###|#.|....|.||.....|.........#.
#.|#.....#.|.#|#.|.#.#||...|.....||..|..|.##|#.|#|
..|...||#.|.#...|#.#....#.|##.#.|.....##..#..|.#..
..|.#...|.|...##|..#.#|.#|..|..#.|#.#...#|#.#||#..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment