Skip to content

Commit f01b123

Browse files
committedDec 2, 2024
2024 Day 2 part 2
1 parent 8ed78bb commit f01b123

File tree

2 files changed

+113
-85
lines changed

2 files changed

+113
-85
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,72 @@
11
package net.leibi.adventofcode2024.day2;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
5+
import java.util.HashSet;
46
import java.util.List;
7+
import java.util.Set;
58

69
public class Day2 {
710

8-
public static final int MIN_SAFE = 1;
9-
public static final int MAX_SAFE = 3;
11+
public static final int MIN_SAFE = 1;
12+
public static final int MAX_SAFE = 3;
1013

11-
public long part1(String input) {
12-
final var list = input.lines().map(s -> s.split("\\s+")).toList();
14+
public long part1(String input, boolean dampener) {
15+
final var list = input.lines().map(s -> s.split("\\s+")).toList();
1316

14-
long cnt = 0;
15-
for (String[] strings : list) {
16-
var integerList = Arrays.stream(strings).map(Integer::parseInt).toList();
17-
if (isSafe(integerList)) {
18-
cnt++;
19-
}
17+
long cnt = 0;
18+
for (String[] strings : list) {
19+
var integerList = Arrays.stream(strings).map(Integer::parseInt).toList();
20+
if (isSafe(integerList, dampener)) {
21+
cnt++;
22+
}
23+
}
24+
return cnt;
2025
}
21-
return cnt;
22-
}
2326

24-
boolean isSafe(List<Integer> report) {
27+
boolean isSafe(List<Integer> report, boolean dampener) {
2528

26-
Direction direction = getDirection(getDiff(report, 1));
27-
boolean returnValue = true;
28-
for (var i = 1; i < report.size(); i++) {
29-
var diff = getDiff(report, i);
30-
if (isNotSafeDistance(diff)) return false;
29+
Direction direction = getDirection(getDiff(report, 1));
30+
final var failedIndices = getFailedIndices(report, direction);
31+
if (dampener && !failedIndices.isEmpty()) {
32+
for (var i = 0; i < report.size(); i++) {
33+
var clone = new ArrayList<>(report);
34+
clone.remove(i);
35+
if (isSafe(clone, false)) return true;
36+
}
37+
}
38+
return failedIndices.isEmpty();
39+
}
40+
41+
private static Set<Integer> getFailedIndices(List<Integer> report, Direction direction) {
42+
Set<Integer> failedIndices = new HashSet<>();
43+
for (var i = 1; i < report.size(); i++) {
44+
var diff = getDiff(report, i);
45+
if (isNotSafeDistance(diff)) {
46+
failedIndices.add(i - 1);
47+
}
48+
49+
Direction localDir = getDirection(diff);
50+
if (localDir != direction) {
51+
failedIndices.add(i - 1);
52+
}
53+
}
54+
return failedIndices;
55+
}
56+
57+
private static int getDiff(List<Integer> report, int i) {
58+
return report.get(i) - report.get(i - 1);
59+
}
60+
61+
private static Direction getDirection(int diff) {
62+
return diff > 0 ? Direction.INC : Direction.DEC;
63+
}
64+
65+
private static boolean isNotSafeDistance(int diff) {
66+
return Math.abs(diff) > MAX_SAFE || Math.abs(diff) < MIN_SAFE;
67+
}
3168

32-
Direction localDir = getDirection(diff);
33-
if (localDir != direction) return false;
69+
enum Direction {
70+
INC, DEC, BOTH
3471
}
35-
return returnValue;
36-
}
37-
38-
private static int getDiff(List<Integer> report, int i) {
39-
return report.get(i) - report.get(i - 1);
40-
}
41-
42-
private static Direction getDirection(int diff) {
43-
return diff > 0 ? Direction.INC : Direction.DEC;
44-
}
45-
46-
private static boolean isNotSafeDistance(int diff) {
47-
return Math.abs(diff) > MAX_SAFE || Math.abs(diff) < MIN_SAFE;
48-
}
49-
50-
enum Direction {
51-
INC,
52-
DEC,
53-
BOTH
54-
}
5572
}
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,64 @@
11
package net.leibi.adventofcode2024.day2;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
3+
import org.junit.jupiter.api.Test;
44

55
import java.util.List;
6-
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
78

89
class Day2Test {
910

10-
private final Day2 day2 = new Day2();
11-
12-
@Test
13-
void part1Small() {
14-
assertThat((day2.part1(Input.SMALL))).isEqualTo(2L);
15-
}
16-
17-
@Test
18-
void part1Big() {
19-
assertThat((day2.part1(Input.BIG))).isEqualTo(624L);
20-
}
21-
22-
@Test
23-
void part2Small() {
24-
assertThat((day2.part1(Input.SMALL))).isEqualTo(4L);
25-
}
26-
27-
@Test
28-
void part2Big() {
29-
assertThat((day2.part1(Input.BIG))).isEqualTo(624L);
30-
}
31-
32-
@Test
33-
void isSafe() {
34-
35-
assertThat(day2.isSafe(List.of(7, 6, 4, 2, 1), false)).isTrue();
36-
assertThat(day2.isSafe(List.of(1, 2, 7, 8, 9), false)).isFalse();
37-
assertThat(day2.isSafe(List.of(9, 7, 6, 2, 1), false)).isFalse();
38-
assertThat(day2.isSafe(List.of(1, 3, 2, 4, 5), false)).isFalse();
39-
assertThat(day2.isSafe(List.of(8, 6, 4, 4, 1), false)).isFalse();
40-
assertThat(day2.isSafe(List.of(1, 3, 6, 7, 9), false)).isTrue();
41-
}
42-
43-
@Test
44-
void isSafeWithDampener() {
45-
46-
assertThat(day2.isSafe(List.of(7, 6, 4, 2, 1), true)).isTrue();
47-
assertThat(day2.isSafe(List.of(1, 2, 7, 8, 9), true)).isFalse();
48-
assertThat(day2.isSafe(List.of(9, 7, 6, 2, 1), true)).isFalse();
49-
assertThat(day2.isSafe(List.of(1, 3, 2, 4, 5), true)).isTrue();
50-
assertThat(day2.isSafe(List.of(8, 6, 4, 4, 1), true)).isTrue();
51-
assertThat(day2.isSafe(List.of(1, 3, 6, 7, 9), true)).isTrue();
52-
}
11+
private final Day2 day2 = new Day2();
12+
13+
@Test
14+
void part1Small() {
15+
assertThat((day2.part1(Input.SMALL, false))).isEqualTo(2L);
16+
}
17+
18+
@Test
19+
void part1Big() {
20+
assertThat((day2.part1(Input.BIG, false))).isEqualTo(624L);
21+
}
22+
23+
@Test
24+
void part2Small() {
25+
assertThat((day2.part1(Input.SMALL, true))).isEqualTo(4L);
26+
}
27+
28+
@Test
29+
void part2Big() {
30+
assertThat((day2.part1(Input.BIG, true))).isEqualTo(658L);
31+
}
32+
33+
@Test
34+
void isSafe() {
35+
36+
assertThat(day2.isSafe(List.of(7, 6, 4, 2, 1), false)).isTrue();
37+
assertThat(day2.isSafe(List.of(1, 2, 7, 8, 9), false)).isFalse();
38+
assertThat(day2.isSafe(List.of(9, 7, 6, 2, 1), false)).isFalse();
39+
assertThat(day2.isSafe(List.of(1, 3, 2, 4, 5), false)).isFalse();
40+
assertThat(day2.isSafe(List.of(8, 6, 4, 4, 1), false)).isFalse();
41+
assertThat(day2.isSafe(List.of(1, 3, 6, 7, 9), false)).isTrue();
42+
}
43+
44+
@Test
45+
void isSafeWithDampener() {
46+
47+
assertThat(day2.isSafe(List.of(7, 6, 4, 2, 1), true)).isTrue();
48+
assertThat(day2.isSafe(List.of(1, 2, 7, 8, 9), true)).isFalse();
49+
assertThat(day2.isSafe(List.of(9, 7, 6, 2, 1), true)).isFalse();
50+
assertThat(day2.isSafe(List.of(1, 3, 2, 4, 5), true)).isTrue();
51+
assertThat(day2.isSafe(List.of(8, 6, 4, 4, 1), true)).isTrue();
52+
assertThat(day2.isSafe(List.of(1, 3, 6, 7, 9), true)).isTrue();
53+
}
54+
55+
@Test
56+
void isSafeWithDampenerExample() {
57+
assertThat(day2.isSafe(List.of(1, 3, 2, 4, 5), true)).isTrue();
58+
}
59+
60+
@Test
61+
void isSafeWithDampenerExample2() {
62+
assertThat(day2.isSafe(List.of(8, 6, 4, 4, 1), true)).isTrue();
63+
}
5364
}

0 commit comments

Comments
 (0)