Skip to content
Snippets Groups Projects
Commit 1c8ffd0c authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Completed Year 2022 Day 2 Part 2

parent 1f2dfe11
Branches
No related tags found
No related merge requests found
...@@ -63,6 +63,23 @@ The subproblems are ...@@ -63,6 +63,23 @@ The subproblems are
- *Aha!* one of the knapsacks in the sample data has 'L' as a common item, twice - *Aha!* one of the knapsacks in the sample data has 'L' as a common item, twice
- Sum the priorities of all the "common items" - Sum the priorities of all the "common items"
### Part 2
The subproblems are
- Determine an item's priority (solved from part 1)
- ~~Determine which items are present in exactly three knapsacks~~
- ~~Determine which knapsacks each item is in~~
- ~~I'm not sure that it matters, but what if a "triplet item" occurs multiple times in a knapsack; is it no longer a "triplet item"? We probably could check by finding out if an elf is in more than one triplet.~~
- Looks like I misread the problem statement originally. It seems that we don't need to find the triplets; the triplets appear in order in the data, knapsacks {*i*, *i+1*, *i+2*}
- Assume the number of knapsacks is divisible by 3
- Determine which item is common in each triplet
- Assume each triplet has exactly one "triplet item"
- Sum the priorities of all the "triplet items"
### Refactoring opportunity
Both problems involve finding a singular common character -- one among two strings, the other among three.
#Day 4 #Day 4
(coming soon) (coming soon)
......
...@@ -2,8 +2,7 @@ package edu.unl.cse.bohn.year2022; ...@@ -2,8 +2,7 @@ package edu.unl.cse.bohn.year2022;
import edu.unl.cse.bohn.Puzzle; import edu.unl.cse.bohn.Puzzle;
import java.util.Iterator; import java.util.*;
import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Day3 extends Puzzle { public class Day3 extends Puzzle {
...@@ -37,51 +36,72 @@ public class Day3 extends Puzzle { ...@@ -37,51 +36,72 @@ public class Day3 extends Puzzle {
@Override @Override
public long computePart2(List<String> data) { public long computePart2(List<String> data) {
return 0; long prioritySum = 0;
if (data.size() % 3 != 0) {
throw new IllegalArgumentException("The number of knapsacks (" + data.size() + ") is not divisible by three.");
}
for (int i = 0; i < data.size(); i += 3) {
char commonItem = getCommonItem(data.get(i), data.get(i + 1), data.get(i + 2));
prioritySum += getPriority(commonItem);
}
return prioritySum;
} }
private char getCommonItem(String firstCompartment, String secondCompartment) { private char getCommonItem(String firstCompartment, String secondCompartment) {
return getCommonItem(firstCompartment, secondCompartment, null);
}
private char getCommonItem(String firstCollection, String secondCollection, String thirdCollection) {
Character commonItem = null; Character commonItem = null;
List<Character> firstCompartmentItems = firstCompartment.chars().sorted().mapToObj(c -> (char) c).toList(); List<Character> firstItems = firstCollection.chars().sorted().mapToObj(c -> (char) c).toList();
List<Character> secondCompartmentItems = secondCompartment.chars().sorted().mapToObj(c -> (char) c).toList(); List<Character> secondItems = secondCollection.chars().sorted().mapToObj(c -> (char) c).toList();
Iterator<Character> firstCompartmentIterator = firstCompartmentItems.iterator(); List<Character> thirdItems = (thirdCollection == null) ?
Iterator<Character> secondCompartmentIterator = secondCompartmentItems.iterator(); List.copyOf(secondItems) : thirdCollection.chars().sorted().mapToObj(c -> (char) c).toList();
Iterator<Character> firstIterator = firstItems.iterator();
Iterator<Character> secondIterator = secondItems.iterator();
Iterator<Character> thirdIterator = thirdItems.iterator();
Character firstItem = null; Character firstItem = null;
Character secondItem = null; Character secondItem = null;
// with the items sorted, we will advance through the items until we reach the end one of the compartments Character thirdItem = null;
while (firstCompartmentIterator.hasNext() || secondCompartmentIterator.hasNext()) { // with the items sorted, we will advance through the items until we reach the end of the compartments
firstItem = (firstItem == null && firstCompartmentIterator.hasNext()) ? while (firstIterator.hasNext() || secondIterator.hasNext() || thirdIterator.hasNext()) {
firstCompartmentIterator.next() : firstItem; firstItem = (firstItem == null && firstIterator.hasNext()) ?
secondItem = (secondItem == null && secondCompartmentIterator.hasNext()) ? firstIterator.next() : firstItem;
secondCompartmentIterator.next() : secondItem; secondItem = (secondItem == null && secondIterator.hasNext()) ?
if (commonItem == null && (firstItem == null || secondItem == null)) { secondIterator.next() : secondItem;
throw new IllegalArgumentException("Reached the end of a compartment among " thirdItem = (thirdItem == null && thirdIterator.hasNext()) ?
+ firstCompartment + " and " + secondCompartment + " without finding a common item."); thirdIterator.next() : thirdItem;
} if (commonItem == null && (firstItem == null || secondItem == null || thirdItem == null)) {
else if (firstItem == secondItem) { throw new IllegalArgumentException("Reached the end of a compartment among {" + firstCollection + " " +
secondCollection + " " + thirdCollection + " without finding a common item.");
} else if (firstItem == secondItem && secondItem == thirdItem) {
if (commonItem == null || commonItem == firstItem) { if (commonItem == null || commonItem == firstItem) {
commonItem = firstItem; commonItem = firstItem;
firstItem = null; firstItem = null;
secondItem = null; secondItem = null;
thirdItem = null;
} else { } else {
throw new IllegalStateException("Found a common item (" + firstItem + ") when one already exists " + throw new IllegalStateException("Found a common item (" + firstItem + ") when one already exists " +
"(" + commonItem + ") for compartments " + firstCompartment + " and " + secondCompartment); "(" + commonItem + ") among {" + firstCollection + " " + secondCollection + " " +
thirdCollection + "}");
} }
} else if (firstItem != null && secondItem != null) { } else if (firstItem != null && secondItem != null && thirdItem != null) {
if (firstItem < secondItem) { if (firstItem < secondItem || firstItem < thirdItem) {
firstItem = null; firstItem = null;
} else { } else if (secondItem < thirdItem) {
secondItem = null; secondItem = null;
} else {
thirdItem = null;
} }
} else { // if we've gotten this far with a null item, then we just need to exhaust the remaining string } else { // if we've gotten this far with a null item, then we just need to exhaust the remaining strings
firstItem = null; firstItem = null;
secondItem = null; secondItem = null;
thirdItem = null;
} }
} }
if (commonItem == null) { if (commonItem == null) {
throw new IllegalArgumentException("No common item found among " throw new IllegalArgumentException("No common item found among {"
+ firstCompartment + " and " + secondCompartment); + firstCollection + " " + secondCollection + " " + thirdCollection + "}");
// + firstCompartmentItems + " and " + secondCompartmentItems);
} }
return commonItem; return commonItem;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment