Skip to content

Commit 50b3add

Browse files
day03 for AoC 2023 (part 1)
Took 7 minutes
1 parent 03d6547 commit 50b3add

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package aminetti.adventofcode2024.day03;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.List;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class Day03 {
11+
private static final Logger LOGGER = LoggerFactory.getLogger(Day03.class);
12+
private List<String> input;
13+
14+
public Day03() {
15+
}
16+
17+
public void parseInput(List<String> input) {
18+
this.input = input;
19+
20+
}
21+
22+
public long solvePart1() {
23+
Pattern p = Pattern.compile("mul\\((\\d{1,3}),(\\d{1,3})\\)");
24+
25+
long sum = 0L;
26+
for (String s : input) {
27+
Matcher matcher = p.matcher(s);
28+
29+
while (matcher.find()) {
30+
Long a = Long.valueOf(matcher.group(1));
31+
Long b = Long.valueOf(matcher.group(2));
32+
sum += a * b;
33+
34+
}
35+
}
36+
37+
return sum;
38+
}
39+
40+
public long solvePart2() {
41+
42+
return 0;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day03;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
import static java.nio.charset.StandardCharsets.UTF_8;
9+
import static org.apache.commons.io.IOUtils.readLines;
10+
import static org.apache.commons.io.IOUtils.resourceToString;
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.is;
13+
14+
class Day03Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day03/day03_input.txt", UTF_8));
20+
21+
// when
22+
Day03 solver = new Day03();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(179834255L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day03/day03_input_test.txt", UTF_8));
34+
35+
// when
36+
Day03 solver = new Day03();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(161L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day03/day03_input.txt", UTF_8));
48+
49+
// when
50+
Day03 solver = new Day03();
51+
solver.parseInput(input);
52+
long l = solver.solvePart2();
53+
54+
// then
55+
assertThat(l, is(0L));
56+
}
57+
58+
@Test
59+
void testInputPart2() throws IOException {
60+
// given
61+
List<String> input = readLines(resourceToString("/day03/day03_input_test.txt", UTF_8));
62+
63+
// when
64+
Day03 solver = new Day03();
65+
solver.parseInput(input);
66+
long l = solver.solvePart2();
67+
68+
// then
69+
assertThat(l, is(0L));
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)