-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.nim
55 lines (44 loc) · 1.22 KB
/
day14.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import strutils, sequtils
const input = 765071
type
Solution = tuple[first: string, second: int]
proc matches(rl, digits: seq[int8], nrOfRecipes: int): bool =
for i in 1 .. 6:
if rl[nrOfRecipes-i] != digits[6-i]:
return false
return true
template put(rl: seq[int8], value: int8) =
rl[nrOfRecipes] = value
inc nrOfRecipes
template move(elf: var int, digit: int8) =
inc elf
elf += digit
while elf >= nrOfRecipes:
elf -= nrOfRecipes
proc solve(digits: seq[int8]): Solution =
var
firstDigit, secondDigit, sum: int8
firstElf = 0
secondElf = 1
nrOfRecipes = 2
recipeList = newSeq[int8](22_000_000) # manually tweaked
recipeList[firstElf] = 3
recipeList[secondElf] = 7
while true:
firstDigit = recipeList[firstElf]
secondDigit = recipeList[secondElf]
sum = firstDigit + secondDigit
if sum > 9:
recipeList.put 1
sum -= 10
if recipeList.matches(digits, nrOfRecipes):
break
recipeList.put sum
firstElf.move firstDigit
secondElf.move secondDigit
result.first = recipeList[input ..< input+10].join
result.second = nrOfRecipes - 6
let digits = toSeq($input).mapIt(it.int8 - '0'.int8)
let s = solve digits
echo s.first
echo s.second