23 lines
813 B
Rust
23 lines
813 B
Rust
use std::{collections::HashSet, fs::read_to_string};
|
|
|
|
fn find_nonrecuring_sequence(input: &[char], length: usize) -> usize {
|
|
input
|
|
.windows(length)
|
|
.enumerate()
|
|
.find(|(_, chars)| chars.iter().collect::<HashSet<_>>().len() == length)
|
|
.unwrap_or((input.len(), &[]))
|
|
.0
|
|
+ length
|
|
}
|
|
|
|
fn main() {
|
|
const PACKET_START_LENGTH: usize = 4;
|
|
const MESSAGE_START_LENGTH: usize = 14;
|
|
let input = read_to_string("input.txt").unwrap();
|
|
let input = input.chars().collect::<Vec<_>>();
|
|
let packet_marker = find_nonrecuring_sequence(&input, PACKET_START_LENGTH);
|
|
println!("first packet marker at {packet_marker}");
|
|
let message_marker = find_nonrecuring_sequence(&input, MESSAGE_START_LENGTH);
|
|
println!("first message marker at {message_marker}");
|
|
}
|