Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 617a14e

Browse files
day05 for AoC 2023 (part 2)
Took 29 minutes
1 parent 6229d39 commit 617a14e

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

‎src/main/java/aminetti/adventofcode2024/day05/Day05.java

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package aminetti.adventofcode2024.day05;
22

3-
import aminetti.adventofcode2023.day05.SeedFertilizer;
4-
import com.google.common.collect.Lists;
53
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
75

@@ -124,7 +122,70 @@ private boolean isInOrder(List<String> pageList) {
124122

125123

126124
public long solvePart2() {
125+
LOGGER.info("Rules: {}", rules);
126+
LOGGER.info("All pages: {}", allPages);
127+
128+
129+
List<List<String>> unOrderedLists = allPages.stream().filter(x -> !isInOrder(x)).collect(Collectors.toList());
130+
131+
long sum = 0;
132+
for (List<String> pageList : unOrderedLists) {
133+
LOGGER.info("Ordering {}", pageList);
134+
List<String> orderedList = orderList(pageList);
135+
String middlePage = orderedList.get(pageList.size() / 2);
136+
LOGGER.info("Ordered the list {} in this way {}; and the middle element is {}", pageList, orderedList, middlePage);
137+
sum += Long.parseLong(middlePage);
138+
139+
}
140+
141+
142+
return sum;
143+
}
144+
145+
private List<String> orderList(List<String> pageList) {
146+
Map<String, Integer> inEdgeCount = pageList.stream().collect(Collectors.toMap(Function.identity(), (x) -> 0));
147+
for (String p : rules.keySet()) {
148+
for (String v : rules.get(p)) {
149+
if (pageList.contains(v) && pageList.contains(p)) {
150+
inEdgeCount.put(v, inEdgeCount.get(v) + 1);
151+
}
152+
}
153+
}
154+
LOGGER.info("The pages {} have this inEdgeCount: {}", pageList, inEdgeCount);
155+
List<String> firstElements = inEdgeCount.entrySet().stream()
156+
.filter(e -> e.getValue() == 0)
157+
.map(Map.Entry::getKey)
158+
.collect(Collectors.toList());
159+
160+
List<List<String>> partialOrder = new ArrayList<>();
161+
partialOrder.add(firstElements);
162+
163+
for (int i = 0; i < partialOrder.size(); i++) {
164+
List<String> level = partialOrder.get(i);
165+
if (level.isEmpty()) {
166+
break;
167+
}
168+
List<String> nextLevel = new ArrayList<>();
169+
partialOrder.add(nextLevel);
170+
for (String e : level) {
171+
if (!rules.containsKey(e)) {
172+
continue;
173+
}
174+
for (String v : rules.get(e)) {
175+
if (!pageList.contains(v)) {
176+
continue;
177+
}
178+
Integer inEdgesForV = inEdgeCount.get(v);
179+
inEdgesForV--;
180+
inEdgeCount.put(v, inEdgesForV);
181+
if (inEdgesForV == 0) {
182+
nextLevel.add(v);
183+
}
184+
}
185+
}
186+
}
187+
ArrayList<String> unordered = new ArrayList<>(pageList);
188+
return partialOrder.stream().flatMap(x -> x.stream()).toList();
127189

128-
return 0;
129190
}
130191
}

‎src/test/java/aminetti/adventofcode2024/day05/Day05Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void actualInputPart1() throws IOException {
2424
long l = solver.solvePart1();
2525

2626
// then
27-
assertThat(l, is(0L));
27+
assertThat(l, is(5166L));
2828
}
2929

3030
@Test
@@ -52,7 +52,7 @@ void actualInputPart2() throws IOException {
5252
long l = solver.solvePart2();
5353

5454
// then
55-
assertThat(l, is(0L));
55+
assertThat(l, is(4679L));
5656
}
5757

5858
@Test
@@ -66,7 +66,7 @@ void testInputPart2() throws IOException {
6666
long l = solver.solvePart2();
6767

6868
// then
69-
assertThat(l, is(0L));
69+
assertThat(l, is(123L));
7070
}
7171

7272

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /