My solutions to 2018's advent of code
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

day07.nim 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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: ptr 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.addr.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. status.deepCopy(dependencies)
  28. while len(completed) < 26:
  29. time += 1
  30. for i in 0..25:
  31. if task_times[i] == 1:
  32. task_times[i] = 0
  33. completed &= chr(i + 65)
  34. status.addr.execute(i)
  35. elif task_times[i] > 1:
  36. task_times[i] -= 1
  37. for i in 0..4:
  38. if worker_times[i] > 0:
  39. worker_times[i] -= 1
  40. if worker_times[i] == 0:
  41. var task = status.next_task
  42. if task > -1:
  43. worker_times[i] = 61 + task
  44. task_times[task] = 61 + task
  45. status[task][0] = true
  46. time - 1
  47. echo dependencies.part1
  48. echo dependencies.part2