My solutions to 2018's advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

day08.nim 936B

1234567891011121314151617181920212223242526272829303132
  1. import math, sequtils, strutils
  2. func readNode(input: seq[string], start: int): tuple[value: int, metasum: int, offset: int] =
  3. let
  4. child_count = input[start + 0].parseInt
  5. meta_count = input[start + 1].parseInt
  6. var
  7. value = 0
  8. metasum = 0
  9. offset = start + 2
  10. child_values: seq[int]
  11. for i in 0 .. child_count - 1:
  12. var res = readNode(input, offset)
  13. child_values.add(res.value)
  14. offset = res.offset
  15. metasum += res.metasum
  16. for m in input[offset .. offset + meta_count - 1]:
  17. var v = m.parseInt
  18. if v > 0 and v - 1 < child_count:
  19. value += child_values[v - 1]
  20. metasum += v
  21. offset += meta_count
  22. if child_count == 0:
  23. value = metasum
  24. (value, metasum, offset)
  25. let root = readNode(readFile("data/08.txt").strip.split(" "), 0)
  26. echo root.metasum
  27. echo root.value