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.

day07.nim 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import sequtils, strutils
  2. var
  3. dependencies: array[26, array[27, bool]]
  4. for line in readFile("data/07.txt").strip.splitlines:
  5. dependencies[ord(line[36]) - 65][ord(line[5]) - 64] = true
  6. func next_task(status: array[26, array[27, bool]]): int =
  7. var falses: array[27, bool]
  8. status.find(falses)
  9. func execute(status: var array[26, array[27, bool]], action: int) =
  10. status[action][0] = true
  11. for i in 0..25:
  12. status[i][action + 1] = false
  13. func part1(dependencies: array[26, array[27, bool]]): string =
  14. var status: array[26, array[27, bool]]
  15. status.deepCopy(dependencies)
  16. for i in 0..25:
  17. var task = status.next_task
  18. status.execute(task)
  19. result &= chr(task + 65)
  20. func part2(dependencies: array[26, array[27, bool]]): int =
  21. var
  22. status: array[26, array[27, bool]]
  23. worker_times: array[5, int]
  24. task_times: array[26, int]
  25. completed = ""
  26. time = 0
  27. step = 0
  28. status.deepCopy(dependencies)
  29. while len(completed) < 26:
  30. time += step
  31. step = int.high
  32. for i in 0..25:
  33. if task_times[i] > 0:
  34. if task_times[i] <= time:
  35. task_times[i] = 0
  36. completed &= chr(i + 65)
  37. status.execute(i)
  38. else:
  39. step = min(step, task_times[i] - time)
  40. for i in 0..4:
  41. if worker_times[i] <= time:
  42. var task = status.next_task
  43. if task > -1:
  44. worker_times[i] = time + 61 + task
  45. task_times[task] = time + 61 + task
  46. status[task][0] = true
  47. step = min(step, 61 + task)
  48. time
  49. echo dependencies.part1
  50. echo dependencies.part2