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

Completed Year 2022 Day 6 Part 1

parent 5e5d59b5
No related branches found
No related tags found
No related merge requests found
......@@ -161,5 +161,19 @@ techniques should be used.
## Day 6
- [The problem](https://adventofcode.com/2022/day/6)
- [The solution](src/main/java/edu/unl/cse/bohn/year2022/Day6.java)
### Part 1
The subproblems are
- Identify the first sequence of four unique characters in a string
- Assume there is such a sequence
- Determine when a sequence of four characters contain duplicates
- 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
## Day 7
(coming soon)
package edu.unl.cse.bohn.year2022;
import edu.unl.cse.bohn.Puzzle;
import java.util.*;
@SuppressWarnings("unused")
public class Day6 extends Puzzle {
@SuppressWarnings({"SpellCheckingInspection", "CommentedOutCode"})
public Day6(boolean isProductionReady) {
super(isProductionReady);
// sampleData = "mjqjpqmgbljsphdztnvjfqwrcgsmlb";
// sampleData = "bvwbjplbgvbhsrlpgdmjqwftvncz";
// sampleData = "nppdvjthqldpwncqszvftbrmjlhg";
// sampleData = "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg";
sampleData = "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw";
}
@Override
public long computePart1(List<String> data) {
return getStartOfPacketIndex(data.get(0));
}
@Override
public long computePart2(List<String> data) {
return 0;
}
private long getStartOfPacketIndex(String dataStream) {
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) {
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