-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c86edc
commit 4528397
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters