From fb5277ae5f29fbf644c0e77ff9228ec4f2f49a3c Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Thu, 8 Dec 2022 21:50:41 -0600 Subject: [PATCH] Implement a solution for day 6 part 1 and 2 --- day_06/java/Generic.java | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 day_06/java/Generic.java diff --git a/day_06/java/Generic.java b/day_06/java/Generic.java new file mode 100644 index 0000000..af02e83 --- /dev/null +++ b/day_06/java/Generic.java @@ -0,0 +1,47 @@ +package day_06.java; + +import java.io.FileNotFoundException; +import java.util.Scanner; + +import jlib.FileSystem; +import jlib.Sequence; + +class Generic { + static Integer bufferSize; + + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Not enough arguments"); + return; + } + + bufferSize = Integer.parseInt(args[1]); + + Scanner s; + try { + s = FileSystem.readFile(args[0]); + } catch (FileNotFoundException e) { + System.out.println("Can't find the file"); + return; + } + + while (s.hasNextLine()) { + String line = s.nextLine(); + Sequence buffer = new Sequence<>(bufferSize); + + // Ensure the line has enough characters to make a marker + if (line.length() < 4) { + System.out.println("Not long enough to get a marker"); + } + + // Find the marker in the data + for (int i = 0; i < line.length(); i++) { + buffer.add(line.charAt(i)); + if (buffer.size() == bufferSize && buffer.isUnique()) { + System.out.println("Found marker at character " + (i + 1)); + break; + } + } + } + } +}