-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
87 lines (74 loc) · 1.58 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
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
type
Inst = seq[string]
Computer = ref object
ip: int
insts: seq[Inst]
registers: Table[string, int]
proc newComputer(insts: seq[Inst]): Computer =
result.new
result.insts = insts
result.registers = { "a": 0, "b": 0, "c": 0, "d": 0 }.toTable
proc run(self: Computer) =
while self.ip < self.insts.len:
let inst = self.insts[self.ip]
case inst[0]:
of "cpy":
let x = if inst[1] in "abcd": self.registers[inst[1]]
else: inst[1].parseInt
let y = inst[2]
self.registers[y] = x
of "inc":
let x = inst[1]
self.registers[x] += 1
of "dec":
let x = inst[1]
self.registers[x] -= 1
of "jnz":
let x = if inst[1] in "abcd": self.registers[inst[1]]
else: inst[1].parseInt
let y = inst[2].parseInt
if x != 0:
self.ip += y
continue
self.ip += 1
proc parseLine(line: string): Inst =
line.split(" ").toSeq
proc part1(input: string): int =
let insts = input.split("\n").mapIt(it.parseLine)
let c = newComputer(insts)
c.run
c.registers["a"]
when defined(test):
let input = """
cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a
""".strip
doAssert part1(input) == 42
proc part2(input: string): int =
let insts = input.split("\n").mapIt(it.parseLine)
let c = newComputer(insts)
c.registers["c"] = 1
c.run
c.registers["a"]
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)