|
| 1 | +import re |
| 2 | +from itertools import product |
| 3 | + |
| 4 | +from utils.file import read_input |
| 5 | + |
| 6 | +lines: list[str] = read_input(__package__) |
| 7 | + |
| 8 | + |
| 9 | +class WordMatcher: |
| 10 | + def __init__(self, words: set[str]) -> None: |
| 11 | + self._words = words |
| 12 | + self._count = 0 |
| 13 | + |
| 14 | + def check(self, word: str) -> None: |
| 15 | + if word in self._words: |
| 16 | + self._count += 1 |
| 17 | + |
| 18 | + @property |
| 19 | + def count(self) -> int: |
| 20 | + return self._count |
| 21 | + |
| 22 | + |
| 23 | +word_matcher = WordMatcher({"XMAS", "SAMX"}) |
| 24 | + |
| 25 | +h, w = len(lines), len(lines[0]) |
| 26 | +for i, j in product(range(h), range(w)): |
| 27 | + if j < w - 3: |
| 28 | + candidate_right = lines[i][j : j + 4] |
| 29 | + word_matcher.check(lines[i][j : j + 4]) |
| 30 | + |
| 31 | + if i < h - 3: |
| 32 | + candidate_down = "".join(lines[i + k][j] for k in range(4)) |
| 33 | + word_matcher.check(candidate_down) |
| 34 | + |
| 35 | + if i < h - 3 and j < w - 3: |
| 36 | + candidate_down_right = "".join(lines[i + k][j + k] for k in range(4)) |
| 37 | + word_matcher.check(candidate_down_right) |
| 38 | + |
| 39 | + if j >= 3 and i < h - 3: |
| 40 | + candidate_down_left = "".join(lines[i + k][j - k] for k in range(4)) |
| 41 | + word_matcher.check(candidate_down_left) |
| 42 | + |
| 43 | +print("Part 1:", word_matcher.count) |
| 44 | + |
| 45 | +count2 = 0 |
| 46 | +EXPECTED_CORNERS = {"MMSS", "MSSM", "SSMM", "SMMS"} |
| 47 | +for i, j in product(range(1, h - 1), range(1, w - 1)): |
| 48 | + if lines[i][j] != "A": |
| 49 | + continue |
| 50 | + corners = "".join([lines[i - 1][j - 1], lines[i - 1][j + 1], lines[i + 1][j + 1], lines[i + 1][j - 1]]) |
| 51 | + if corners in EXPECTED_CORNERS: |
| 52 | + count2 += 1 |
| 53 | + |
| 54 | +print("Part 1:", count2) |
0 commit comments