main.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os, sys
  2. import numpy as np
  3. task_dir = os.path.dirname(__file__)
  4. sys.path.append(f"{task_dir}/..")
  5. from get_tasks import get_input, generate_readme, check_example
  6. def update_step_count(step: np.ndarray) -> tuple[int, np.ndarray, bool]:
  7. acc = 0
  8. while len(np.where((step > 9) & (step < 1000))[0]) != 0:
  9. x, y = np.where((step > 9) & (step < 1000))
  10. for idx in range(len(x)):
  11. step[max(0, x[idx] - 1) : x[idx] + 2, max(0, y[idx] - 1) : y[idx] + 2] += 1
  12. step[x[idx], y[idx]] = 1000 + acc
  13. acc += 1
  14. return (
  15. len(step[step > 9]),
  16. np.where(step > 9, 0, step),
  17. len(step[step > 9]) == step.size,
  18. )
  19. def part1(input: list[str]):
  20. octopuses = np.array([list(line.strip()) for line in input], dtype=int)
  21. sum_flashes = 0
  22. for _ in range(1, 101):
  23. octopuses += 1
  24. flashes, octopuses, _ = update_step_count(octopuses)
  25. sum_flashes += flashes
  26. print("The asnwer of part1 is:", sum_flashes)
  27. def part2(input: list[str]):
  28. octopuses = np.array([list(line.strip()) for line in input], dtype=int)
  29. step = 0
  30. all_flash = False
  31. while not all_flash:
  32. octopuses += 1
  33. _, _, all_flash = update_step_count(octopuses)
  34. step += 1
  35. print("The answer if part2 is:", step)
  36. if __name__ == "__main__":
  37. input, example = get_input(task_dir, 11)
  38. check_example(example, part1)
  39. check_example(example, part2)
  40. part1(input)
  41. part2(input)
  42. generate_readme(task_dir, 11)