-
Notifications
You must be signed in to change notification settings - Fork 1
/
day07.nim
50 lines (38 loc) · 1.25 KB
/
day07.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
import strutils, tables
const instructions = readFile("./inputs/07.txt").strip.splitlines
type
Wire = ref object
value: int
hasValue: bool
logic: seq[string]
proc initialize(): Table[string, Wire] =
result = initTable[string, Wire]()
for line in instructions:
let values = line.split(" -> ")
result[values[1]] = Wire(logic: values[0].split())
var wires = initialize()
proc getValue(wireName: string): int =
if not wires.hasKey(wireName): return wireName.parseInt()
if wires[wireName].hasValue: return wires[wireName].value
let
wire = wires[wireName]
logic = wire.logic
case logic.len
of 1: result = getValue(logic[0])
of 2: result = not getValue(logic[1])
else:
case logic[1]
of "AND": result = getValue(logic[0]) and getValue(logic[2])
of "OR": result = getValue(logic[0]) or getValue(logic[2])
of "LSHIFT": result = getValue(logic[0]) shl getValue(logic[2])
of "RSHIFT": result = getValue(logic[0]) shr getValue(logic[2])
wire.hasValue = true
wire.value = result
let firstPart = getValue("a")
echo firstPart
for wire in wires.keys:
wires[wire].hasValue = false
wires["b"].value = firstPart
wires["b"].hasValue = true
let secondPart = getValue("a")
echo secondPart