main.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, check_example, generate_readme
  6. def folding(input: list[str]) -> tuple[int, np.ndarray]:
  7. coords = np.array(
  8. [
  9. (line.split(",")[1], line.split(",")[0])
  10. for line in input
  11. if len(line) > 1 and len(line) < 12
  12. ],
  13. dtype=int,
  14. )
  15. instructions = [line[11:].split("=") for line in input if line.startswith("f")]
  16. x, y = 0, 0
  17. for inst in instructions:
  18. match inst:
  19. case ["y", c]: x = int(c) * 2 + 1
  20. case ["x", c]: y = int(c) * 2 + 1
  21. if x != 0 and y != 0: paper = np.zeros((x, y), dtype=np.int8); break
  22. paper[coords[:, 0], coords[:, 1]] = 1
  23. for step, inst in enumerate(instructions):
  24. match inst:
  25. case ["y", c]: paper = paper[: int(c)] + np.flipud(paper[int(c) + 1 :])
  26. case ["x", c]: paper = paper[:, : int(c)] + np.fliplr(paper[:, int(c) + 1 :])
  27. if step == 0:
  28. part1 = np.where(paper > 0, 1, 0).sum()
  29. part2 = np.where(paper > 0, "#", ".")
  30. return part1, part2
  31. def part1(input: list[str]):
  32. part1, _ = folding(input)
  33. print("The anwer of part1 is:", part1)
  34. def part2(input: list[str]):
  35. _, part2 = folding(input)
  36. print("The answer of part2 is:\n")
  37. for line in part2:
  38. print("".join(line))
  39. if __name__ == "__main__":
  40. input, example = get_input(task_dir, 13)
  41. check_example(example, part1)
  42. part1(input)
  43. part2(input)
  44. generate_readme(task_dir, 13)