Skip to content

Commit 7fcaf6d

Browse files
committed
AoC 2024 Day 2 - java
1 parent 1efbdd8 commit 7fcaf6d

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | | | | | | | | | | | | | | | | | | | | | | |
12-
| java | [](src/main/java/AoC2024_01.java) | | [](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | |
12+
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | |
1313
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | | | | | | | | | | | | | | | | | | | | | | |
1414
<!-- @END:ImplementationsTable:2024@ -->
1515

src/main/java/AoC2024_02.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import static com.github.pareronia.aoc.IntegerSequence.Range.range;
2+
import static com.github.pareronia.aoc.IterTools.windows;
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import com.github.pareronia.aoc.Utils;
9+
import com.github.pareronia.aoc.solution.Sample;
10+
import com.github.pareronia.aoc.solution.Samples;
11+
import com.github.pareronia.aoc.solution.SolutionBase;
12+
13+
public final class AoC2024_02
14+
extends SolutionBase<List<List<Integer>>, Integer, Integer> {
15+
16+
private AoC2024_02(final boolean debug) {
17+
super(debug);
18+
}
19+
20+
public static AoC2024_02 create() {
21+
return new AoC2024_02(false);
22+
}
23+
24+
public static AoC2024_02 createDebug() {
25+
return new AoC2024_02(true);
26+
}
27+
28+
@Override
29+
protected List<List<Integer>> parseInput(final List<String> inputs) {
30+
return inputs.stream()
31+
.map(line -> Arrays.stream(line.split(" "))
32+
.map(Integer::parseInt)
33+
.toList())
34+
.toList();
35+
}
36+
37+
private boolean safe(final List<Integer> levels) {
38+
final List<Integer> diffs = Utils.stream(windows(levels))
39+
.map(w -> w.second() - w.first())
40+
.toList();
41+
return diffs.stream().allMatch(diff -> 1 <= diff && diff <= 3)
42+
|| diffs.stream().allMatch(diff -> -1 >= diff && diff >= -3);
43+
}
44+
45+
@Override
46+
public Integer solvePart1(final List<List<Integer>> reports) {
47+
return (int) reports.stream().filter(this::safe).count();
48+
}
49+
50+
@Override
51+
public Integer solvePart2(final List<List<Integer>> reports) {
52+
return (int) reports.stream()
53+
.filter(report ->
54+
range(report.size()).intStream()
55+
.mapToObj(i -> {
56+
final List<Integer> tmp = new ArrayList<>(report);
57+
tmp.remove(i);
58+
return tmp;
59+
})
60+
.anyMatch(this::safe)
61+
).count();
62+
}
63+
64+
@Override
65+
@Samples({
66+
@Sample(method = "part1", input = TEST, expected = "2"),
67+
@Sample(method = "part2", input = TEST, expected = "4"),
68+
})
69+
public void samples() {
70+
}
71+
72+
public static void main(final String[] args) throws Exception {
73+
AoC2024_02.create().run();
74+
}
75+
76+
private static final String TEST = """
77+
7 6 4 2 1
78+
1 2 7 8 9
79+
9 7 6 2 1
80+
1 3 2 4 5
81+
8 6 4 4 1
82+
1 3 6 7 9
83+
""";
84+
}

src/main/java/com/github/pareronia/aoc/IterTools.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
155155
return cycle(iterable.iterator());
156156
}
157157

158+
public static <T> Iterator<WindowPair<T>> windows(final List<T> list) {
159+
return new Iterator<>() {
160+
int i = 0;
161+
162+
@Override
163+
public boolean hasNext() {
164+
return i < list.size() - 1;
165+
}
166+
167+
@Override
168+
public WindowPair<T> next() {
169+
final WindowPair<T> next
170+
= new WindowPair<>(list.get(i), list.get(i + 1));
171+
i++;
172+
return next;
173+
}
174+
};
175+
}
176+
158177
private static final class Heap {
159178

160179
public static void accept(final int[] a, final Consumer<int[]> consumer) {
@@ -193,5 +212,7 @@ private static void swap(final int[] a, final int i, final int j) {
193212

194213
public record ZippedPair<T>(T first, T second) {}
195214

215+
public record WindowPair<T>(T first, T second) {}
216+
196217
public record Enumerated<T>(int index, T value) {}
197218
}

src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.junit.jupiter.api.Test;
1212

13+
import com.github.pareronia.aoc.IterTools.WindowPair;
1314
import com.github.pareronia.aoc.IterTools.ZippedPair;
1415

1516
public class IterToolsTestCase {
@@ -78,4 +79,14 @@ public void cycle() {
7879
assertThat(ccycle.next()).isEqualTo('c');
7980
}
8081
}
82+
83+
@Test
84+
public void windows() {
85+
final Iterator<WindowPair<Integer>> windows
86+
= IterTools.windows(List.of(1, 2, 3, 4));
87+
assertThat(windows.next()).isEqualTo(new WindowPair<>(1, 2));
88+
assertThat(windows.next()).isEqualTo(new WindowPair<>(2, 3));
89+
assertThat(windows.next()).isEqualTo(new WindowPair<>(3, 4));
90+
assertThat(windows.hasNext()).isFalse();
91+
}
8192
}

0 commit comments

Comments
 (0)