Compare commits

..

No commits in common. "0ee0ea376005da1f3824d11c96b6f1c94ead7d09" and "9db475e3913e41032f025516faad6b307453ae76" have entirely different histories.

5 changed files with 57 additions and 25 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.0 KiB

View File

@ -2,7 +2,7 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2024 - 26 ⭐ - Python 2024 - 20 ⭐ - Python
</h1> </h1>
<a href="day01.py"> <a href="day01.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px"> <img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@ -34,13 +34,4 @@
<a href="day10.py"> <a href="day10.py">
<img src=".aoc_tiles/tiles/2024/10.png" width="161px"> <img src=".aoc_tiles/tiles/2024/10.png" width="161px">
</a> </a>
<a href="day11.py">
<img src=".aoc_tiles/tiles/2024/11.png" width="161px">
</a>
<a href="day12.py">
<img src=".aoc_tiles/tiles/2024/12.png" width="161px">
</a>
<a href="day13.py">
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
</a>
<!-- AOC TILES END --> <!-- AOC TILES END -->

View File

@ -1,3 +1,5 @@
# from sympy import solve
# from sympy.abc import x, y
from math import lcm from math import lcm
from tools.aoc import AOCDay from tools.aoc import AOCDay
from typing import Any from typing import Any
@ -6,15 +8,14 @@ BLOWUP_FACTOR = 10_000_000_000_000
def solve(machine: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], part2: bool = False) -> tuple[int, int]: def solve(machine: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], part2: bool = False) -> tuple[int, int]:
# ax + by = c # for machine[a][0] and machine[b][0]; and again for machine[x][1] # ax + by = c
# y = (c - ax) / b # y = (c - ax) / b
# And yes, I know I could have just used sympy or numpy, but I wanted to learn something; and this is fast enough lcm_a = lcm(machine[0][0], machine[1][0])
lcm_a = lcm(machine[0][0], machine[0][1])
tmb_one = lcm_a / machine[0][0] tmb_one = lcm_a / machine[0][0]
tmb_two = lcm_a / machine[0][1] tmb_two = lcm_a / machine[1][0]
a1 = machine[0][0] * tmb_one a1 = machine[0][0] * tmb_one
b1 = machine[1][0] * tmb_one a2 = machine[1][0] * tmb_two
b1 = machine[0][1] * tmb_one
b2 = machine[1][1] * tmb_two b2 = machine[1][1] * tmb_two
if not part2: if not part2:
c1 = machine[2][0] * tmb_one c1 = machine[2][0] * tmb_one
@ -23,12 +24,20 @@ def solve(machine: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], par
c1 = (machine[2][0] + BLOWUP_FACTOR) * tmb_one c1 = (machine[2][0] + BLOWUP_FACTOR) * tmb_one
c2 = (machine[2][1] + BLOWUP_FACTOR) * tmb_two c2 = (machine[2][1] + BLOWUP_FACTOR) * tmb_two
y = (c1 - c2) / (b1 - b2) ratio_a = a1 / a2
x = (c1 - (b1 * y)) / a1 ratio_b = b1 / b2
if x == int(x) and y == int(y): ratio_c = c1 / c2
return int(x), int(y)
if ratio_a == ratio_b == ratio_c:
return -1, -1 # infinite many solutions
elif ratio_a == ratio_b and ratio_b != ratio_c:
return -1, -1 # no solutions
else: else:
return -1, -1 y = (c1 - c2) / (b1 - b2)
x = (c1 - (b1 * y)) / a1
return x, y
pass
class Day(AOCDay): class Day(AOCDay):
@ -57,12 +66,44 @@ class Day(AOCDay):
return machines return machines
def part1(self) -> Any: def part1(self) -> Any:
solves = [solve(machine) for machine in self.parse_input()] tokens = 0
return sum(3 * x + y for x, y in solves if x > 0) for machine in self.parse_input():
x, y = solve(machine)
if x > 0 and y > 0:
print(machine, x, y, 3 * x + y)
tokens += 3 * x + y
return tokens
# found = False
# for ba_press in range(100):
# for bb_press in range(100):
# if (
# ba_press * machine[0][0] + bb_press * machine[1][0] == machine[2][0]
# and ba_press * machine[0][1] + bb_press * machine[1][1] == machine[2][1]
# ):
# tokens += 3 * ba_press + bb_press
# found = True
# break
# if found:
# break
# return tokens
def part2(self) -> Any: def part2(self) -> Any:
solves = [solve(machine, part2=True) for machine in self.parse_input()] tokens = 0
return sum(3 * x + y for x, y in solves if x > 0) # for machine in self.parse_input():
# ans = solve(
# [
# machine[0][0] * x + machine[1][0] * y - (machine[2][0] + BLOWUP_FACTOR),
# machine[0][1] * x + machine[1][1] * y - (machine[2][1] + BLOWUP_FACTOR),
# ],
# [x, y],
# dict=True,
# )
# if len(ans) == 1 and ans[0][x] == int(ans[0][x]) and ans[0][y] == int(ans[0][y]):
# tokens += 3 * ans[0][x] + ans[0][y]
return tokens
if __name__ == "__main__": if __name__ == "__main__":