Skip to content

Commit b641445

Browse files
day07 for AoC 2024 (part 1)
Took 1 hour 28 minutes
1 parent f794093 commit b641445

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package aminetti.adventofcode2024.day07;
2+
3+
import com.google.common.collect.Iterables;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import static aminetti.adventofcode2024.day07.Day07.Op.MULT;
13+
import static com.google.common.collect.Lists.newArrayList;
14+
15+
public class Day07 {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(Day07.class);
17+
private List<String> input;
18+
private final List<Long> results = new ArrayList<>();
19+
private final List<List<Long>> components = new ArrayList<>();
20+
21+
public Day07() {
22+
}
23+
24+
public void parseInput(List<String> input) {
25+
this.input = input;
26+
27+
for (String s : input) {
28+
String[] split = s.split(":");
29+
results.add(Long.parseLong(split[0]));
30+
String[] split1 = split[1].split("\\s+");
31+
components.add(Arrays.stream(split1).filter(StringUtils::isNotBlank).map(Long::parseLong).toList());
32+
}
33+
}
34+
35+
public long solvePart1() {
36+
long sum = 0;
37+
for (int i = 0; i < results.size(); i++) {
38+
Long result = results.get(i);
39+
long count = countOfCorrectOperators(result, components.get(i), List.of(), newArrayList());
40+
LOGGER.debug("count for {}: {}", result, count);
41+
if (count > 0) {
42+
sum += result;
43+
}
44+
}
45+
return sum;
46+
}
47+
48+
public enum Op {
49+
SUM, MULT;
50+
51+
public String print() {
52+
return this == SUM ? "+" : "*";
53+
}
54+
}
55+
56+
57+
private long countOfCorrectOperators(long result, List<Long> longs, List<Op> operators, List<Long> components) {
58+
if (longs.size() == 1) {
59+
Long lastEl = Iterables.getOnlyElement(longs);
60+
if (result == lastEl) {
61+
62+
String fullOperation = "(".repeat(operators.size()) + lastEl;
63+
64+
ArrayList<Op> mutableOps = newArrayList(operators);
65+
ArrayList<Long> mutableComponents = newArrayList(components);
66+
while (!mutableOps.isEmpty()) {
67+
fullOperation += mutableOps.removeLast().print() + "" + mutableComponents.removeLast() + ")";
68+
}
69+
70+
LOGGER.debug("Operation: {}", fullOperation);
71+
LOGGER.info("Found correct operators");
72+
return 1;
73+
}
74+
75+
return 0;
76+
77+
}
78+
79+
if (result <= 0 || longs.isEmpty()) {
80+
return 0;
81+
}
82+
83+
84+
ArrayList<Op> opsForSum = newArrayList(operators);
85+
opsForSum.add(Op.SUM);
86+
87+
ArrayList<Op> opsForMult = newArrayList(operators);
88+
opsForMult.add(MULT);
89+
90+
ArrayList<Long> next = newArrayList(longs);
91+
Long component = next.removeLast();
92+
93+
ArrayList<Long> nextComponents = newArrayList(components);
94+
nextComponents.add(component);
95+
96+
long sumMult = 0;
97+
if (result % component == 0) {
98+
sumMult += countOfCorrectOperators(result / component, next, opsForMult, nextComponents);
99+
}
100+
return sumMult
101+
+ countOfCorrectOperators(result - component, next, opsForSum, nextComponents);
102+
}
103+
104+
public long solvePart2() {
105+
106+
return 0;
107+
}
108+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package aminetti.adventofcode2024.day07;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.condition.EnabledIf;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
import static java.nio.charset.StandardCharsets.UTF_8;
10+
import static org.apache.commons.io.IOUtils.readLines;
11+
import static org.apache.commons.io.IOUtils.resourceToString;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.*;
14+
15+
class Day07Test {
16+
17+
@Test @EnabledIf("inputExists")
18+
void actualInputPart1() throws IOException {
19+
// given
20+
List<String> input = readLines(resourceToString("/day07/day07_input.txt", UTF_8));
21+
22+
// when
23+
Day07 solver = new Day07();
24+
solver.parseInput(input);
25+
long l = solver.solvePart1();
26+
27+
// then
28+
assertThat(l, is(lessThan(15766644995031L)));
29+
assertThat(l, is(14711933466277L));
30+
}
31+
32+
@Test
33+
void testInputPart1() throws IOException {
34+
// given
35+
List<String> input = readLines(resourceToString("/day07/day07_input_test.txt", UTF_8));
36+
37+
// when
38+
Day07 solver = new Day07();
39+
solver.parseInput(input);
40+
long l = solver.solvePart1();
41+
42+
// then
43+
assertThat(l, is(3749L));
44+
}
45+
46+
@Test @EnabledIf("inputExists")
47+
void actualInputPart2() throws IOException {
48+
// given
49+
List<String> input = readLines(resourceToString("/day07/day07_input.txt", UTF_8));
50+
51+
// when
52+
Day07 solver = new Day07();
53+
solver.parseInput(input);
54+
long l = solver.solvePart2();
55+
56+
// then
57+
assertThat(l, is(0L));
58+
}
59+
60+
@Test
61+
void testInputPart2() throws IOException {
62+
// given
63+
List<String> input = readLines(resourceToString("/day07/day07_input_test.txt", UTF_8));
64+
65+
// when
66+
Day07 solver = new Day07();
67+
solver.parseInput(input);
68+
long l = solver.solvePart2();
69+
70+
// then
71+
assertThat(l, is(0L));
72+
}
73+
74+
public static boolean inputExists() {
75+
return Day07Test.class.getResource("/day07/day07_input.txt") != null;
76+
}
77+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
190: 10 19
2+
3267: 81 40 27
3+
83: 17 5
4+
156: 15 6
5+
7290: 6 8 6 15
6+
161011: 16 10 13
7+
192: 17 8 14
8+
21037: 9 7 18 13
9+
292: 11 6 16 20

0 commit comments

Comments
 (0)