Skip to content

Commit 4528397

Browse files
committed
[2024/13] p1 & p2 solved
1 parent 5c86edc commit 4528397

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

.aoc_tiles/tiles/2024/13.png

4.95 KB
Loading

2024/13/example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Button A: X+94, Y+34
2+
Button B: X+22, Y+67
3+
Prize: X=8400, Y=5400
4+
5+
Button A: X+26, Y+66
6+
Button B: X+67, Y+21
7+
Prize: X=12748, Y=12176
8+
9+
Button A: X+17, Y+86
10+
Button B: X+84, Y+37
11+
Prize: X=7870, Y=6450
12+
13+
Button A: X+69, Y+23
14+
Button B: X+27, Y+71
15+
Prize: X=18641, Y=10279

2024/13/script.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from GhostyUtils import aoc
2+
from GhostyUtils.vec2 import Vec2
3+
4+
5+
def solve(machine: list[list[tuple], tuple], offset: int = 0):
6+
a, b = [Vec2(button) for button in machine[0]]
7+
p = Vec2(machine[1]) + Vec2(offset, offset)
8+
9+
if aoc.args.progress or aoc.args.verbose:
10+
print(f"A vec: {a}, B vec: {b}, Prize location: {p}")
11+
12+
det = (a.x * b.y - a.y * b.x)
13+
14+
a_presses = int((p.x * b.y - p.y * b.x) / det)
15+
b_presses = int((p.y * a.x - p.x * a.y) / det)
16+
17+
if a * a_presses + b * b_presses == p:
18+
if aoc.args.progress or aoc.args.verbose:
19+
print(f"> A: {a_presses}x, B: {b_presses}x, Tokens: {a_presses * 3 + b_presses}")
20+
return a_presses * 3 + b_presses
21+
else:
22+
if aoc.args.progress or aoc.args.verbose:
23+
print("> Misses")
24+
return 0
25+
26+
27+
def main():
28+
inputs = aoc.read_sections()
29+
30+
machines = []
31+
for mch in inputs:
32+
mch = mch.splitlines()
33+
buttons = [tuple(int(coord[2:])
34+
for coord in btn.split(': ')[1].split(', '))
35+
for btn in mch[:2]]
36+
prize = tuple(int(coord[2:]) for coord in mch[2].split(': ')[1].split(', '))
37+
38+
machines.append([buttons, prize])
39+
40+
if aoc.args.verbose:
41+
print(f"buttons: {buttons}, prize: {prize}")
42+
43+
print(f"p1: {sum(solve(machine) for machine in machines)}")
44+
print(f"p2: {sum(solve(machine, offset=10000000000000) for machine in machines)}")
45+
46+
47+
if __name__ == "__main__":
48+
main()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 185/476 ⭐
6+
Advent of Code - 187/476 ⭐
77
</h1>
88
<h1 align="center">
9-
2024 - 24 ⭐ - Python
9+
2024 - 26 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -44,6 +44,9 @@ My solutions to the yearly Advents of Code
4444
<a href="2024/12/script.py">
4545
<img src=".aoc_tiles/tiles/2024/12.png" width="161px">
4646
</a>
47+
<a href="2024/13/script.py">
48+
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
49+
</a>
4750
<h1 align="center">
4851
2023 - 47 ⭐ - Python
4952
</h1>

0 commit comments

Comments
 (0)