Skip to content

Commit

Permalink
feat: solve day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 5, 2024
1 parent ce2f63e commit 60d6958
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [Day 2 - Red-Nosed Reports](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day02)
- [Day 3 - Mull It Over](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day03)
- [Day 4 - Ceres Search](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day04)
- [Day 5](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05)
- [Day 5 - Print Queue](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05)
- [Day 6](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day06)
- [Day 7](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07)
- [Day 8](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day08)
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/adventofcode/flashk/day05/PageComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.adventofcode.flashk.day05;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;

public class PageComparator implements Comparator<Integer> {

private final Map<Integer, Set<Integer>> orderingRules;

public PageComparator(Map<Integer, Set<Integer>> orderingRules) {
this.orderingRules = orderingRules;
}

@Override
public int compare(Integer o1, Integer o2) {

// -1 si o1 precede a o2
// 1 si o2 precede a o1
// Resto de casos: 0

Set<Integer> afterPagesO1 = orderingRules.getOrDefault(o1, Collections.emptySet());

if(afterPagesO1.contains(o2)) {
return -1;
}

Set<Integer> afterPagesO2 = orderingRules.getOrDefault(o2, Collections.emptySet());

if(afterPagesO2.contains(o1)) {
return 1;
}

return 0;
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/adventofcode/flashk/day05/PrintQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.adventofcode.flashk.day05;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PrintQueue {

private final Map<Integer, Set<Integer>> orderingRules = new HashMap<>();
private final List<Update> updates = new ArrayList<>();

public PrintQueue(List<String> inputs) {
boolean isUpdate = false;
for(String input : inputs) {
if(StringUtils.isBlank(input)) {
isUpdate = true;
continue;
}

if(!isUpdate) {
String[] keyValue = input.split("\\|");

Integer key = Integer.valueOf(keyValue[0]);
Integer value = Integer.valueOf(keyValue[1]);

if(orderingRules.containsKey(key)){
orderingRules.get(key).add(value);
} else {
Set<Integer> values = new HashSet<>();
values.add(value);
orderingRules.put(key, values);
}
} else {
updates.add(new Update(input));
}
}
}


public long solveA() {
return updates.stream()
.filter(update -> update.isOrderedBy(orderingRules))
.mapToInt(Update::getMiddlePage)
.sum();
}

public long solveB() {
PageComparator comparator = new PageComparator(orderingRules);
return updates.stream()
.filter(update -> !update.isOrderedBy(orderingRules))
.mapToInt(update -> update.order(comparator))
.sum();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/adventofcode/flashk/day05/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Day 5:
# Day 5: Print Queue

[https://adventofcode.com/2024/day/5](https://adventofcode.com/2024/day/5)
64 changes: 64 additions & 0 deletions src/main/java/com/adventofcode/flashk/day05/Update.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.adventofcode.flashk.day05;

import lombok.Getter;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Update {

private List<Integer> pages;

@Getter
private int middlePage;

public Update(String input) {
pages = Arrays.stream(input.split(",")).map(Integer::valueOf).toList();
middlePage = pages.get(pages.size()/2);
}

public boolean isOrderedBy(Map<Integer, Set<Integer>> orderingRules) {

boolean isOrdered = true;

int i = 0;
while(isOrdered && i < pages.size()) {
int page = pages.get(i);
isOrdered = isOrdered(i, orderingRules.getOrDefault(page, new HashSet<>()));
i++;
}

return isOrdered;
}

public int order(PageComparator comparator) {

pages = pages.stream().sorted(comparator).toList();
middlePage = pages.get(pages.size()/2);

return middlePage;
}

private boolean isOrdered(int pageIndex, Set<Integer> orderingRules) {

if(orderingRules.isEmpty()) {
return true;
}

boolean isOrdered = true;
int i = pageIndex-1;

// Look back
while(isOrdered && i >= 0) {
int previousPage = pages.get(i);
isOrdered = !orderingRules.contains(previousPage);
i--;
}

return isOrdered;
}
}
22 changes: 15 additions & 7 deletions src/test/java/com/adventofcode/flashk/day05/Day05Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
Expand All @@ -19,12 +18,13 @@
import com.adventofcode.flashk.common.test.utils.Timer;
import com.adventofcode.flashk.common.test.utils.Input;

import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName(TestDisplayName.DAY_05)
@TestMethodOrder(OrderAnnotation.class)
@Disabled // TODO Remove comment when implemented
public class Day05Test extends PuzzleTest {

private final static String INPUT_FOLDER = TestFolder.DAY_05;
private static final String INPUT_FOLDER = TestFolder.DAY_05;

@BeforeAll
public static void beforeAll() {
Expand All @@ -43,7 +43,9 @@ public void testSolvePart1Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

PrintQueue printQueue = new PrintQueue(inputs);

assertEquals(143, printQueue.solveA());
}

@Test
Expand All @@ -57,7 +59,9 @@ public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

PrintQueue printQueue = new PrintQueue(inputs);

assertEquals(5747, printQueue.solveA());
}

@Test
Expand All @@ -71,7 +75,9 @@ public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

PrintQueue printQueue = new PrintQueue(inputs);

assertEquals(123, printQueue.solveB());
}

@Test
Expand All @@ -85,7 +91,9 @@ public void testSolvePart2Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

PrintQueue printQueue = new PrintQueue(inputs);

assertEquals(5502, printQueue.solveB());
}

}
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit 60d6958

Please sign in to comment.