#!/usr/bin/perl use v5.35.0; # Machine setup requires solving paired equations of the form # # AX * a + BX * b = PX # AY * a + BY * b = PY # # Solutions are # # a = (PX * BY - BX * PY) / (AX * BY - BX * AY) # b = (AX * PY - PX * AY) / (AX * BY - BX * AY) # # and are required to be integers because they represent numbers of # button presses, so they only exist when the numerators are multiples # of the (common) denominator. # # Solutions are fully determined so there is no need to search for # optimal A and B button press combinations per machine; there is no # "cheapest way" to win the prize, they're either winnable or not. my ($ax, $ay, $bx, $by, $px, $py, $cost); my $adjustment = 10000000000000; my $patience = 'inf'; while (<>) { print; ($ax, $ay) = ($1, $2) if (/Button A: X\+(\d+), Y\+(\d+)/); ($bx, $by) = ($1, $2) if (/Button B: X\+(\d+), Y\+(\d+)/); if (/Prize: X=(\d+), Y=(\d+)/) { ($px, $py) = ($1 + $adjustment, $2 + $adjustment); say "$ax $ay $bx $by $px $py"; my $denom = $ax * $by - $bx * $ay; my $num_a = $px * $by - $bx * $py; my $num_b = $ax * $py - $px * $ay; if ($num_a % $denom || $num_b % $denom) { say "Not winnable"; } else { my $a = $num_a / $denom; my $b = $num_b / $denom; say "Button A x $a, button B x $b"; if ($a > $patience || $b > $patience) { say "Too slow"; } else { my $tokens = 3 * $a + 1 * $b; say "Spent $tokens tokens"; $cost += $tokens; } } } } say "\nTotal spent: $cost"