Skip to content

Commit c0f5343

Browse files
day07 for AoC 2024 (part 2)
Took 36 minutes
1 parent b641445 commit c0f5343

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/main/java/aminetti/adventofcode2024/day07/Day07.java

+87-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ public long solvePart1() {
4646
}
4747

4848
public enum Op {
49-
SUM, MULT;
49+
SUM, MULT, CONCAT;
5050

5151
public String print() {
52-
return this == SUM ? "+" : "*";
52+
return switch (this) {
53+
case SUM -> "+";
54+
case MULT -> "*";
55+
case CONCAT -> "||";
56+
};
5357
}
5458
}
5559

@@ -102,7 +106,87 @@ private long countOfCorrectOperators(long result, List<Long> longs, List<Op> ope
102106
}
103107

104108
public long solvePart2() {
109+
long sum = 0;
110+
for (int i = 0; i < results.size(); i++) {
111+
Long result = results.get(i);
105112

106-
return 0;
113+
LOGGER.debug("Checking {}", result);
114+
long count = countOfCorrectOperatorsWithConcat(result, components.get(i), List.of(), newArrayList());
115+
LOGGER.debug("count for {}: {}", result, count);
116+
if (count > 0) {
117+
sum += result;
118+
}
119+
}
120+
return sum;
107121
}
122+
123+
124+
private long countOfCorrectOperatorsWithConcat(long result, List<Long> longs, List<Op> operators, List<Long> components) {
125+
if (longs.size() == 1) {
126+
Long lastEl = Iterables.getOnlyElement(longs);
127+
if (result == lastEl) {
128+
129+
String fullOperation = "(".repeat(operators.size()) + lastEl;
130+
131+
ArrayList<Op> mutableOps = newArrayList(operators);
132+
ArrayList<Long> mutableComponents = newArrayList(components);
133+
while (!mutableOps.isEmpty()) {
134+
fullOperation += mutableOps.removeLast().print() + "" + mutableComponents.removeLast() + ")";
135+
}
136+
137+
LOGGER.debug("Operation: {}", fullOperation);
138+
LOGGER.info("Found correct operators");
139+
return 1;
140+
}
141+
142+
return 0;
143+
144+
}
145+
146+
if (result <= 0 || longs.isEmpty()) {
147+
return 0;
148+
}
149+
150+
151+
ArrayList<Op> opsForSum = newArrayList(operators);
152+
opsForSum.add(Op.SUM);
153+
154+
155+
156+
ArrayList<Long> next = newArrayList(longs);
157+
Long component = next.removeLast();
158+
159+
ArrayList<Long> nextComponents = newArrayList(components);
160+
nextComponents.add(component);
161+
162+
long sumConcat = 0;
163+
String stringResult = "" + result;
164+
String stringComponent = "" + component;
165+
166+
LOGGER.info("Result: {} - longs: {}", result, longs);
167+
LOGGER.info("Result: {} - stringComponent: {}", stringResult, stringComponent);
168+
169+
if (stringResult.length() > stringComponent.length() && stringResult.endsWith(stringComponent)) {
170+
LOGGER.info("Trailing matches: Result: {} - stringComponent: {}", stringResult, stringComponent);
171+
172+
ArrayList<Op> opsForConcat = newArrayList(operators);
173+
opsForConcat.add(Op.CONCAT);
174+
175+
long expectedResult = Long.parseLong(stringResult.substring(0, stringResult.length() - stringComponent.length()));
176+
LOGGER.info("expectedResult: {}", expectedResult);
177+
sumConcat += countOfCorrectOperatorsWithConcat(
178+
expectedResult,
179+
next, opsForConcat, nextComponents);
180+
}
181+
182+
long sumMult = 0;
183+
if (result % component == 0) {
184+
ArrayList<Op> opsForMult = newArrayList(operators);
185+
opsForMult.add(MULT);
186+
sumMult += countOfCorrectOperatorsWithConcat(result / component, next, opsForMult, nextComponents);
187+
}
188+
return sumMult + sumConcat
189+
+ countOfCorrectOperatorsWithConcat(result - component, next, opsForSum, nextComponents);
190+
}
191+
108192
}

src/test/java/aminetti/adventofcode2024/day07/Day07Test.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import static org.apache.commons.io.IOUtils.readLines;
1111
import static org.apache.commons.io.IOUtils.resourceToString;
1212
import static org.hamcrest.MatcherAssert.assertThat;
13-
import static org.hamcrest.Matchers.*;
13+
import static org.hamcrest.Matchers.is;
14+
import static org.hamcrest.Matchers.lessThan;
1415

1516
class Day07Test {
1617

@@ -54,7 +55,7 @@ void actualInputPart2() throws IOException {
5455
long l = solver.solvePart2();
5556

5657
// then
57-
assertThat(l, is(0L));
58+
assertThat(l, is(286580387663654L));
5859
}
5960

6061
@Test
@@ -68,7 +69,7 @@ void testInputPart2() throws IOException {
6869
long l = solver.solvePart2();
6970

7071
// then
71-
assertThat(l, is(0L));
72+
assertThat(l, is(11387L));
7273
}
7374

7475
public static boolean inputExists() {

0 commit comments

Comments
 (0)