-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
11 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
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
86 changes: 75 additions & 11 deletions
86
adventofcode/src/main/java/org/ck/adventofcode/year2024/Day13.java
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 |
---|---|---|
@@ -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) {} | ||
} |
17 changes: 13 additions & 4 deletions
17
adventofcode/src/test/java/org/ck/adventofcode/year2024/Day13Test.java
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 |
---|---|---|
@@ -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"); | ||
} | ||
} |
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 @@ | ||
37686 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2024/day13/01.txt
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
480 |
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 @@ | ||
77204516023437 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2024/day13/02.txt
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
875318608908 |
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 |