Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 04 #6

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added aoc_2024/day_04/__init__.py
Empty file.
93 changes: 93 additions & 0 deletions aoc_2024/day_04/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from dataclasses import dataclass
from functools import cached_property
from typing import Sequence
from aoc_2024.day_04.parser import Parser


@dataclass
class Day04PartASolver:
input: Sequence[Sequence[str]]

@property
def solution(self) -> int:
return sum(
[
self._vertical_count,
self._horizontal_count,
self._lr_diagonal_count,
self._rl_diagonal_count,
]
)

@cached_property
def _forward(self) -> Sequence[str]:
return list("XMAS")

@cached_property
def _backward(self) -> Sequence[str]:
return self._forward[::-1]

@cached_property
def _height(self) -> int:
return len(self.input)

@cached_property
def _width(self) -> int:
return len(self.input[0])

@cached_property
def _vertical_count(self) -> int:
output = 0
for y in range(self._height - 3):
for x in range(self._width):
vals = [self.input[y + i][x] for i in range(4)]
if vals in (self._forward, self._backward):
output += 1
return output

@cached_property
def _horizontal_count(self) -> int:
output = 0
for y in range(self._height):
for x in range(self._width - 3):
vals = [self.input[y][x + i] for i in range(4)]
if vals in (self._forward, self._backward):
output += 1
return output

@cached_property
def _lr_diagonal_count(self) -> int:
output = 0
for y in range(self._height - 3):
for x in range(self._width - 3):
vals = [self.input[y + i][x + i] for i in range(4)]
if vals in (self._forward, self._backward):
output += 1
return output

@cached_property
def _rl_diagonal_count(self) -> int:
output = 0
for y in range(self._height - 3):
for x in range(self._width - 3):
vals = [self.input[y + 3 - i][x + i] for i in range(4)]
if vals in (self._forward, self._backward):
output += 1
return output


def solve(input: str) -> int:
data = Parser.parse(input)
solver = Day04PartASolver(data)

return solver.solution


def get_solution() -> int:
with open("aoc_2024/day_04/input.txt", "r") as f:
input = f.read()
return solve(input)


if __name__ == "__main__":
print(get_solution())
54 changes: 54 additions & 0 deletions aoc_2024/day_04/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from dataclasses import dataclass
from functools import cached_property
from typing import Sequence
from aoc_2024.day_04.parser import Parser


@dataclass
class Day04PartBSolver:
input: Sequence[Sequence[str]]

@property
def solution(self) -> int:
output = 0
target = (self._forward, self._backward)
for y in range(self._height - 2):
for x in range(self._width - 2):
lr_vals = [self.input[y + i][x + i] for i in range(3)]
rl_vals = [self.input[y + 2 - i][x + i] for i in range(3)]
if lr_vals in target and rl_vals in target:
output += 1
return output

@cached_property
def _forward(self) -> Sequence[str]:
return list("MAS")

@cached_property
def _backward(self) -> Sequence[str]:
return self._forward[::-1]

@cached_property
def _height(self) -> int:
return len(self.input)

@cached_property
def _width(self) -> int:
return len(self.input[0])


def solve(input: str) -> int:
data = Parser.parse(input)
solver = Day04PartBSolver(data)

return solver.solution


def get_solution() -> int:
with open("aoc_2024/day_04/input.txt", "r") as f:
input = f.read()
return solve(input)


if __name__ == "__main__":
print(get_solution())
Binary file added aoc_2024/day_04/from_prompt.py
Binary file not shown.
Binary file added aoc_2024/day_04/input.txt
Binary file not shown.
8 changes: 8 additions & 0 deletions aoc_2024/day_04/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Sequence


class Parser:
@staticmethod
def parse(input: str) -> Sequence[Sequence[str]]:
lines = input.strip().splitlines()
return [list(line) for line in lines]
Empty file added tests/test_day_04/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/test_day_04/test_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from aoc_2024.day_04.a import get_solution, solve
from aoc_2024.day_04.from_prompt import SAMPLE_DATA, SAMPLE_SOLUTION_A, SOLUTION_A


def test_solve():
assert solve(SAMPLE_DATA) == SAMPLE_SOLUTION_A


def test_my_solution():
assert get_solution() == SOLUTION_A
13 changes: 13 additions & 0 deletions tests/test_day_04/test_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from aoc_2024.day_04.b import get_solution, solve
from aoc_2024.day_04.from_prompt import SAMPLE_DATA, SAMPLE_SOLUTION_B, SOLUTION_B


def test_solve():
assert solve(SAMPLE_DATA) == SAMPLE_SOLUTION_B


@pytest.mark.skip
def test_my_solution():
assert get_solution() == SOLUTION_B
Loading