Skip to content

Commit ede5113

Browse files
committed
feat: solve day 13
1 parent e12fba6 commit ede5113

File tree

5 files changed

+140
-8
lines changed

5 files changed

+140
-8
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.adventofcode.flashk.day13;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ClawContraption {
7+
8+
private List<Machine> machines = new ArrayList<>();
9+
10+
public ClawContraption(List<String> inputs) {
11+
for(int i = 0; i < inputs.size(); i += 4) {
12+
machines.add(new Machine(inputs.get(i), inputs.get(i + 1), inputs.get(i + 2)));
13+
}
14+
}
15+
16+
public long solveA() {
17+
return machines.stream().map(Machine::optimize).filter(m -> m != -1).reduce(0L, Long::sum);
18+
}
19+
20+
public long solveB() {
21+
return machines.stream().map(Machine::optimizeB).filter(m -> m != -1).reduce(0L, Long::sum);
22+
}
23+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.adventofcode.flashk.day13;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public class Machine {
9+
10+
private static final Pattern BUTTONS_PATTERN = Pattern.compile("X\\+(\\d*), Y\\+(\\d*)");
11+
private static final Pattern PRIZE_PATTERN = Pattern.compile("X=(\\d*), Y=(\\d*)");
12+
13+
private static final int TOKENS_A = 3;
14+
private static final int TOKENS_B = 1;
15+
16+
private final Vector2 buttonA;
17+
private final Vector2 buttonB;
18+
private final Vector2 prizePosition;
19+
private final long prizePositionBX;
20+
private final long prizePositionBY;
21+
22+
public Machine(String buttonA, String buttonB, String prize){
23+
this.buttonA = initializeVector(buttonA);
24+
this.buttonB = initializeVector(buttonB);
25+
this.prizePosition = initializeVector(prize);
26+
this.prizePositionBX = prizePosition.getX() + 10000000000000L;
27+
this.prizePositionBY = prizePosition.getY() + 10000000000000L;
28+
29+
}
30+
31+
private Vector2 initializeVector(String entry) {
32+
if(entry.startsWith("Button")) {
33+
Matcher matcher = BUTTONS_PATTERN.matcher(entry);
34+
if(matcher.find()){
35+
int xPos = Integer.parseInt(matcher.group(1));
36+
int yPos = Integer.parseInt(matcher.group(2));
37+
return new Vector2(xPos, yPos);
38+
}
39+
} else if(entry.startsWith("Prize")) {
40+
Matcher matcher = PRIZE_PATTERN.matcher(entry);
41+
if(matcher.find()){
42+
int xPos = Integer.parseInt(matcher.group(1));
43+
int yPos = Integer.parseInt(matcher.group(2));
44+
return new Vector2(xPos, yPos);
45+
}
46+
}
47+
48+
throw new IllegalArgumentException("Invalid entry");
49+
}
50+
51+
public void testMovement() {
52+
Vector2 initialPosition = new Vector2(0, 0);
53+
Vector2 finalPositionA = new Vector2(80 * buttonA.getX(), 80 * buttonA.getY());
54+
Vector2 finalPositionB = new Vector2(40 * buttonB.getX(), 40 * buttonB.getY());
55+
56+
initialPosition.transform(finalPositionA);
57+
initialPosition.transform(finalPositionB);
58+
}
59+
60+
public long optimize() {
61+
62+
int minTokens = Integer.MAX_VALUE;
63+
64+
for(int buttonAPresses = 0; buttonAPresses <= 100; buttonAPresses++) {
65+
for(int buttonBPresses = 0; buttonBPresses <= 100; buttonBPresses++) {
66+
Vector2 initialPosition = new Vector2(0, 0);
67+
Vector2 finalPositionA = new Vector2(buttonAPresses * buttonA.getX(), buttonAPresses * buttonA.getY());
68+
Vector2 finalPositionB = new Vector2(buttonBPresses * buttonB.getX(), buttonBPresses * buttonB.getY());
69+
70+
initialPosition.transform(finalPositionA);
71+
initialPosition.transform(finalPositionB);
72+
73+
if(initialPosition.equals(prizePosition)) {
74+
int tokens = buttonAPresses * TOKENS_A + buttonBPresses * TOKENS_B;
75+
minTokens = Math.min(minTokens, tokens);
76+
}
77+
}
78+
}
79+
80+
return minTokens != Integer.MAX_VALUE ? minTokens : -1;
81+
}
82+
83+
public long optimizeB() {
84+
85+
// Ecuación lineal con dos incógnitas
86+
87+
long axpy = buttonA.getX() * prizePositionBY;
88+
long aypx = buttonA.getY() * prizePositionBX;
89+
long axby = (long) buttonA.getX() * buttonB.getY();
90+
long aybx = (long) buttonA.getY() * buttonB.getX();
91+
92+
long b = (axpy - aypx) / (axby - aybx);
93+
long a = (prizePositionBX - b * buttonB.getX()) / buttonA.getX();
94+
95+
long posX = a * buttonA.getX() + b * buttonB.getX();
96+
long posY = a * buttonA.getY() + b * buttonB.getY();
97+
98+
if(posX == prizePositionBX && posY == prizePositionBY) {
99+
return a * TOKENS_A + b * TOKENS_B;
100+
} else {
101+
return -1;
102+
}
103+
104+
}
105+
106+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 13:
1+
# Day 13: Claw Contraption
22

33
[https://adventofcode.com/2024/day/13](https://adventofcode.com/2024/day/13)

src/test/java/com/adventofcode/flashk/day13/Day13Test.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
@DisplayName(TestDisplayName.DAY_13)
2323
@TestMethodOrder(OrderAnnotation.class)
24-
@Disabled // TODO Remove comment when implemented
2524
public class Day13Test extends PuzzleTest {
2625

2726
private static final String INPUT_FOLDER = TestFolder.DAY_13;
@@ -36,7 +35,9 @@ public void testSolvePart1Sample() {
3635
// Read input file
3736
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
3837

39-
assertEquals(0L,0L);
38+
ClawContraption clawContraption = new ClawContraption(inputs);
39+
40+
assertEquals(480L,clawContraption.solveA());
4041
}
4142

4243
@Test
@@ -48,9 +49,9 @@ public void testSolvePart1Input() {
4849

4950
// Read input file
5051
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
52+
ClawContraption clawContraption = new ClawContraption(inputs);
5153

52-
System.out.println("Solution: ");
53-
assertEquals(0L,0L);
54+
assertEquals(29877L,clawContraption.solveA());
5455

5556
}
5657

@@ -63,7 +64,9 @@ public void testSolvePart2Sample() {
6364

6465
// Read input file
6566
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
67+
ClawContraption clawContraption = new ClawContraption(inputs);
6668

69+
// There is no example for this part
6770
assertEquals(0L,0L);
6871
}
6972

@@ -76,9 +79,9 @@ public void testSolvePart2Input() {
7679

7780
// Read input file
7881
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
82+
ClawContraption clawContraption = new ClawContraption(inputs);
7983

80-
System.out.println("Solution: ");
81-
assertEquals(0L,0L);
84+
assertEquals(99423413811305L,clawContraption.solveB());
8285

8386
}
8487

src/test/resources/inputs

0 commit comments

Comments
 (0)