Skip to content

Commit 37b205d

Browse files
day02 for AoC 2023 (part 1 & 2)
Took 1 hour 0 minutes
1 parent 0867cb6 commit 37b205d

File tree

4 files changed

+1200
-0
lines changed

4 files changed

+1200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package aminetti.adventofcode2024.day02;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
11+
public class Day02 {
12+
private static final Logger LOGGER = LoggerFactory.getLogger(Day02.class);
13+
private List<String> input;
14+
15+
public Day02() {
16+
}
17+
18+
public void parseInput(List<String> input) {
19+
this.input = input;
20+
}
21+
22+
public long solvePart1() {
23+
Pattern p = Pattern.compile("(\\d+)");
24+
25+
List<List<Long>> allLists = new ArrayList<>(input.size());
26+
27+
for (String s : input) {
28+
Matcher matcher = p.matcher(s);
29+
List<Long> list = new ArrayList<>();
30+
31+
while (matcher.find()) {
32+
Long a = Long.valueOf(matcher.group(1));
33+
LOGGER.debug("Reading: {}", a);
34+
list.add(a);
35+
}
36+
37+
allLists.add(list);
38+
}
39+
40+
return allLists.stream()
41+
.filter(this::isSafe)
42+
.count();
43+
44+
}
45+
46+
private boolean isSafe(List<Long> l) {
47+
48+
LOGGER.info("Checking {}", l);
49+
Long prev = null;
50+
Boolean ascending = null;
51+
for (Long i : l) {
52+
if (prev == null) {
53+
prev = i;
54+
continue;
55+
}
56+
57+
if (prev == i) {
58+
LOGGER.info(" has 2 same values {}", i);
59+
return false;
60+
}
61+
62+
if (ascending == null) {
63+
ascending = prev < i;
64+
LOGGER.info(" is {}", ascending ? "ascending" : "descending");
65+
} else {
66+
67+
if (ascending && prev >= i) {
68+
LOGGER.info(" started as ascending, but is not!");
69+
return false;
70+
}
71+
if (!ascending && prev <= i) {
72+
LOGGER.info(" started as descending, but is not!");
73+
return false;
74+
}
75+
}
76+
77+
if (Math.abs(prev - i) > 3 && Math.abs(prev - i) > 0) {
78+
LOGGER.info(" has too big gap between {} and {}", prev, i);
79+
return false;
80+
}
81+
82+
prev = i;
83+
}
84+
85+
LOGGER.info(" is safe");
86+
return true;
87+
}
88+
89+
public long solvePart2() {
90+
Pattern p = Pattern.compile("(\\d+)");
91+
92+
List<List<Long>> allLists = new ArrayList<>(input.size());
93+
94+
for (String s : input) {
95+
Matcher matcher = p.matcher(s);
96+
List<Long> list = new ArrayList<>();
97+
98+
while (matcher.find()) {
99+
Long a = Long.valueOf(matcher.group(1));
100+
LOGGER.debug("Reading: {}", a);
101+
list.add(a);
102+
}
103+
104+
allLists.add(list);
105+
}
106+
107+
return allLists.stream()
108+
.filter(this::isSafeWithoutOneLevel)
109+
.count();
110+
}
111+
112+
private boolean isSafeWithoutOneLevel(List<Long> l) {
113+
List<List<Long>> possibleCombinations = new ArrayList<>();
114+
for (int i = 0; i < l.size(); i++) {
115+
List<Long> l1 = new ArrayList<>(l);
116+
l1.remove(i);
117+
possibleCombinations.add(l1);
118+
}
119+
return possibleCombinations.stream().anyMatch(this::isSafe);
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day02;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
import static java.nio.charset.StandardCharsets.UTF_8;
9+
import static org.apache.commons.io.IOUtils.readLines;
10+
import static org.apache.commons.io.IOUtils.resourceToString;
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.is;
13+
14+
class Day02Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day02/day02_input.txt", UTF_8));
20+
21+
// when
22+
Day02 solver = new Day02();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(663L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day02/day02_input_test.txt", UTF_8));
34+
35+
// when
36+
Day02 solver = new Day02();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(2L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day02/day02_input.txt", UTF_8));
48+
49+
// when
50+
Day02 solver = new Day02();
51+
solver.parseInput(input);
52+
long l = solver.solvePart2();
53+
54+
// then
55+
assertThat(l, is(692L));
56+
}
57+
58+
@Test
59+
void testInputPart2() throws IOException {
60+
// given
61+
List<String> input = readLines(resourceToString("/day02/day02_input_test.txt", UTF_8));
62+
63+
// when
64+
Day02 solver = new Day02();
65+
solver.parseInput(input);
66+
long l = solver.solvePart2();
67+
68+
// then
69+
assertThat(l, is(4L));
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)