Advertisement
JonathanGupton

Advent of Code 2024 - Day 04 - Python

Dec 4th, 2024 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. from pathlib import Path
  2.  
  3. """
  4. DIRECTION REFERENCE
  5.  
  6. (-1,-1)(0,-1)(1, -1)
  7. (-1, 0)(0, 0)(1, 0)
  8. (-1, 1)(0, 1)(1,  1)
  9. """
  10.  
  11. XMAS_DIRECTIONS = {
  12.     "north": ((complex(0, -1), "M"), (complex(0, -2), "A"), (complex(0, -3), "S")),
  13.     "northeast": ((complex(1, -1), "M"), (complex(2, -2), "A"), (complex(3, -3), "S")),
  14.     "east": ((complex(1, 0), "M"), (complex(2, 0), "A"), (complex(3, 0), "S")),
  15.     "southeast": ((complex(1, 1), "M"), (complex(2, 2), "A"), (complex(3, 3), "S")),
  16.     "south": ((complex(0, 1), "M"), (complex(0, 2), "A"), (complex(0, 3), "S")),
  17.     "southwest": ((complex(-1, 1), "M"), (complex(-2, 2), "A"), (complex(-3, 3), "S")),
  18.     "west": ((complex(-1, 0), "M"), (complex(-2, 0), "A"), (complex(-3, 0), "S")),
  19.     "northwest": (
  20.         (complex(-1, -1), "M"),
  21.         (complex(-2, -2), "A"),
  22.         (complex(-3, -3), "S"),
  23.     ),
  24. }
  25.  
  26. X_MAS_DIRECTIONS = {
  27.     "north": (
  28.         (complex(-1, -1), "M"),
  29.         (complex(1, -1), "M"),
  30.         (complex(-1, 1), "S"),
  31.         (complex(1, 1), "S"),
  32.     ),
  33.     "east": (
  34.         (complex(1, -1), "M"),
  35.         (complex(1, 1), "M"),
  36.         (complex(-1, 1), "S"),
  37.         (complex(-1, -1), "S"),
  38.     ),
  39.     "south": (
  40.         (complex(1, 1), "M"),
  41.         (complex(-1, 1), "M"),
  42.         (complex(-1, -1), "S"),
  43.         (complex(1, -1), "S"),
  44.     ),
  45.     "west": (
  46.         (complex(-1, -1), "M"),
  47.         (complex(-1, 1), "M"),
  48.         (complex(1, -1), "S"),
  49.         (complex(1, 1), "S"),
  50.     ),
  51. }
  52.  
  53.  
  54. def load_wordsearch(fp: Path) -> dict[complex, str]:
  55.     result = {}
  56.     with open(fp, "r") as f:
  57.         for i, line in enumerate(f.readlines()):
  58.             for j, char in enumerate(line):
  59.                 result[complex(j, i)] = char
  60.     return result
  61.  
  62.  
  63. def get_adjacent_count(
  64.     start: complex,
  65.     wordsearch: dict[complex, str],
  66.     pattern: dict[str, tuple[tuple[complex, str], ...]],
  67. ) -> int:
  68.     found = 0
  69.     for cardinal, direction in pattern.items():
  70.         if all(wordsearch.get(start + d) == c for d, c in direction):
  71.             found += 1
  72.     return found
  73.  
  74.  
  75. def xmas_wordsearch(wordsearch: dict[complex, str]) -> int:
  76.     found = 0
  77.     for k, v in wordsearch.items():
  78.         if v == "X":
  79.             found += get_adjacent_count(k, wordsearch, XMAS_DIRECTIONS)
  80.     return found
  81.  
  82.  
  83. def x_mas_wordsearch(wordsearch: dict[complex, str]) -> int:
  84.     found = 0
  85.     for k, v in wordsearch.items():
  86.         if v == "A":
  87.             found += get_adjacent_count(k, wordsearch, X_MAS_DIRECTIONS)
  88.     return found
  89.  
  90.  
  91. def example_part_a():
  92.     fp = Path("./example/day04-example-01.txt")
  93.     wordsearch = load_wordsearch(fp)
  94.     found = xmas_wordsearch(wordsearch)
  95.     print(found, " == 4", sep="")  # 4
  96.  
  97.     fp = Path("./example/day04-example-02.txt")
  98.     wordsearch = load_wordsearch(fp)
  99.     found = xmas_wordsearch(wordsearch)
  100.     print(found, " == 18", sep="")  # 18
  101.  
  102.  
  103. def part_a():
  104.     fp = Path("./data/day04.txt")
  105.     wordsearch = load_wordsearch(fp)
  106.     found = xmas_wordsearch(wordsearch)
  107.     print(found)
  108.  
  109.  
  110. def example_part_b():
  111.     fp = Path("./example/day04-example-02.txt")
  112.     wordsearch = load_wordsearch(fp)
  113.     found = x_mas_wordsearch(wordsearch)
  114.     print(found, " == 9", sep="")  # 9
  115.  
  116.  
  117. def part_b():
  118.     fp = Path("./data/day04.txt")
  119.     wordsearch = load_wordsearch(fp)
  120.     found = x_mas_wordsearch(wordsearch)
  121.     print(found)
  122.  
  123.  
  124. if __name__ == "__main__":
  125.     example_part_a()
  126.     example_part_b()
  127.     part_a()
  128.     part_b()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement