Solution for 2022/day06-part1
This commit is contained in:
parent
ef37c1514a
commit
49edb57aa4
8
2022/day06-part1/Cargo.toml
Normal file
8
2022/day06-part1/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day06-part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
30
2022/day06-part1/src/main.rs
Normal file
30
2022/day06-part1/src/main.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use std::collections::{HashSet, VecDeque};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Use command line arguments to specify the input filename.
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
if args.len() < 2 {
|
||||||
|
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next, read the contents of the input file into a string for easier processing.
|
||||||
|
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||||
|
|
||||||
|
// Use a double-ended queue as a ringbuffer to keep track of the characters.
|
||||||
|
let mut deq = VecDeque::from([' '; 4]);
|
||||||
|
|
||||||
|
// Iterate through all characters, together with their respective indices.
|
||||||
|
for (i, char) in input.chars().enumerate() {
|
||||||
|
// Remove the first character from the deque.
|
||||||
|
deq.pop_front();
|
||||||
|
// And add the next character to it.
|
||||||
|
deq.push_back(char);
|
||||||
|
// Collecting into a set, removing duplicates along the way.
|
||||||
|
let hs: HashSet<char> = deq.iter().copied().collect();
|
||||||
|
// Count the number of unique elements in the set.
|
||||||
|
if hs.len() >= 4 && !hs.contains(&' ') {
|
||||||
|
println!("Start of packet: {}", i + 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user