-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_15.js
More file actions
51 lines (40 loc) · 1.07 KB
/
day_15.js
File metadata and controls
51 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
const rawInput = []
// The actual input
rawInput[0] = [2, 0, 1, 9, 5, 19]
// The 1-st example
rawInput[1] = [0, 3, 6]
// The 2-nd example
rawInput[2] = [2, 3, 1]
let INPUT = 0 // Input data selector.
const execute = require('./execute')
let diffOfLastSpoken
const algorithm1 = (said, value, turn, firstEound) => {
let say
if (firstEound) {
say = value
diffOfLastSpoken = 0
} else {
say = diffOfLastSpoken
diffOfLastSpoken = said.has(say) ? turn - said.get(say) : 0
}
said.set(say, turn)
return say
}
const compute = (algorithm, interesting) => {
let input = rawInput[INPUT], num
const spoken = new Map(), len = input.length
for (let i = 0; i < interesting; ++i) {
const j = i % len
num = algorithm(spoken, input[j], i, i < len)
}
return num
}
execute('puzzle #1', compute, algorithm1, 2020)
execute('puzzle #2', compute, algorithm1, 30000000)
// puzzle #1: 1009
// elapsed: 547 µsecs
//
// puzzle #2: 62714
// elapsed: 5476375 µsecs
// 5567 golds, 1391 silvers by that time