-
Notifications
You must be signed in to change notification settings - Fork 3
/
day08.nim
42 lines (31 loc) · 972 Bytes
/
day08.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, strutils
const instructions = readFile("./inputs/08.txt").splitLines()
type Instruction = tuple
reg, instr, condReg, condOp: string
amount, value: int
func parseLine(w: seq[string]): Instruction =
(w[0], w[1], w[^3], w[^2], w[2].parseInt, w[^1].parseInt)
func isConditionSatisfied(op: string, regVal, value: int): bool =
case op
of "<": regVal < value
of ">": regVal > value
of "<=": regVal <= value
of ">=": regVal >= value
of "==": regVal == value
else: regVal != value
var
registers = initCountTable[string]()
maximum: int
for line in instructions:
let
l = line.split().parseLine()
regVal = registers.getOrDefault(l.condReg)
if isConditionSatisfied(l.condOp, regVal, l.value):
case l.instr
of "inc":
registers.inc(l.reg, l.amount)
of "dec":
registers.inc(l.reg, -l.amount)
maximum = max(registers[l.reg], maximum)
echo registers.largest.val
echo maximum