aboutsummaryrefslogtreecommitdiffstats
path: root/day21/solution.nim
blob: bbf77bd9978d22a9ced73a7d5b1d8999073707cc (plain) (blame)
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
import strutils
import sequtils
import tables
import strformat

type
  Node = ref object of RootObj
    left, right: string
    val: int

  AddNode = ref object of Node
  SubNode = ref object of Node
  MulNode = ref object of Node
  DivNode = ref object of Node

var nodes: Table[string, Node]

method calc(node: Node): int =
  node.val

method calc(node: AddNode): int = 
  nodes[node.left].calc() + nodes[node.right].calc()

method calc(node: SubNode): int = 
  nodes[node.left].calc() - nodes[node.right].calc()

method calc(node: MulNode): int = 
  nodes[node.left].calc() * nodes[node.right].calc()

method calc(node: DivNode): int = 
  nodes[node.left].calc() div nodes[node.right].calc()

proc `$`(node: Node): string =
  fmt"val: {node.val}, left: {node.left}, right: {node.right}"

proc parseLine(line: string): void =
  let tokens = line.split(": ")
  let id = tokens[0]
  let ops = tokens[1].split(" ")

  # if single token -> its a number
  if ops.len() == 1:
    nodes[id] = Node(val: ops[0].parseInt())
  else:
    let left = ops[0]
    let right = ops[2]
    case ops[1]:
      of "+":
        nodes[id] = AddNode(left: left, right: right)
      of "-":
        nodes[id] = SubNode(left: left, right: right)
      of "*":
        nodes[id] = MulNode(left: left, right: right)
      of "/":
        nodes[id] = DivNode(left: left, right: right)
      else:
        assert(false)

proc parseFile(): void =
  let lines = readFile("./input.txt").strip().splitLines()

  for line in lines:
    parseLine(line)

parseFile()
echo nodes["root"].calc()