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.

day14.nim 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import sequtils, strutils
  2. let
  3. input = readFile("data/14.txt").strip
  4. inputSeq = input.map do (c: char) -> int8: cast[int8](c.int - '0'.int)
  5. inputInt = input.parseInt
  6. inputLen = input.len
  7. var
  8. scores = @[3'i8, 7'i8]
  9. elf1 = 0
  10. elf2 = 1
  11. part2 = -1
  12. proc check(inputSeq: seq[int8], val: int, part2: var int) {.inline.} =
  13. var found {.global.} = 0
  14. if val == inputSeq[found]:
  15. found.inc
  16. if found == inputLen:
  17. part2 = scores.len - inputLen
  18. found = 0
  19. else:
  20. found = 0
  21. while scores.len < inputInt + 10 or part2 == -1:
  22. var score = scores[elf1] + scores[elf2]
  23. if score >= 10:
  24. scores.add(1)
  25. inputSeq.check(1, part2)
  26. let newScore = cast[int8](score mod 10)
  27. scores.add(newScore)
  28. inputSeq.check(newScore, part2)
  29. elf1 = (elf1 + scores[elf1] + 1) mod scores.len
  30. elf2 = (elf2 + scores[elf2] + 1) mod scores.len
  31. echo scores[inputInt..inputInt+9].join
  32. echo part2