diff options
Diffstat (limited to 'day17')
-rw-r--r-- | day17/data.js | 12 | ||||
-rw-r--r-- | day17/day17-1.js | 54 | ||||
-rw-r--r-- | day17/day17-2.js | 58 |
3 files changed, 124 insertions, 0 deletions
diff --git a/day17/data.js b/day17/data.js new file mode 100644 index 0000000..648380b --- /dev/null +++ b/day17/data.js @@ -0,0 +1,12 @@ +const data = ` +#####... +.#..##.. +##.##.## +...####. +#.#...## +.##...#. +.#.#.### +#.#.#..# +` + +module.exports = data diff --git a/day17/day17-1.js b/day17/day17-1.js new file mode 100644 index 0000000..42e6b6f --- /dev/null +++ b/day17/day17-1.js @@ -0,0 +1,54 @@ +const data = require('./data') +const startArray = [data.split(/\n/g).filter(e => e).map(e => e.split(''))] +const cycle = (arr) => { + const l = arr[0].length + const empty = Array(l+2).fill(Array(l+2).fill('.')) + const empty2 = Array(l+2).fill('.') + const addDots = ars => ars.map(ar => ([empty2, ...(ar.map(a => ['.', ...a, '.'])), empty2])) + const resized = [empty, ...addDots(arr), empty] + const around = (iz, iy, ix) => { + const f = (rz, ry, rx) => { + if ( + !resized[iz+rz] + || !resized[iz+rz][iy+ry] + || !resized[iz+rz][iy+ry][ix+rx] + ) { return 0 } + return resized[iz+rz][iy+ry][ix+rx] === '#' ? 1 : 0 + } + + let c = 0 + for (let z=-1; z<=1; z++) { + for (let y=-1; y<=1; y++) { + for (let x=-1; x<=1; x++) { + if(!(x == 0 && y == 0 && z == 0)) { c += f(z, y, x) } + } + } + } + return c + } + + return resized.map((z, iz) => z.map((y,iy) => y.map((x,ix) => { + const active = x === '#' + const activeAround = around(iz,iy,ix) + + if (active) { + return (activeAround === 2 || activeAround === 3) ? '#' : '.' + } else { + return (activeAround === 3) ? '#' : '.' + } + }))) +} + +let cycleResult = startArray, round = 1 +while (round <= 6) { + cycleResult = cycle(cycleResult) + round++ +} + +let result = 0 +cycleResult.forEach(z => z.forEach(y => y.forEach(x => { + if (x === '#') {result++} +}))) + +console.log('Answer: ', result) + diff --git a/day17/day17-2.js b/day17/day17-2.js new file mode 100644 index 0000000..65dea2d --- /dev/null +++ b/day17/day17-2.js @@ -0,0 +1,58 @@ +const data = require('./data') +const startArray = [[data.split(/\n/g).filter(e => e).map(e => e.split(''))]] + +const cycle = (array) => { + const l = array[0][0].length + const empty = Array(l+2).fill(Array(l+2).fill('.')) + const empty2 = Array(l+2).fill('.') + const empty3 = Array(array.length + 2).fill(empty) + const addDots = ars => ars.map(ar => ([empty2, ...(ar.map(a => ['.', ...a, '.'])), empty2])) + const resized = [ empty3, ...array.map(arr => [empty, ...addDots(arr), empty]), empty3] + const around = (iw, iz, iy, ix) => { + const f = (rw, rz, ry, rx) => { + if ( + !resized[iw+rw] + || !resized[iw+rw][iz+rz] + || !resized[iw+rw][iz+rz][iy+ry] + || !resized[iw+rw][iz+rz][iy+ry][ix+rx] + ) { return 0 } + return resized[iw+rw][iz+rz][iy+ry][ix+rx] === '#' ? 1 : 0 + } + + let c = 0 + for (let w=-1; w<=1; w++) { + for (let z=-1; z<=1; z++) { + for (let y=-1; y<=1; y++) { + for (let x=-1; x<=1; x++) { + if (!(x===0 && y===0 && z===0 && w===0)) { c += f(w,z,y,x) } + } + } + } + } + return c + } + + return resized.map((w, iw) => w.map((z, iz) => z.map((y,iy) => y.map((x,ix) => { + const active = x === '#' + const activeAround = around(iw,iz,iy,ix) + if (active) { + return (activeAround === 2 || activeAround === 3) ? '#' : '.' + } else { + return (activeAround === 3) ? '#' : '.' + } + })))) +} + +let cycleResult = startArray, round = 1 +while (round <= 6) { + cycleResult = cycle(cycleResult) + round++ +} + +let result = 0 +cycleResult.forEach(w => w.forEach(z => z.forEach(y => y.forEach(x => { + if (x === '#') {result++} +})))) + +console.log('Answer: ', result) + |