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 07 #11

Merged
merged 2 commits into from
Dec 7, 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_07/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions aoc_2024/day_07/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from dataclasses import dataclass
from typing import Generator
from aoc_2024.day_07.parser import Parser


@dataclass
class Day07PartASolver:
equations: list[tuple[int, list[int]]]

@property
def solution(self) -> int:
output = 0
for answer, nums in self.equations:
if self.can_be_true(answer, nums):
output += answer
return output

def can_be_true(self, answer: int, nums: list[int]) -> bool:
return True in self.has_solution(answer, nums, nums[0], 1)

def has_solution(
self,
answer: int,
nums: list[int],
so_far: int,
index: int,
) -> Generator[bool, None, None]:
if so_far > answer:
yield False
elif index == len(nums):
yield so_far == answer
else:
yield from self.has_solution(answer, nums, so_far + nums[index], index + 1)
yield from self.has_solution(answer, nums, so_far * nums[index], index + 1)


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

return solver.solution


def get_solution() -> int:
with open("aoc_2024/day_07/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_07/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from dataclasses import dataclass
from typing import Generator
from aoc_2024.day_07.parser import Parser


@dataclass
class Day07PartBSolver:
equations: list[tuple[int, list[int]]]

@property
def solution(self) -> int:
output = 0
for answer, nums in self.equations:
if self.can_be_true(answer, nums):
output += answer
return output

def can_be_true(self, answer: int, nums: list[int]) -> bool:
return True in self.has_solution(answer, nums, nums[0], 1)

def has_solution(
self,
answer: int,
nums: list[int],
so_far: int,
index: int,
) -> Generator[bool, None, None]:
if so_far > answer:
yield False
elif index == len(nums):
yield so_far == answer
else:
yield from self.has_solution(answer, nums, so_far + nums[index], index + 1)
yield from self.has_solution(answer, nums, so_far * nums[index], index + 1)
yield from self.has_solution(
answer, nums, int(f"{so_far}{nums[index]}"), index + 1
)


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

return solver.solution


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


if __name__ == "__main__":
print(get_solution())
Binary file added aoc_2024/day_07/from_prompt.py
Binary file not shown.
Binary file added aoc_2024/day_07/input.txt
Binary file not shown.
10 changes: 10 additions & 0 deletions aoc_2024/day_07/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Parser:
@staticmethod
def parse(input: str) -> list[tuple[int, list[int]]]:
lines = input.strip().splitlines()
return [Parser.parse_line(line) for line in lines]

@staticmethod
def parse_line(line: str) -> tuple[int, list[int]]:
left, right = line.split(": ")
return int(left), [int(num) for num in right.split(" ")]
Empty file added tests/test_day_07/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/test_day_07/test_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from aoc_2024.day_07.a import get_solution, solve
from aoc_2024.day_07.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
10 changes: 10 additions & 0 deletions tests/test_day_07/test_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from aoc_2024.day_07.b import get_solution, solve
from aoc_2024.day_07.from_prompt import SAMPLE_DATA, SAMPLE_SOLUTION_B, SOLUTION_B


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


def test_my_solution():
assert get_solution() == SOLUTION_B
Loading