From 216616ab804a9b8df86511f356f692db5406d654 Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 5 Dec 2022 22:54:35 -0800 Subject: Day Six --- 2022/rust/day06/src/main.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2022/rust/day06/src/main.rs (limited to '2022/rust/day06/src') diff --git a/2022/rust/day06/src/main.rs b/2022/rust/day06/src/main.rs new file mode 100644 index 0000000..b5f42cc --- /dev/null +++ b/2022/rust/day06/src/main.rs @@ -0,0 +1,27 @@ +use std::collections::HashSet; +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let signal: Vec = input.trim().chars().collect(); + + let depth = 4; + for (i, c) in signal.windows(depth).enumerate() { + // why can rust not infer the type of HashSet? + // https://stackoverflow.com/questions/62949404/ + if HashSet::<_>::from_iter(c).len() == depth { + println!("{}", i + depth); + break; + } + } + + let depth = 14; + for (i, c) in signal.windows(depth).enumerate() { + if HashSet::<_>::from_iter(c).len() == depth { + println!("{}", i + depth); + break; + } + } +} -- cgit v1.2.3-70-g09d2