Skip to content

Commit

Permalink
[2024/13] p1 & p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 13, 2024
1 parent 5c86edc commit 4528397
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions 2024/13/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
48 changes: 48 additions & 0 deletions 2024/13/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from GhostyUtils import aoc
from GhostyUtils.vec2 import Vec2


def solve(machine: list[list[tuple], tuple], offset: int = 0):
a, b = [Vec2(button) for button in machine[0]]
p = Vec2(machine[1]) + Vec2(offset, offset)

if aoc.args.progress or aoc.args.verbose:
print(f"A vec: {a}, B vec: {b}, Prize location: {p}")

det = (a.x * b.y - a.y * b.x)

a_presses = int((p.x * b.y - p.y * b.x) / det)
b_presses = int((p.y * a.x - p.x * a.y) / det)

if a * a_presses + b * b_presses == p:
if aoc.args.progress or aoc.args.verbose:
print(f"> A: {a_presses}x, B: {b_presses}x, Tokens: {a_presses * 3 + b_presses}")
return a_presses * 3 + b_presses
else:
if aoc.args.progress or aoc.args.verbose:
print("> Misses")
return 0


def main():
inputs = aoc.read_sections()

machines = []
for mch in inputs:
mch = mch.splitlines()
buttons = [tuple(int(coord[2:])
for coord in btn.split(': ')[1].split(', '))
for btn in mch[:2]]
prize = tuple(int(coord[2:]) for coord in mch[2].split(': ')[1].split(', '))

machines.append([buttons, prize])

if aoc.args.verbose:
print(f"buttons: {buttons}, prize: {prize}")

print(f"p1: {sum(solve(machine) for machine in machines)}")
print(f"p2: {sum(solve(machine, offset=10000000000000) for machine in machines)}")


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 185/476 ⭐
Advent of Code - 187/476 ⭐
</h1>
<h1 align="center">
2024 - 24 ⭐ - Python
2024 - 26 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -44,6 +44,9 @@ My solutions to the yearly Advents of Code
<a href="2024/12/script.py">
<img src=".aoc_tiles/tiles/2024/12.png" width="161px">
</a>
<a href="2024/13/script.py">
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit 4528397

Please sign in to comment.