Skip to content

Commit

Permalink
AoC 2024 Day 2 - java
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 4, 2024
1 parent 1efbdd8 commit 7fcaf6d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| | 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 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | | | | | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | | [](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | | | | | | | | | | | | | | | | | | | | | | |
| 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) | | | | | | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

Expand Down
84 changes: 84 additions & 0 deletions src/main/java/AoC2024_02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import static com.github.pareronia.aoc.IntegerSequence.Range.range;
import static com.github.pareronia.aoc.IterTools.windows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.github.pareronia.aoc.Utils;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;

public final class AoC2024_02
extends SolutionBase<List<List<Integer>>, Integer, Integer> {

private AoC2024_02(final boolean debug) {
super(debug);
}

public static AoC2024_02 create() {
return new AoC2024_02(false);
}

public static AoC2024_02 createDebug() {
return new AoC2024_02(true);
}

@Override
protected List<List<Integer>> parseInput(final List<String> inputs) {
return inputs.stream()
.map(line -> Arrays.stream(line.split(" "))
.map(Integer::parseInt)
.toList())
.toList();
}

private boolean safe(final List<Integer> levels) {
final List<Integer> diffs = Utils.stream(windows(levels))
.map(w -> w.second() - w.first())
.toList();
return diffs.stream().allMatch(diff -> 1 <= diff && diff <= 3)
|| diffs.stream().allMatch(diff -> -1 >= diff && diff >= -3);
}

@Override
public Integer solvePart1(final List<List<Integer>> reports) {
return (int) reports.stream().filter(this::safe).count();
}

@Override
public Integer solvePart2(final List<List<Integer>> reports) {
return (int) reports.stream()
.filter(report ->
range(report.size()).intStream()
.mapToObj(i -> {
final List<Integer> tmp = new ArrayList<>(report);
tmp.remove(i);
return tmp;
})
.anyMatch(this::safe)
).count();
}

@Override
@Samples({
@Sample(method = "part1", input = TEST, expected = "2"),
@Sample(method = "part2", input = TEST, expected = "4"),
})
public void samples() {
}

public static void main(final String[] args) throws Exception {
AoC2024_02.create().run();
}

private static final String TEST = """
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
""";
}
21 changes: 21 additions & 0 deletions src/main/java/com/github/pareronia/aoc/IterTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
return cycle(iterable.iterator());
}

public static <T> Iterator<WindowPair<T>> windows(final List<T> list) {
return new Iterator<>() {
int i = 0;

@Override
public boolean hasNext() {
return i < list.size() - 1;
}

@Override
public WindowPair<T> next() {
final WindowPair<T> next
= new WindowPair<>(list.get(i), list.get(i + 1));
i++;
return next;
}
};
}

private static final class Heap {

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

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

public record WindowPair<T>(T first, T second) {}

public record Enumerated<T>(int index, T value) {}
}
11 changes: 11 additions & 0 deletions src/test/java/com/github/pareronia/aoc/IterToolsTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.junit.jupiter.api.Test;

import com.github.pareronia.aoc.IterTools.WindowPair;
import com.github.pareronia.aoc.IterTools.ZippedPair;

public class IterToolsTestCase {
Expand Down Expand Up @@ -78,4 +79,14 @@ public void cycle() {
assertThat(ccycle.next()).isEqualTo('c');
}
}

@Test
public void windows() {
final Iterator<WindowPair<Integer>> windows
= IterTools.windows(List.of(1, 2, 3, 4));
assertThat(windows.next()).isEqualTo(new WindowPair<>(1, 2));
assertThat(windows.next()).isEqualTo(new WindowPair<>(2, 3));
assertThat(windows.next()).isEqualTo(new WindowPair<>(3, 4));
assertThat(windows.hasNext()).isFalse();
}
}

0 comments on commit 7fcaf6d

Please sign in to comment.