[rs] Simplify 2022_06
This commit is contained in:
parent
39982ce5b5
commit
beee256111
1 changed files with 10 additions and 20 deletions
|
|
@ -1,25 +1,15 @@
|
||||||
use core::panic;
|
use std::collections::HashSet;
|
||||||
use std::collections::{HashSet, VecDeque};
|
|
||||||
|
|
||||||
fn scan(input: &str, lookback: usize) -> usize {
|
fn scan(chars: &[char], lookback: usize) -> usize {
|
||||||
let mut last_n = VecDeque::with_capacity(lookback);
|
chars
|
||||||
for (i, c) in input.chars().enumerate() {
|
.windows(lookback)
|
||||||
last_n.push_back(c);
|
.position(|w| w.iter().copied().collect::<HashSet<_>>().len() == lookback)
|
||||||
while last_n.len() > lookback {
|
.unwrap()
|
||||||
last_n.pop_front();
|
+ lookback
|
||||||
}
|
|
||||||
if last_n.len() < lookback {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let last_n_set = last_n.iter().copied().collect::<HashSet<_>>();
|
|
||||||
if last_n_set.len() == lookback {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve(input: String) {
|
pub fn solve(input: String) {
|
||||||
println!("Part 1: {}", scan(&input, 4) + 1);
|
let chars = input.chars().collect::<Vec<_>>();
|
||||||
println!("Part 2: {}", scan(&input, 14) + 1);
|
println!("Part 1: {}", scan(&chars, 4));
|
||||||
|
println!("Part 2: {}", scan(&chars, 14));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue