Skip to content

Commit 60d6958

Browse files
committed
feat: solve day 5
1 parent ce2f63e commit 60d6958

File tree

7 files changed

+178
-10
lines changed

7 files changed

+178
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- [Day 2 - Red-Nosed Reports](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day02)
88
- [Day 3 - Mull It Over](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day03)
99
- [Day 4 - Ceres Search](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day04)
10-
- [Day 5](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05)
10+
- [Day 5 - Print Queue](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05)
1111
- [Day 6](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day06)
1212
- [Day 7](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07)
1313
- [Day 8](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day08)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.adventofcode.flashk.day05;
2+
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class PageComparator implements Comparator<Integer> {
9+
10+
private final Map<Integer, Set<Integer>> orderingRules;
11+
12+
public PageComparator(Map<Integer, Set<Integer>> orderingRules) {
13+
this.orderingRules = orderingRules;
14+
}
15+
16+
@Override
17+
public int compare(Integer o1, Integer o2) {
18+
19+
// -1 si o1 precede a o2
20+
// 1 si o2 precede a o1
21+
// Resto de casos: 0
22+
23+
Set<Integer> afterPagesO1 = orderingRules.getOrDefault(o1, Collections.emptySet());
24+
25+
if(afterPagesO1.contains(o2)) {
26+
return -1;
27+
}
28+
29+
Set<Integer> afterPagesO2 = orderingRules.getOrDefault(o2, Collections.emptySet());
30+
31+
if(afterPagesO2.contains(o1)) {
32+
return 1;
33+
}
34+
35+
return 0;
36+
}
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.adventofcode.flashk.day05;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public class PrintQueue {
13+
14+
private final Map<Integer, Set<Integer>> orderingRules = new HashMap<>();
15+
private final List<Update> updates = new ArrayList<>();
16+
17+
public PrintQueue(List<String> inputs) {
18+
boolean isUpdate = false;
19+
for(String input : inputs) {
20+
if(StringUtils.isBlank(input)) {
21+
isUpdate = true;
22+
continue;
23+
}
24+
25+
if(!isUpdate) {
26+
String[] keyValue = input.split("\\|");
27+
28+
Integer key = Integer.valueOf(keyValue[0]);
29+
Integer value = Integer.valueOf(keyValue[1]);
30+
31+
if(orderingRules.containsKey(key)){
32+
orderingRules.get(key).add(value);
33+
} else {
34+
Set<Integer> values = new HashSet<>();
35+
values.add(value);
36+
orderingRules.put(key, values);
37+
}
38+
} else {
39+
updates.add(new Update(input));
40+
}
41+
}
42+
}
43+
44+
45+
public long solveA() {
46+
return updates.stream()
47+
.filter(update -> update.isOrderedBy(orderingRules))
48+
.mapToInt(Update::getMiddlePage)
49+
.sum();
50+
}
51+
52+
public long solveB() {
53+
PageComparator comparator = new PageComparator(orderingRules);
54+
return updates.stream()
55+
.filter(update -> !update.isOrderedBy(orderingRules))
56+
.mapToInt(update -> update.order(comparator))
57+
.sum();
58+
}
59+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 5:
1+
# Day 5: Print Queue
22

33
[https://adventofcode.com/2024/day/5](https://adventofcode.com/2024/day/5)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.adventofcode.flashk.day05;
2+
3+
import lombok.Getter;
4+
5+
import java.util.Arrays;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
12+
public class Update {
13+
14+
private List<Integer> pages;
15+
16+
@Getter
17+
private int middlePage;
18+
19+
public Update(String input) {
20+
pages = Arrays.stream(input.split(",")).map(Integer::valueOf).toList();
21+
middlePage = pages.get(pages.size()/2);
22+
}
23+
24+
public boolean isOrderedBy(Map<Integer, Set<Integer>> orderingRules) {
25+
26+
boolean isOrdered = true;
27+
28+
int i = 0;
29+
while(isOrdered && i < pages.size()) {
30+
int page = pages.get(i);
31+
isOrdered = isOrdered(i, orderingRules.getOrDefault(page, new HashSet<>()));
32+
i++;
33+
}
34+
35+
return isOrdered;
36+
}
37+
38+
public int order(PageComparator comparator) {
39+
40+
pages = pages.stream().sorted(comparator).toList();
41+
middlePage = pages.get(pages.size()/2);
42+
43+
return middlePage;
44+
}
45+
46+
private boolean isOrdered(int pageIndex, Set<Integer> orderingRules) {
47+
48+
if(orderingRules.isEmpty()) {
49+
return true;
50+
}
51+
52+
boolean isOrdered = true;
53+
int i = pageIndex-1;
54+
55+
// Look back
56+
while(isOrdered && i >= 0) {
57+
int previousPage = pages.get(i);
58+
isOrdered = !orderingRules.contains(previousPage);
59+
i--;
60+
}
61+
62+
return isOrdered;
63+
}
64+
}

src/test/java/com/adventofcode/flashk/day05/Day05Test.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.List;
44

55
import org.junit.jupiter.api.BeforeAll;
6-
import org.junit.jupiter.api.Disabled;
76
import org.junit.jupiter.api.DisplayName;
87
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
98
import org.junit.jupiter.api.Order;
@@ -19,12 +18,13 @@
1918
import com.adventofcode.flashk.common.test.utils.Timer;
2019
import com.adventofcode.flashk.common.test.utils.Input;
2120

21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
2223
@DisplayName(TestDisplayName.DAY_05)
2324
@TestMethodOrder(OrderAnnotation.class)
24-
@Disabled // TODO Remove comment when implemented
2525
public class Day05Test extends PuzzleTest {
2626

27-
private final static String INPUT_FOLDER = TestFolder.DAY_05;
27+
private static final String INPUT_FOLDER = TestFolder.DAY_05;
2828

2929
@BeforeAll
3030
public static void beforeAll() {
@@ -43,7 +43,9 @@ public void testSolvePart1Sample() {
4343

4444
// Read input file
4545
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
46-
46+
PrintQueue printQueue = new PrintQueue(inputs);
47+
48+
assertEquals(143, printQueue.solveA());
4749
}
4850

4951
@Test
@@ -57,7 +59,9 @@ public void testSolvePart1Input() {
5759

5860
// Read input file
5961
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
60-
62+
PrintQueue printQueue = new PrintQueue(inputs);
63+
64+
assertEquals(5747, printQueue.solveA());
6165
}
6266

6367
@Test
@@ -71,7 +75,9 @@ public void testSolvePart2Sample() {
7175

7276
// Read input file
7377
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
74-
78+
PrintQueue printQueue = new PrintQueue(inputs);
79+
80+
assertEquals(123, printQueue.solveB());
7581
}
7682

7783
@Test
@@ -85,7 +91,9 @@ public void testSolvePart2Input() {
8591

8692
// Read input file
8793
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
88-
94+
PrintQueue printQueue = new PrintQueue(inputs);
95+
96+
assertEquals(5502, printQueue.solveB());
8997
}
9098

9199
}

src/test/resources/inputs

0 commit comments

Comments
 (0)