days 5 + 6
This commit is contained in:
Generated
+16
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rustc-hash = "1.1.0"
|
||||
@@ -0,0 +1,17 @@
|
||||
use rustc_hash::FxHashSet;
|
||||
const PACKET_WINDOW_SIZE: usize = 4;
|
||||
const MESSAGE_WINDOW_SIZE: usize = 14;
|
||||
|
||||
fn main() {
|
||||
let s = std::fs::read_to_string("/dev/stdin").unwrap();
|
||||
println!("P1: {}, P2: {}", find_index(&s, PACKET_WINDOW_SIZE), find_index(&s, MESSAGE_WINDOW_SIZE));
|
||||
}
|
||||
|
||||
fn find_index(s: &str, window_size: usize) -> usize {
|
||||
for (count, window) in s.chars().collect::<Vec<char>>().windows(window_size).enumerate(){
|
||||
if FxHashSet::from_iter(window).len() == window_size {
|
||||
return count + window_size;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
Reference in New Issue
Block a user