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.

day21.nim 739B

123456789101112131415161718192021222324252627
  1. import elfcode, math, sequtils, strutils, sets
  2. var
  3. instructionSet = readInstructions(readFile("data/21.txt").strip.splitlines)
  4. seen = initSet[int](15000.nextPowerOfTwo)
  5. last = 0
  6. iterator emulate(): int =
  7. var r1, r4 = 0
  8. while true:
  9. r4 = r1 or 65536
  10. r1 = instructionSet.instructions[7].a
  11. while r4 > 0:
  12. r1 = (((r1 + (r4 and 255)) and instructionSet.instructions[10].b) *
  13. instructionSet.instructions[11].b) and instructionSet.instructions[12].b
  14. r4 = r4 div 256
  15. yield r1
  16. for i in emulate():
  17. if seen.len == 0:
  18. echo i
  19. if seen.containsOrIncl(i):
  20. echo last
  21. break
  22. last = i