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

Completed Year 2022 Day 6

parent e3fe1fd7
No related branches found
No related tags found
No related merge requests found
......@@ -173,6 +173,14 @@ The subproblems are
- Count the number of characters before the next character after that sequence
- *i.e.*, count the number of characters before that sequence plus the four characters in that sequence
### Part 2
The difference here is the length of the character sequence with unique characters.
### Refactoring opportunity
This is straight-forward -- I just need to parameterize the helper method that looks for the unique-character sequence.
## Day 7
(coming soon)
......
......@@ -18,33 +18,32 @@ public class Day6 extends Puzzle {
@Override
public long computePart1(List<String> data) {
return getStartOfPacketIndex(data.get(0));
return getStartOfPacketIndex(data.get(0), 4);
}
@Override
public long computePart2(List<String> data) {
return 0;
//noinspection MagicNumber
return getStartOfPacketIndex(data.get(0), 14);
}
private long getStartOfPacketIndex(String dataStream) {
private long getStartOfPacketIndex(String dataStream, int lengthOfMarker) {
LinkedList<Character> markerCandidate = new LinkedList<>();
int nextIndex = 0;
int indexOfDuplicate = Integer.MIN_VALUE;
boolean foundMarker = false;
while (!foundMarker) {
char c = dataStream.charAt(nextIndex++);
// System.out.print(markerCandidate + " + " + c + " = ");
int indexOfPossibleDuplicate = markerCandidate.lastIndexOf(c);
indexOfDuplicate = Integer.max(indexOfDuplicate, indexOfPossibleDuplicate);
markerCandidate.add(c);
if (nextIndex > 4) {
if (nextIndex > lengthOfMarker) {
markerCandidate.remove();
indexOfDuplicate--;
if (indexOfDuplicate < 0) {
foundMarker = true;
}
}
// System.out.println(markerCandidate);
}
return nextIndex;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment