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.1KB

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. inputHead = inputSeq[0..inputSeq.len-2]
  6. lastDigit = inputSeq[inputSeq.len - 1]
  7. inputInt = input.parseInt
  8. var
  9. scores: array[50_000_000, int8] # Arbitrary large preallocation
  10. size = 2
  11. elf1 = 0
  12. elf2 = 1
  13. part2 = -1
  14. scores[0] = 3
  15. scores[1] = 7
  16. while size < inputInt + 10 or part2 == -1:
  17. var score = scores[elf1] + scores[elf2]
  18. if score >= 10:
  19. scores[size] = 1
  20. size.inc
  21. if 1 == lastDigit and size > input.len and scores[size-input.len..size-2] == inputHead:
  22. part2 = size - input.len
  23. let newScore = cast[int8](score mod 10)
  24. scores[size] = newScore
  25. size.inc
  26. if newScore == lastDigit and size > input.len and scores[size-input.len..size-2] == inputHead:
  27. part2 = size - input.len
  28. elf1 = (elf1 + scores[elf1] + 1) mod size
  29. elf2 = (elf2 + scores[elf2] + 1) mod size
  30. echo scores[inputInt..inputInt+9].join
  31. echo part2