-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
91 lines (74 loc) · 1.75 KB
/
solution.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
type
Inst = seq[string]
proc parseLine(line: string): Inst =
line.split(" ")
proc parse(input: string): seq[Inst] =
input.split("\n").mapIt(it.parseLine)
type
Processor = ref object
regs: Table[string, int]
ip: int
insts: seq[Inst]
proc newProcessor(insts: seq[Inst]): Processor =
result.new
result.insts = insts
for r in 'a' .. 'h':
result.regs[$r] = 0
proc getVal(self: Processor, x: string): int =
if x in self.regs: self.regs[x]
else: x.parseInt
proc debug(self: Processor): int =
while self.ip in 0 ..< self.insts.len:
let inst = self.insts[self.ip]
case inst[0]:
of "set":
let (x, y) = (inst[1], inst[2])
self.regs[x] = self.getVal(y)
of "sub":
let (x, y) = (inst[1], inst[2])
self.regs[x] = self.regs[x] - self.getVal(y)
of "mul":
let (x, y) = (inst[1], inst[2])
self.regs[x] = self.regs[x] * self.getVal(y)
result += 1
of "jnz":
let (x, y) = (inst[1], inst[2])
if self.getVal(x) != 0:
self.ip += self.getVal(y)
continue
self.ip += 1
proc part1(input: string): int =
let p = newProcessor(input.parse)
p.debug
proc isPrime(n: int): bool =
var p = 2
while p * p <= n:
if n mod p == 0: return false
p += 1
true
proc part2(input: string): int =
let insts = input.parse
let p = newProcessor(insts[0 ..< 8])
p.regs["a"] = 1
discard p.debug
var (b, c) = (p.regs["b"], p.regs["c"])
let step = insts[30][2].parseInt.abs
countup(b, c, step).countIt(not it.isPrime)
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)