From 0cd2383f16d6769d3b3449c8b01d0d96805a9b19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20Mei=C3=9Fner?= <florian.meissner@mailbox.org>
Date: Fri, 6 Jan 2023 05:54:56 +0100
Subject: [PATCH] 6pt2

---
 src/day_06.rs | 24 ++++++++++++++++--------
 src/main.rs   | 13 ++++++++++---
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/src/day_06.rs b/src/day_06.rs
index 3336ba4..0bee01d 100644
--- a/src/day_06.rs
+++ b/src/day_06.rs
@@ -1,12 +1,20 @@
-use std::slice::Windows;
-use std::vec;
 use std::collections::{VecDeque, HashSet};
 
 use crate::challenge_args::ChallengeArgs;
 
-const WINDOW_SIZE: usize = 4;
 
-pub fn first_marker_pos(args: &ChallengeArgs) -> Result<usize, ()> {
+type Position = Result<usize, ()>;
+
+
+pub fn start_of_packet_pos(args: &ChallengeArgs) -> Position {
+    return first_marker_pos(args, 4);
+}
+
+pub fn start_of_message_pos(args: &ChallengeArgs) -> Position {
+    return first_marker_pos(args, 14);
+}
+
+fn first_marker_pos(args: &ChallengeArgs, window_size: usize) -> Position {
     let stream: &str = &args.input;
     let mut history: VecDeque<char> = Default::default();
     let mut pos: Result<usize, ()> = Err(());
@@ -14,17 +22,17 @@ pub fn first_marker_pos(args: &ChallengeArgs) -> Result<usize, ()> {
 
         history.push_back(c);
 
-        for i in 0..(history.len().checked_sub(WINDOW_SIZE).unwrap_or(0)) {
+        for i in 0..(history.len().checked_sub(window_size).unwrap_or(0)) {
             history.pop_front();
             //history.drain(range);
         }
-        assert!(history.len() <= WINDOW_SIZE);
+        assert!(history.len() <= window_size);
 
         // only try to match if the window is already filled
-        if history.len() == WINDOW_SIZE {
+        if history.len() == window_size {
             // chars are unique to each other if a set filled with x chars has len==x
             let set: HashSet<&char> = HashSet::from_iter(history.iter());
-            let is_unique: bool = set.len() == WINDOW_SIZE;
+            let is_unique: bool = set.len() == window_size;
 
             //dbg!("history: {}, set: {}, is_unique: {}", &history, set, is_unique);
 
diff --git a/src/main.rs b/src/main.rs
index 8368676..303606c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -57,14 +57,21 @@ fn solve_for_certain_day(args: &ChallengeArgs) {
         //     invalid_task => abort_for_invalid_task(invalid_task),
         // },
         6 => {
-            let pos = day_06::first_marker_pos(&args);
             match args.task {
                 1 => {
+                    let pos = day_06::start_of_packet_pos(args);
                     match pos {
-                        Ok(pos) => println!("First marker appearing at: {}", pos),
-                        Err(_) => println!("No marker found!")
+                        Ok(pos) => println!("Packet starts at: {}", pos),
+                        Err(_) => println!("Packet: No marker found!")
                     }
                 },
+                2 => {
+                    let pos = day_06::start_of_message_pos(args);
+                    match pos {
+                        Ok(pos) => println!("Message starts at: {}", pos),
+                        Err(_) => println!("Message: No marker found!")
+                    }
+                }
                 invalid_task => abort_for_invalid_task(invalid_task)
             }
         },
-- 
GitLab