|
| 1 | +package com.codefork.aoc2024.day13; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +public record Machine(Button a, Button b, Prize prize) { |
| 10 | + |
| 11 | + private static Pattern buttonPattern = Pattern.compile("Button ([AB]): X\\+(\\d+), Y\\+(\\d+)"); |
| 12 | + private static Pattern prizePattern = Pattern.compile("Prize: X=(\\d+), Y=(\\d+)"); |
| 13 | + |
| 14 | + public record Button(int moveX, int moveY) { |
| 15 | + |
| 16 | + } |
| 17 | + |
| 18 | + public record Prize(long x, long y) { |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + public record Solution(long countA, long countB) { |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + public Optional<Solution> findSolution() { |
| 27 | + // I wrote out on paper the two simultaneous equations, merged them into one using |
| 28 | + // substitution, and solved for the number of times button B is pressed (countB). |
| 29 | + // Once we have that, we can plug it into one of the original equations to get countA |
| 30 | + var numer = (a.moveY() * prize.x()) - (a.moveX() * prize.y()); |
| 31 | + var denom = (a.moveY() * b.moveX()) - (a.moveX() * b.moveY()); |
| 32 | + |
| 33 | + // test that countB and countA are whole numbers; we can't push a button a fraction of a time, |
| 34 | + // so reject those solutions |
| 35 | + if (numer % denom == 0) { |
| 36 | + var countB = numer / denom; |
| 37 | + var numerCountA = prize.x() - (b.moveX() * countB); |
| 38 | + if (numerCountA % a.moveX() == 0) { |
| 39 | + var countA = numerCountA / a.moveX(); |
| 40 | + return Optional.of(new Solution(countA, countB)); |
| 41 | + } |
| 42 | + } |
| 43 | + return Optional.empty(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * The nulls here are gross, but IntelliJ warns you're not supposed to use Optionals for parameters, |
| 48 | + * so I'm not sure how else to do this more safely. |
| 49 | + */ |
| 50 | + public static List<Machine> consume(Iterator<String> iter, long prizeAddition, List<Machine> machines, Button a, Button b) { |
| 51 | + if (iter.hasNext()) { |
| 52 | + var line = iter.next(); |
| 53 | + var buttonMatcher = buttonPattern.matcher(line); |
| 54 | + if (buttonMatcher.find()) { |
| 55 | + var button = buttonMatcher.group(1); |
| 56 | + var x = Integer.parseInt(buttonMatcher.group(2)); |
| 57 | + var y = Integer.parseInt(buttonMatcher.group(3)); |
| 58 | + if (button.equals("A")) { |
| 59 | + return consume(iter, prizeAddition, machines, new Button(x, y), b); |
| 60 | + } |
| 61 | + if (button.equals("B")) { |
| 62 | + return consume(iter, prizeAddition, machines, a, new Button(x, y)); |
| 63 | + } |
| 64 | + } |
| 65 | + var prizeMatcher = prizePattern.matcher(line); |
| 66 | + if (prizeMatcher.find()) { |
| 67 | + var x = Integer.parseInt(prizeMatcher.group(1)) + prizeAddition; |
| 68 | + var y = Integer.parseInt(prizeMatcher.group(2)) + prizeAddition; |
| 69 | + var prize = new Prize(x, y); |
| 70 | + // a and b should be present at this point; if they're not, something's wrong with the input |
| 71 | + machines.add(new Machine(a, b, prize)); |
| 72 | + return consume(iter, prizeAddition, machines, null, null); |
| 73 | + } |
| 74 | + consume(iter, prizeAddition, machines, a, b); |
| 75 | + } |
| 76 | + return machines; |
| 77 | + } |
| 78 | + |
| 79 | + public static List<Machine> consume(Iterator<String> iter, long prizeAddition) { |
| 80 | + return consume(iter, prizeAddition, new ArrayList<>(), null, null); |
| 81 | + } |
| 82 | +} |
0 commit comments