From f54f65ca694c69d83c8c5a0168d7b4c6d4ec6a46 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 6 Dec 2022 23:11:05 +0800 Subject: [PATCH] day 6 Quick challenge today, just from the input alone it is easy to conclude the naive solution will work fine even with a large windowing size. --- day_6/src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/day_6/src/main.rs b/day_6/src/main.rs index e7a11a9..30b2442 100644 --- a/day_6/src/main.rs +++ b/day_6/src/main.rs @@ -1,3 +1,57 @@ -fn main() { - println!("Hello, world!"); +use std::{collections::HashSet, fs::read_to_string, ops::Add}; + +const PATH: &str = "src/input"; + +const HEADER_PACKET_LENGTH: usize = 4; +const MESSAGE_LENGTH: usize = 14; + +fn is_unique(chunk: &str) -> bool { + let mut set = HashSet::::new(); + for c in chunk.bytes().into_iter() { + if set.contains(&c) { + return false; + } else { + set.insert(c); + } + } + return true; +} + +fn main() { + assert!(HEADER_PACKET_LENGTH > 0); + assert!(MESSAGE_LENGTH > 0); + + let datagram = read_to_string(PATH) + .expect("Error reading file") + .replace(char::is_whitespace, "") + .add(&"?".repeat(HEADER_PACKET_LENGTH)) + .add(&"?".repeat(MESSAGE_LENGTH)); + + assert!(datagram.is_ascii()); + + let mut header_idx: usize = 0; + let mut message_idx: usize = 0; + + for i in 0..datagram.len() { + // There are certainly more efficient methods that save computation by only + // computing whether the addition of one character and removal of the last + // are sufficient to be unique. This is easier though. + if header_idx == 0 { + let window_header = datagram.get(i..i + HEADER_PACKET_LENGTH).unwrap(); + if is_unique(window_header) { + header_idx = i + HEADER_PACKET_LENGTH; + } + } + if message_idx == 0 { + let window_message = datagram.get(i..i + MESSAGE_LENGTH).unwrap(); + if is_unique(window_message) { + message_idx = i + MESSAGE_LENGTH; + } + } + if header_idx != 0 && message_idx != 0 { + println!("PART 1 {}", header_idx); + println!("PART 2 {}", message_idx); + break; + } + } }