Skip to content

Commit 488b972

Browse files
day01 for AoC 2023 (part 1)
Took 23 minutes
1 parent 5fb8ba6 commit 488b972

File tree

4 files changed

+1131
-0
lines changed

4 files changed

+1131
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package aminetti.adventofcode2024.day01;
2+
3+
import com.google.common.collect.Streams;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.math.BigInteger;
8+
import java.sql.Array;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
14+
public class Day01 {
15+
private static final Logger LOGGER = LoggerFactory.getLogger(Day01.class);
16+
private List<String> input;
17+
18+
public Day01() {
19+
}
20+
21+
public void parseInput(List<String> input) {
22+
this.input = input;
23+
}
24+
25+
public long solvePart1() {
26+
Pattern p = Pattern.compile("(\\d+)\\s+(\\d+)");
27+
28+
ArrayList<Integer> left = new ArrayList<>(input.size());
29+
ArrayList<Integer> right = new ArrayList<>(input.size());
30+
31+
for (String s : input) {
32+
Matcher matcher = p.matcher(s);
33+
if (matcher.matches()) {
34+
Integer a = Integer.valueOf(matcher.group(1));
35+
Integer b = Integer.valueOf(matcher.group(2));
36+
LOGGER.info("Reading: {} and {}", a ,b);
37+
left.add(a);
38+
right.add(b);
39+
} else {
40+
throw new IllegalArgumentException("Invalid input: " + s);
41+
}
42+
}
43+
44+
return Streams.zip(left.stream().sorted(), right.stream().sorted(),
45+
(a, b) -> Math.abs(a-b)).reduce(0, (a, b) -> a + b);
46+
}
47+
48+
public long solvePart2() {
49+
50+
return 0;
51+
}
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day01;
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 Day01Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day01/day01_input.txt", UTF_8));
20+
21+
// when
22+
Day01 solver = new Day01();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(110396520L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day01/day01_input_test.txt", UTF_8));
34+
35+
// when
36+
Day01 solver = new Day01();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(11L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day01/day01_input.txt", UTF_8));
48+
49+
// when
50+
Day01 solver = new Day01();
51+
solver.parseInput(input);
52+
long l = solver.solvePart2();
53+
54+
// then
55+
assertThat(l, is(11L));
56+
}
57+
58+
@Test
59+
void testInputPart2() throws IOException {
60+
// given
61+
List<String> input = readLines(resourceToString("/day01/day01_input_test.txt", UTF_8));
62+
63+
// when
64+
Day01 solver = new Day01();
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)