Skip to content

Commit

Permalink
aoc 2024 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 2, 2024
1 parent 156b685 commit d74b409
Show file tree
Hide file tree
Showing 11 changed files with 2,087 additions and 15 deletions.
6 changes: 3 additions & 3 deletions adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1422,14 +1422,14 @@
[20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java
[20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java

# 2024 (2/49)
# 2024 (4/49)

| # | Name | Solution | Test |
|---------:|------------------------------------------------|:------------------------------------:|:---------------------------------:|
| 20240101 | [Day 1: Historian Hysteria][20240101] | ✅[💾][20240101solution] | ✅[💾][20240101tests] |
| 20240102 | [Day 1: Historian Hysteria - Part 2][20240102] | ✅[💾][20240102solution] | ✅[💾][20240102tests] |
| 20240201 | [Day 2: ?][20240201] | [💾][20240201solution] | [💾][20240201tests] |
| 20240202 | [Day 2: ? - Part 2][20240202] | [💾][20240202solution] | [💾][20240202tests] |
| 20240201 | [Day 2: Red-Nosed Reports][20240201] | ✅[💾][20240201solution] | ✅[💾][20240201tests] |
| 20240202 | [Day 2: Red-Nosed Reports - Part 2][20240202] | ✅[💾][20240202solution] | ✅[💾][20240202tests] |
| 20240301 | [Day 3: ?][20240301] | [💾][20240301solution] | [💾][20240301tests] |
| 20240302 | [Day 3: ? - Part 2][20240302] | [💾][20240302solution] | [💾][20240302tests] |
| 20240401 | [Day 4: ?][20240401] | [💾][20240401solution] | [💾][20240401tests] |
Expand Down
78 changes: 68 additions & 10 deletions adventofcode/src/main/java/org/ck/adventofcode/year2024/Day02.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,92 @@
package org.ck.adventofcode.year2024;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20240201,
name = "Day 2: ?",
name = "Day 2: Red-Nosed Reports",
url = "https://adventofcode.com/2024/day/2",
category = "2024",
solved = false)
category = "2024")
@Solution(
id = 20240202,
name = "Day 2: ? - Part 2",
name = "Day 2: Red-Nosed Reports - Part 2",
url = "https://adventofcode.com/2024/day/2#part2",
category = "2024",
solved = false)
category = "2024")
public class Day02 extends AOCSolution {

@Override
protected void runPartOne(final Scanner in) {
run(in);
run(in, List::of);
}

@Override
protected void runPartTwo(final Scanner in) {
run(in);
run(
in,
list -> {
final List<List<Integer>> all = new ArrayList<>();
all.add(list);

for (int i = 0; i < list.size(); ++i) {
final List<Integer> tmp = new ArrayList<>(list);
tmp.remove(i);

all.add(tmp);
}

return all;
});
}

private void run(final Scanner in, Function<List<Integer>, List<List<Integer>>> foo) {
long safeCount = 0;

while (in.hasNextLine()) {
final List<Integer> line = readInput(in);

final List<List<Integer>> all = foo.apply(line);

for (List<Integer> t : all) {
final boolean ascending = t.get(0) - t.get(1) < 0;
boolean isSafe = true;

for (int i = 0; i < t.size() - 1; ++i) {
final int difference = t.get(i) - t.get(i + 1);

if (levelsAreValid(ascending, difference)) {
isSafe = false;
break;
}
}

if (isSafe) {
++safeCount;
break;
}
}
}

print(safeCount);
}

private void run(final Scanner in) {
print("Whoopsie");
private static boolean levelsAreValid(final boolean ascending, final int difference) {
return (ascending && difference > 0)
|| (!ascending && difference < 0)
|| !(Math.abs(difference) >= 1 && Math.abs(difference) <= 3);
}

private static List<Integer> readInput(final Scanner in) {
final List<Integer> line = new ArrayList<>();

for (String part : in.nextLine().split(" ")) {
line.add(Integer.parseInt(part));
}

return line;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.ck.adventofcode.year2024;

import org.ck.adventofcode.util.BaseAOCTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Disabled
class Day02Test extends BaseAOCTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
213
Loading

0 comments on commit d74b409

Please sign in to comment.