aoc2024/day04.py
2024-12-04 06:44:10 +01:00

59 lines
1.6 KiB
Python

from tools.aoc import AOCDay
from typing import Any
from tools.grid import Grid
class Day(AOCDay):
inputs = [
[
(18, "input4_test"),
(2370, "input4"),
],
[
(9, "input4_test"),
(1908, "input4"),
],
]
def part1(self) -> Any:
match = 0
word_search = Grid.from_data(self.getInput(), default="")
for current_pos in word_search.getActiveCells():
if word_search.get(current_pos) != "X":
continue
for dir in [i - current_pos for i in word_search.getNeighboursOf(current_pos)]:
if "".join(word_search.get(current_pos + dir * j) for j in range(4)) == "XMAS":
match += 1
return match
def part2(self) -> Any:
match = 0
word_search = Grid.from_data(self.getInput(), default="")
for current_pos in word_search.getActiveCells():
s = "".join(
[
word_search.get(current_pos),
word_search.get(current_pos + (1, 1)),
word_search.get(current_pos + (2, 2)),
]
)
if s != "MAS" and s != "SAM":
continue
s2 = "".join(
[
word_search.get(current_pos + (0, 2)),
word_search.get(current_pos + (1, 1)),
word_search.get(current_pos + (2, 0)),
]
)
if s2 == "MAS" or s2 == "SAM":
match += 1
return match
if __name__ == "__main__":
day = Day(2024, 4)
day.run(verbose=True)