Skip to content

Commit 833f883

Browse files
committed
feat: solve day 2
1 parent cfb5b19 commit 833f883

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 2:
1+
# Day 2: Red-Nosed Reports
22

33
[https://adventofcode.com/2024/day/2](https://adventofcode.com/2024/day/2)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.adventofcode.flashk.day02;
2+
3+
import java.util.List;
4+
5+
public class RedNosedReports {
6+
7+
private List<Report> reports;
8+
9+
public RedNosedReports(List<String> inputs) {
10+
reports = inputs.stream().map(Report::new).toList();
11+
}
12+
13+
public int solveA() {
14+
int result = 0;
15+
for(Report report : reports) {
16+
if(report.isSafe()) {
17+
result++;
18+
}
19+
}
20+
21+
return result;
22+
}
23+
24+
public int solveB() {
25+
int result = 0;
26+
for(Report report : reports) {
27+
if(report.isSafeDampener()) {
28+
result++;
29+
}
30+
}
31+
32+
return result;
33+
}
34+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.adventofcode.flashk.day02;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
@Getter
14+
@Setter
15+
public class Report {
16+
17+
private List<Integer> levels;
18+
19+
public Report(String input) {
20+
// Warning: do not use .toList() as it would create an immutable list
21+
levels = Arrays.stream(input.split(StringUtils.SPACE)).map(Integer::valueOf).collect(Collectors.toList());
22+
}
23+
24+
public boolean isSafe() {
25+
boolean expectedIncreasing = isIncreasing();
26+
boolean isSafe = true;
27+
28+
int i = 1;
29+
int lastLevel = levels.get(0);
30+
31+
while(isSafe && i < levels.size()) {
32+
int currentLevel = levels.get(i);
33+
int distance = currentLevel-lastLevel;
34+
35+
if(distance == 0) {
36+
isSafe = false;
37+
} else if(expectedIncreasing) {
38+
if(distance > 3 || distance < 0) {
39+
isSafe = false;
40+
}
41+
} else {
42+
if(distance < -3 || distance > 0) {
43+
isSafe = false;
44+
}
45+
}
46+
47+
lastLevel = currentLevel;
48+
i++;
49+
}
50+
51+
return isSafe;
52+
}
53+
54+
public boolean isSafeDampener() {
55+
if(isSafe()) {
56+
return true;
57+
}
58+
59+
List<Integer> copyLevels = new ArrayList<>(levels);
60+
61+
boolean isSafe = false;
62+
int i = 0;
63+
64+
while(!isSafe && i < levels.size()) {
65+
66+
levels.remove(i);
67+
isSafe = isSafe();
68+
69+
if(!isSafe()) {
70+
levels = new ArrayList<>(copyLevels);
71+
i++;
72+
}
73+
}
74+
75+
return isSafe;
76+
}
77+
/**
78+
* true si creciente
79+
* false si decreciente;
80+
* @return
81+
*/
82+
private boolean isIncreasing() {
83+
int i = 0;
84+
boolean found = false;
85+
int distance = 0;
86+
while(!found && i < levels.size()-1) {
87+
distance = levels.get(i+1)-levels.get(i);
88+
if(distance != 0) {
89+
found = true;
90+
}
91+
i++;
92+
}
93+
94+
return distance > 0;
95+
}
96+
}

src/test/java/com/adventofcode/flashk/day02/Day02Test.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import com.adventofcode.flashk.common.test.utils.Timer;
2020
import com.adventofcode.flashk.common.test.utils.Input;
2121

22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
2224
@DisplayName(TestDisplayName.DAY_02)
2325
@TestMethodOrder(OrderAnnotation.class)
24-
@Disabled // TODO Remove comment when implemented
2526
public class Day02Test extends PuzzleTest {
2627

2728
private final static String INPUT_FOLDER = TestFolder.DAY_02;
@@ -43,7 +44,9 @@ public void testSolvePart1Sample() {
4344

4445
// Read input file
4546
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
46-
47+
48+
RedNosedReports reports = new RedNosedReports(inputs);
49+
assertEquals(2,reports.solveA());
4750
}
4851

4952
@Test
@@ -57,8 +60,11 @@ public void testSolvePart1Input() {
5760

5861
// Read input file
5962
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
60-
63+
RedNosedReports reports = new RedNosedReports(inputs);
64+
65+
assertEquals(257,reports.solveA());
6166
}
67+
6268

6369
@Test
6470
@Order(3)
@@ -71,7 +77,9 @@ public void testSolvePart2Sample() {
7177

7278
// Read input file
7379
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
74-
80+
RedNosedReports reports = new RedNosedReports(inputs);
81+
82+
assertEquals(4, reports.solveB());
7583
}
7684

7785
@Test
@@ -85,6 +93,9 @@ public void testSolvePart2Input() {
8593

8694
// Read input file
8795
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
96+
RedNosedReports reports = new RedNosedReports(inputs);
97+
98+
assertEquals(328, reports.solveB());
8899

89100
}
90101

src/test/resources/inputs

0 commit comments

Comments
 (0)