-
Notifications
You must be signed in to change notification settings - Fork 3
/
day23.nim
43 lines (35 loc) · 926 Bytes
/
day23.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
import strutils, tables, math
const instructions = readFile("./inputs/23.txt").splitLines
func solve(part: int): int =
var
reg = initTable[string, int]()
i = 0
reg["a"] = part - 1
func interpret(s: string): int =
if s[0].isAlphaAscii: reg.getOrDefault(s) else: s.parseInt
while i < 11:
let
line = split instructions[i]
v = interpret line[^1]
r = line[1]
case line[0]
of "set": reg[r] = v
of "sub": reg[r] -= v
of "mul": reg[r] *= v
of "jnz":
if interpret(r) != 0:
i += v
continue
inc i
if part == 1:
return (reg["b"] - reg["e"]) * (reg["b"] - reg["d"])
else:
var nonprimes = (reg["c"] - reg["b"]) div 34 + 1
for b in countup(reg["b"]+17, reg["c"], 34):
for d in countup(3, sqrt(b.float).int, 2):
if b mod d == 0:
inc nonprimes
break
return nonprimes
echo solve 1
echo solve 2