summaryrefslogtreecommitdiffstats
path: root/day17/day17-1.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-12-17 20:29:42 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-12-17 20:30:54 +0100
commit8fcddb1917ffcfbeb9b091536352e609f8e709f3 (patch)
tree7c595cbf9a5042598a4d3eee1d059436cc45bb37 /day17/day17-1.js
parent6ae98fc6a09025e610b7bd3e64fc9c26ea304e50 (diff)
downloadadvent_of_code_2020-8fcddb1917ffcfbeb9b091536352e609f8e709f3.tar.gz
advent_of_code_2020-8fcddb1917ffcfbeb9b091536352e609f8e709f3.tar.bz2
advent_of_code_2020-8fcddb1917ffcfbeb9b091536352e609f8e709f3.zip
day17
Diffstat (limited to 'day17/day17-1.js')
-rw-r--r--day17/day17-1.js54
1 files changed, 54 insertions, 0 deletions
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)
+