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 6229d39

Browse files
day05 for AoC 2023 (part 1)
Took 1 hour 23 minutes
1 parent 773130c commit 6229d39

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package aminetti.adventofcode2024.day05;
2+
3+
import aminetti.adventofcode2023.day05.SeedFertilizer;
4+
import com.google.common.collect.Lists;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.*;
9+
import java.util.function.Function;
10+
import java.util.stream.Collectors;
11+
12+
public class Day05 {
13+
private static final Logger LOGGER = LoggerFactory.getLogger(Day05.class);
14+
private List<String> input;
15+
16+
private HashMap<String, List<String>> rules = new HashMap<>();
17+
private List<List<String>> allPages = new ArrayList<>();
18+
19+
public Day05() {
20+
}
21+
22+
public void parseInput(List<String> input) {
23+
this.input = input;
24+
25+
26+
var firstInputPart = true;
27+
for (String s : input) {
28+
if (firstInputPart) {
29+
if (s.isEmpty()) {
30+
firstInputPart = false;
31+
continue;
32+
}
33+
//LOGGER.debug("Line: {}", s);
34+
String[] split = s.split("\\|");
35+
List<String> currentRule = rules.getOrDefault(split[0], new ArrayList<>());
36+
currentRule.add(split[1]);
37+
rules.put(split[0], currentRule);
38+
} else {
39+
String[] split = s.split(",");
40+
allPages.add(Arrays.stream(split).toList());
41+
}
42+
43+
}
44+
}
45+
46+
public long solvePart1() {
47+
LOGGER.info("Rules: {}", rules);
48+
LOGGER.info("All pages: {}", allPages);
49+
50+
long sum = 0;
51+
for (List<String> pageList : allPages) {
52+
if (isInOrder(pageList)) {
53+
String middlePage = pageList.get(pageList.size() / 2);
54+
LOGGER.info("Found ordered page: {}; adding {} to the sum", pageList, middlePage);
55+
sum += Long.parseLong(middlePage);
56+
}
57+
}
58+
59+
60+
return sum;
61+
}
62+
63+
private boolean isInOrder(List<String> pageList) {
64+
Map<String, Integer> inEdgeCount = pageList.stream().collect(Collectors.toMap(Function.identity(), (x) -> 0));
65+
for (String p : rules.keySet()) {
66+
for (String v : rules.get(p)) {
67+
if (pageList.contains(v) && pageList.contains(p)) {
68+
inEdgeCount.put(v, inEdgeCount.get(v) + 1);
69+
}
70+
}
71+
}
72+
LOGGER.info("The pages {} have this inEdgeCount: {}", pageList, inEdgeCount);
73+
List<String> firstElements = inEdgeCount.entrySet().stream()
74+
.filter(e -> e.getValue() == 0)
75+
.map(Map.Entry::getKey)
76+
.collect(Collectors.toList());
77+
78+
List<List<String>> partialOrder = new ArrayList<>();
79+
partialOrder.add(firstElements);
80+
81+
for (int i = 0; i < partialOrder.size(); i++) {
82+
List<String> level = partialOrder.get(i);
83+
if (level.isEmpty()) {
84+
break;
85+
}
86+
List<String> nextLevel = new ArrayList<>();
87+
partialOrder.add(nextLevel);
88+
for (String e : level) {
89+
if (!rules.containsKey(e)) {
90+
continue;
91+
}
92+
for (String v : rules.get(e)) {
93+
if (!pageList.contains(v)) {
94+
continue;
95+
}
96+
Integer inEdgesForV = inEdgeCount.get(v);
97+
inEdgesForV--;
98+
inEdgeCount.put(v, inEdgesForV);
99+
if (inEdgesForV == 0) {
100+
nextLevel.add(v);
101+
}
102+
}
103+
}
104+
}
105+
106+
int currentLevel = 0;
107+
for (String p : pageList) {
108+
while (!partialOrder.get(currentLevel).contains(p)) {
109+
LOGGER.info("{} is not in level [{}] {}, so switching to the next level", p, currentLevel, partialOrder.get(currentLevel));
110+
currentLevel++;
111+
if (currentLevel > partialOrder.size() - 1) {
112+
LOGGER.info("{} are NOT ordered", pageList);
113+
LOGGER.info(" current level {} and partialOrder {}", currentLevel, partialOrder);
114+
115+
return false;
116+
}
117+
}
118+
119+
}
120+
121+
LOGGER.info("{} are ordered", pageList);
122+
return true;
123+
}
124+
125+
126+
public long solvePart2() {
127+
128+
return 0;
129+
}
130+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day05;
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 Day05Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day05/day05_input.txt", UTF_8));
20+
21+
// when
22+
Day05 solver = new Day05();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(0L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day05/day05_input_test.txt", UTF_8));
34+
35+
// when
36+
Day05 solver = new Day05();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(143L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day05/day05_input.txt", UTF_8));
48+
49+
// when
50+
Day05 solver = new Day05();
51+
solver.parseInput(input);
52+
long l = solver.solvePart2();
53+
54+
// then
55+
assertThat(l, is(0L));
56+
}
57+
58+
@Test
59+
void testInputPart2() throws IOException {
60+
// given
61+
List<String> input = readLines(resourceToString("/day05/day05_input_test.txt", UTF_8));
62+
63+
// when
64+
Day05 solver = new Day05();
65+
solver.parseInput(input);
66+
long l = solver.solvePart2();
67+
68+
// then
69+
assertThat(l, is(0L));
70+
}
71+
72+
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
47|53
2+
97|13
3+
97|61
4+
97|47
5+
75|29
6+
61|13
7+
75|53
8+
29|13
9+
97|29
10+
53|29
11+
61|53
12+
97|53
13+
61|29
14+
47|13
15+
75|47
16+
97|75
17+
47|61
18+
75|61
19+
47|29
20+
75|13
21+
53|13
22+
23+
75,47,61,53,29
24+
97,61,53,29,13
25+
75,29,13
26+
75,97,47,61,53
27+
61,13,29
28+
97,13,75,29,47

0 commit comments

Comments
(0)

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