Skip to content

Commit

Permalink
aoc 2024 day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 13, 2024
1 parent ebe13a2 commit 932c31e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 18 deletions.
6 changes: 3 additions & 3 deletions adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@
[20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java
[20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java

# 2024 (24/49)
# 2024 (26/49)

| # | Name | Solution | Test |
|---------:|---------------------------------------------------|:------------------------------------:|:---------------------------------:|
Expand Down Expand Up @@ -1458,8 +1458,8 @@
| 20241102 | [Day 11: Plutonian Pebbles - Part 2][20241102] | ✅[💾][20241102solution] | ✅[💾][20241102tests] |
| 20241201 | [Day 12: Garden Groups][20241201] | ✅[💾][20241201solution] | ✅[💾][20241201tests] |
| 20241202 | [Day 12: Garden Groups - Part 2][20241202] | ✅[💾][20241202solution] | ✅[💾][20241202tests] |
| 20241301 | [Day 13: ?][20241301] | [💾][20241301solution] | [💾][20241301tests] |
| 20241302 | [Day 13: ? - Part 2][20241302] | [💾][20241302solution] | [💾][20241302tests] |
| 20241301 | [Day 13: Claw Contraption][20241301] | ✅[💾][20241301solution] | ✅[💾][20241301tests] |
| 20241302 | [Day 13: Claw Contraption - Part 2][20241302] | ✅[💾][20241302solution] | ✅[💾][20241302tests] |
| 20241401 | [Day 14: ?][20241401] | [💾][20241401solution] | [💾][20241401tests] |
| 20241402 | [Day 14: ? - Part 2][20241402] | [💾][20241402solution] | [💾][20241402tests] |
| 20241501 | [Day 15: ?][20241501] | [💾][20241501solution] | [💾][20241501tests] |
Expand Down
86 changes: 75 additions & 11 deletions adventofcode/src/main/java/org/ck/adventofcode/year2024/Day13.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,98 @@
package org.ck.adventofcode.year2024;

import java.util.Scanner;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20241301,
name = "Day 13: ?",
name = "Day 13: Claw Contraption",
url = "https://adventofcode.com/2024/day/13",
category = "2024",
solved = false)
category = "2024")
@Solution(
id = 20241302,
name = "Day 13: ? - Part 2",
name = "Day 13: Claw Contraption - Part 2",
url = "https://adventofcode.com/2024/day/13#part2",
category = "2024",
solved = false)
category = "2024")
public class Day13 extends AOCSolution {
private static final Pattern BUTTON_PATTERN =
Pattern.compile("Button [AB]: X\\+(?<xAmount>\\d+), Y\\+(?<yAmount>\\d+)");
private static final Pattern PRICE_PATTERN =
Pattern.compile("Prize: X=(?<xPosition>\\d+), Y=(?<yPosition>\\d+)");

@Override
protected void runPartOne(final Scanner in) {
run(in);
run(in, 0);
}

@Override
protected void runPartTwo(final Scanner in) {
run(in);
run(in, 10000000000000L);
}

private void run(final Scanner in) {
print("Whoopsie");
private void run(final Scanner in, final long coordinateOffset) {
List<Machine> machines = new ArrayList<>();

while (in.hasNextLine()) {
final Button aButton = getButton(in.nextLine(), 3);
final Button bButton = getButton(in.nextLine(), 1);
final Coordinate priceLocation = getPriceLocation(in.nextLine(), coordinateOffset);
if (in.hasNextLine()) {
in.nextLine();
}

machines.add(new Machine(aButton, bButton, priceLocation));
}

print(machines.stream().mapToLong(this::getMinCoins2).sum());
}

private long getMinCoins2(Machine machine) {
final long pX = machine.priceLocation().x();
final long pY = machine.priceLocation().y();
final long aX = machine.a().movementAmount().x();
final long aY = machine.a().movementAmount().y();
final long bX = machine.b().movementAmount().x();
final long bY = machine.b().movementAmount().y();

final long a = (pX * bY - bX * pY) / (aX * bY - bX * aY);
final long b = (pX - a * aX) / bX;

if (a * aX + b * bX == pX && a * aY + b * bY == pY) {
return a * machine.a().coins() + b * machine.b().coins();
}

return 0;
}

private Coordinate getPriceLocation(final String line, final long coordinateOffset) {
final Matcher matcher = PRICE_PATTERN.matcher(line);
if (matcher.find()) {
return new Coordinate(
Long.parseLong(matcher.group("xPosition")) + coordinateOffset,
Long.parseLong(matcher.group("yPosition")) + coordinateOffset);
}

throw new IllegalArgumentException("Invalid price location: " + line);
}

private Button getButton(final String line, final long coins) {
final Matcher matcher = BUTTON_PATTERN.matcher(line);
if (matcher.find()) {
return new Button(
new Coordinate(
Long.parseLong(matcher.group("xAmount")), Long.parseLong(matcher.group("yAmount"))),
coins);
}

throw new IllegalArgumentException("Invalid button: " + line);
}

private record Coordinate(long x, long y) {}

private record Button(Coordinate movementAmount, long coins) {}

private record Machine(Button a, Button b, Coordinate priceLocation) {}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package org.ck.adventofcode.year2024;

import org.ck.adventofcode.util.BaseAOCTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Disabled
class Day13Test extends BaseAOCTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a"})
@ValueSource(strings = {"01a"})
void testOne(String name) throws Exception {
runTest(new Day13()::partOne, "day13/%s".formatted(name));
}

@Test
void testOne() throws Exception {
runEncryptedTest(new Day13()::partOne, "day13/01");
}

@ParameterizedTest
@ValueSource(strings = {"02", "02a"})
@ValueSource(strings = {"02a"})
void testTwo(String name) throws Exception {
runTest(new Day13()::partTwo, "day13/%s".formatted(name));
}

@Test
void testTwo() throws Exception {
runEncryptedTest(new Day13()::partTwo, "day13/02");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
37686

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
480
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
77204516023437

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
875318608908
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

0 comments on commit 932c31e

Please sign in to comment.