|
|
|
require "matrix"
|
|
|
|
|
|
|
|
input = DATA.read
|
|
|
|
|
|
|
|
machines = input.split("\n\n")
|
|
|
|
.map {|machine| machine.scan(/\d+/).map(&:to_i).each_slice(2).to_a }
|
|
|
|
|
|
|
|
def wins(machine)
|
|
|
|
*ab, c = machine
|
|
|
|
a, b = (Matrix.columns(ab).inverse * Matrix.column_vector(c)).to_a.flatten
|
|
|
|
if [a, b].all? { _1.denominator == 1 }
|
|
|
|
[a, b].map(&:to_i)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pp machines.map { wins(_1) }.compact.sum { 3 * _1 + _2 }
|
|
|
|
|
|
|
|
pp machines
|
|
|
|
.map {|a,b,c| [a,b,c.map { _1 + 10_000_000_000_000 }] }
|
|
|
|
.map { wins(_1) }.compact.sum { 3 * _1 + _2 }
|
|
|
|
|
|
|
|
__END__
|
|
|
|
Button A: X+94, Y+34
|
|
|
|
Button B: X+22, Y+67
|
|
|
|
Prize: X=10000000008400, Y=10000000005400
|
|
|
|
|
|
|
|
Button A: X+26, Y+66
|
|
|
|
Button B: X+67, Y+21
|
|
|
|
Prize: X=10000000012748, Y=10000000012176
|
|
|
|
|
|
|
|
Button A: X+17, Y+86
|
|
|
|
Button B: X+84, Y+37
|
|
|
|
Prize: X=10000000007870, Y=10000000006450
|
|
|
|
|
|
|
|
Button A: X+69, Y+23
|
|
|
|
Button B: X+27, Y+71
|
|
|
|
Prize: X=10000000018641, Y=10000000010279
|