generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/adventofcode/flashk/day05/PageComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
src/main/java/com/adventofcode/flashk/day05/PrintQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from cb76b0 to 1d9b85