Skip to content

Commit 03d6547

Browse files
day02 for AoC 2023 (refactoring)
Took 10 minutes
1 parent 37b205d commit 03d6547

File tree

1 file changed

+20
-41
lines changed
  • src/main/java/aminetti/adventofcode2024/day02

1 file changed

+20
-41
lines changed

src/main/java/aminetti/adventofcode2024/day02/Day02.java

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@
1010

1111
public class Day02 {
1212
private static final Logger LOGGER = LoggerFactory.getLogger(Day02.class);
13-
private List<String> input;
13+
private List<List<Long>> allLists;
1414

1515
public Day02() {
1616
}
1717

1818
public void parseInput(List<String> input) {
19-
this.input = input;
20-
}
21-
22-
public long solvePart1() {
2319
Pattern p = Pattern.compile("(\\d+)");
24-
25-
List<List<Long>> allLists = new ArrayList<>(input.size());
20+
allLists = new ArrayList<>(input.size());
2621

2722
for (String s : input) {
2823
Matcher matcher = p.matcher(s);
@@ -36,16 +31,16 @@ public long solvePart1() {
3631

3732
allLists.add(list);
3833
}
34+
}
3935

36+
public long solvePart1() {
4037
return allLists.stream()
4138
.filter(this::isSafe)
4239
.count();
43-
4440
}
4541

4642
private boolean isSafe(List<Long> l) {
47-
48-
LOGGER.info("Checking {}", l);
43+
LOGGER.debug("Checking {}", l);
4944
Long prev = null;
5045
Boolean ascending = null;
5146
for (Long i : l) {
@@ -54,68 +49,52 @@ private boolean isSafe(List<Long> l) {
5449
continue;
5550
}
5651

57-
if (prev == i) {
58-
LOGGER.info(" has 2 same values {}", i);
52+
if (prev.compareTo(i) == 0) {
53+
LOGGER.debug(" has 2 same values {}", i);
5954
return false;
6055
}
6156

6257
if (ascending == null) {
63-
ascending = prev < i;
64-
LOGGER.info(" is {}", ascending ? "ascending" : "descending");
58+
ascending = prev.compareTo(i) < 0;
59+
LOGGER.debug(" is {}", ascending ? "ascending" : "descending");
6560
} else {
6661

67-
if (ascending && prev >= i) {
68-
LOGGER.info(" started as ascending, but is not!");
62+
if (ascending && prev.compareTo(i) > 0) {
63+
LOGGER.debug(" started as ascending, but is not!");
6964
return false;
7065
}
71-
if (!ascending && prev <= i) {
72-
LOGGER.info(" started as descending, but is not!");
66+
if (!ascending && prev.compareTo(i) < 0) {
67+
LOGGER.debug(" started as descending, but is not!");
7368
return false;
7469
}
7570
}
7671

77-
if (Math.abs(prev - i) > 3 && Math.abs(prev - i) > 0) {
78-
LOGGER.info(" has too big gap between {} and {}", prev, i);
72+
long absDiff = Math.abs(prev - i);
73+
if (absDiff > 3) {
74+
LOGGER.debug(" has too big gap between {} and {}", prev, i);
7975
return false;
8076
}
8177

8278
prev = i;
8379
}
8480

85-
LOGGER.info(" is safe");
81+
LOGGER.debug(" is safe");
8682
return true;
8783
}
8884

8985
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-
10786
return allLists.stream()
10887
.filter(this::isSafeWithoutOneLevel)
10988
.count();
11089
}
11190

11291
private boolean isSafeWithoutOneLevel(List<Long> l) {
113-
List<List<Long>> possibleCombinations = new ArrayList<>();
92+
List<List<Long>> orderedSelectionWithOneElement = new ArrayList<>(l.size());
11493
for (int i = 0; i < l.size(); i++) {
11594
List<Long> l1 = new ArrayList<>(l);
11695
l1.remove(i);
117-
possibleCombinations.add(l1);
96+
orderedSelectionWithOneElement.add(l1);
11897
}
119-
return possibleCombinations.stream().anyMatch(this::isSafe);
98+
return orderedSelectionWithOneElement.stream().anyMatch(this::isSafe);
12099
}
121100
}

0 commit comments

Comments
 (0)