From e3fe1fd7335c3834c2266ea9c94796699b7aa286 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Tue, 6 Dec 2022 08:00:33 -0600 Subject: [PATCH] Completed Year 2022 Day 6 Part 1 --- 2022/README.md | 14 +++++ .../java/edu/unl/cse/bohn/year2022/Day6.java | 51 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 2022/src/main/java/edu/unl/cse/bohn/year2022/Day6.java diff --git a/2022/README.md b/2022/README.md index 214ea26..46dd6d6 100644 --- a/2022/README.md +++ b/2022/README.md @@ -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) diff --git a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day6.java b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day6.java new file mode 100644 index 0000000..b33b4ac --- /dev/null +++ b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day6.java @@ -0,0 +1,51 @@ +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; + } +} -- GitLab