summaryrefslogtreecommitdiffstats
path: root/day15/day15-2.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-12-15 20:20:21 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-12-15 20:20:21 +0100
commit6eb86da5efc99e63c58f1fc9a1f7e3706dd6a2dc (patch)
treede4e045a5e792369058426d8058a478017e65f8d /day15/day15-2.js
parent2f7c1f10323a26dea066eecb52b1c3fae746c0f5 (diff)
downloadadvent_of_code_2020-6eb86da5efc99e63c58f1fc9a1f7e3706dd6a2dc.tar.gz
advent_of_code_2020-6eb86da5efc99e63c58f1fc9a1f7e3706dd6a2dc.tar.bz2
advent_of_code_2020-6eb86da5efc99e63c58f1fc9a1f7e3706dd6a2dc.zip
day15
Diffstat (limited to 'day15/day15-2.js')
-rw-r--r--day15/day15-2.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/day15/day15-2.js b/day15/day15-2.js
new file mode 100644
index 0000000..11b137c
--- /dev/null
+++ b/day15/day15-2.js
@@ -0,0 +1,30 @@
+const data = [0,13,1,16,6,17]
+
+const numbers = {}
+let last, prev1, prev2
+
+const game = (turns) => {
+ for (let i=1; i <= turns; i++) {
+ if (i <= data.length) {
+ numbers[data[i-1]] = [i, 0]
+ last = data[i-1]
+ } else {
+ [prev1, prev2] = numbers[last]
+ if (!prev2) {
+ last = 0
+ numbers[0] = [i, numbers[0][0]]
+ } else {
+ last = prev1 - prev2
+ if (numbers[last]) {
+ numbers[last] = [i, numbers[last][0]]
+ } else {
+ numbers[last] = [i, 0]
+ }
+ }
+ }
+ }
+}
+
+game(30000000)
+console.log(last)
+