-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.nim
42 lines (29 loc) · 1.08 KB
/
day15.nim
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
import tables
proc nextNumber(numberHistory: var OrderedTable[int, int], currentTurn: int,
lastNumber: int): int =
let lastTurn = numberHistory.getOrDefault(lastNumber)
let nextNumber = (if lastTurn == 0: 0 else: currentTurn - lastTurn)
numberHistory[lastNumber] = currentTurn
return nextNumber
# Get input sequence
let puzzleInput = [1,0,15,2,10,13]
var numberHistory: OrderedTable[int, int]
for turn, number in puzzleInput[0..^2]:
numberHistory[number] = turn + 1
let startingTurn = numberHistory.len + 1
var lastNumber = puzzleInput[^1]
for currentTurn in startingTurn .. 2020 - 1 :
lastNumber = nextNumber(numberHistory, currentTurn, lastNumber)
# Print results
echo "--- Part 1 Report ---"
echo "Number = " & $lastNumber
## Part 2
numberHistory = initOrderedTable[int, int]()
for turn, number in puzzleInput[0..^2]:
numberHistory[number] = turn + 1
lastNumber = puzzleInput[^1]
for currentTurn in startingTurn .. 30000000 - 1 :
lastNumber = nextNumber(numberHistory, currentTurn, lastNumber)
# Print results
echo "--- Part 2 Report ---"
echo "Number = " & $lastNumber