main.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 os, c in instructions:
  18. if os == "y":
  19. x = int(c) * 2 + 1
  20. if os == "x":
  21. y = int(c) * 2 + 1
  22. if x != 0 and y != 0:
  23. paper = np.zeros((x, y), dtype=np.int8)
  24. break
  25. paper[coords[:, 0], coords[:, 1]] = 1
  26. for step, (os, c) in enumerate(instructions):
  27. if os == "y":
  28. paper = paper[: int(c)] + np.flipud(paper[int(c) + 1 :])
  29. if os == "x":
  30. paper = paper[:, : int(c)] + np.fliplr(paper[:, int(c) + 1 :])
  31. if step == 0:
  32. part1 = np.where(paper > 0, 1, 0).sum()
  33. part2 = np.where(paper > 0, "#", ".")
  34. return part1, part2
  35. def part1(input: list[str]):
  36. part1, _ = folding(input)
  37. print("The anwer of part1 is:", part1)
  38. def part2(input: list[str]):
  39. _, part2 = folding(input)
  40. print("The answer of part2 is:\n")
  41. for line in part2:
  42. print("".join(line))
  43. if __name__ == "__main__":
  44. input, example = get_input(task_dir, 13)
  45. check_example(example, part1)
  46. part1(input)
  47. part2(input)
  48. generate_readme(task_dir, 13)