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 821B

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