Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2c0bdaf

Browse files
day01 for AoC 2023 (part 2)
Took 10 minutes
1 parent 488b972 commit 2c0bdaf

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

‎src/main/java/aminetti/adventofcode2024/day01/Day01.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7-
import java.math.BigInteger;
8-
import java.sql.Array;
97
import java.util.ArrayList;
8+
import java.util.HashMap;
109
import java.util.List;
10+
import java.util.Map;
1111
import java.util.regex.Matcher;
1212
import java.util.regex.Pattern;
1313

@@ -33,7 +33,7 @@ public long solvePart1() {
3333
if (matcher.matches()) {
3434
Integer a = Integer.valueOf(matcher.group(1));
3535
Integer b = Integer.valueOf(matcher.group(2));
36-
LOGGER.info("Reading: {} and {}", a ,b);
36+
LOGGER.info("Reading: {} and {}", a, b);
3737
left.add(a);
3838
right.add(b);
3939
} else {
@@ -42,11 +42,31 @@ public long solvePart1() {
4242
}
4343

4444
return Streams.zip(left.stream().sorted(), right.stream().sorted(),
45-
(a, b) -> Math.abs(a-b)).reduce(0, (a, b) -> a + b);
45+
(a, b) -> Math.abs(a - b)).reduce(0, Integer::sum);
4646
}
4747

4848
public long solvePart2() {
49+
Pattern p = Pattern.compile("(\\d+)\\s+(\\d+)");
50+
51+
Map<Long, Long> left = new HashMap<>(input.size());
52+
Map<Long, Long> right = new HashMap<>(input.size());
53+
54+
for (String s : input) {
55+
Matcher matcher = p.matcher(s);
56+
if (matcher.matches()) {
57+
Long a = Long.valueOf(matcher.group(1));
58+
Long b = Long.valueOf(matcher.group(2));
59+
LOGGER.info("Reading: {} and {}", a, b);
60+
left.compute(a, (k, v) -> (v == null) ? 1 : v + 1);
61+
right.compute(b, (k, v) -> (v == null) ? 1 : v + 1);
62+
} else {
63+
throw new IllegalArgumentException("Invalid input: " + s);
64+
}
65+
66+
}
4967

50-
return 0;
68+
return left.keySet().stream().
69+
map((k) -> k * left.get(k) * right.getOrDefault(k, 0L))
70+
.reduce(0L, Long::sum);
5171
}
5272
}

‎src/test/java/aminetti/adventofcode2024/day01/Day01Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void actualInputPart1() throws IOException {
2424
long l = solver.solvePart1();
2525

2626
// then
27-
assertThat(l, is(110396520L));
27+
assertThat(l, is(1889772L));
2828
}
2929

3030
@Test
@@ -52,7 +52,7 @@ void actualInputPart2() throws IOException {
5252
long l = solver.solvePart2();
5353

5454
// then
55-
assertThat(l, is(11L));
55+
assertThat(l, is(23228917L));
5656
}
5757

5858
@Test
@@ -66,7 +66,7 @@ void testInputPart2() throws IOException {
6666
long l = solver.solvePart2();
6767

6868
// then
69-
assertThat(l, is(0L));
69+
assertThat(l, is(31L));
7070
}
7171

7272

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /