Skip to content

Commit 58003b2

Browse files
committed
Solution for day 15 (2020)
1 parent e889259 commit 58003b2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

2020/15/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const numbers = '14,3,1,0,9,5'.split(',').map(Number)
2+
3+
console.log('Part 1', run(numbers, 2020))
4+
console.log('Calculating... (Part 2 takes 5-10 seconds to run)')
5+
console.log('Part 2', run(numbers, 30000000))
6+
7+
function run(numbers, total) {
8+
const occurances = new Map()
9+
let next = 0
10+
for (let i = 0; i < total; i++) {
11+
if (numbers[i] === undefined) numbers[i] = next
12+
next = occurances.get(numbers[i]) !== undefined ? i - occurances.get(numbers[i]) : 0
13+
occurances.set(numbers[i], i)
14+
}
15+
return numbers[numbers.length - 1]
16+
}

0 commit comments

Comments
 (0)