[rs] Solve 2022_06 part 2
This commit is contained in:
parent
889410ee83
commit
a2a69a373a
7 changed files with 30 additions and 16 deletions
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 1707
|
Part 1: 1707
|
||||||
|
Part 2: 3697
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,25 @@
|
||||||
|
use core::panic;
|
||||||
|
use std::collections::{HashSet, VecDeque};
|
||||||
|
|
||||||
|
fn scan(input: &str, lookback: usize) -> usize {
|
||||||
|
let mut last_n = VecDeque::with_capacity(lookback);
|
||||||
|
for (i, c) in input.chars().enumerate() {
|
||||||
|
last_n.push_back(c);
|
||||||
|
while last_n.len() > lookback {
|
||||||
|
last_n.pop_front();
|
||||||
|
}
|
||||||
|
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) {
|
||||||
let part1 = input
|
println!("Part 1: {}", scan(&input, 4) + 1);
|
||||||
.chars()
|
println!("Part 2: {}", scan(&input, 14) + 1);
|
||||||
.zip(input.chars().skip(1))
|
|
||||||
.zip(input.chars().skip(2))
|
|
||||||
.zip(input.char_indices().skip(3))
|
|
||||||
.find_map(|(((c1, c2), c3), (i, c4))| {
|
|
||||||
if c1 != c2 && c1 != c3 && c1 != c4 && c2 != c3 && c2 != c4 && c3 != c4 {
|
|
||||||
println!("{c1}{c2}{c3}{c4}");
|
|
||||||
Some(i + 1)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
println!("Part 1: {part1}");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 7
|
Part 1: 7
|
||||||
|
Part 2: 19
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 5
|
Part 1: 5
|
||||||
|
Part 2: 23
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 6
|
Part 1: 6
|
||||||
|
Part 2: 23
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 10
|
Part 1: 10
|
||||||
|
Part 2: 29
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 11
|
Part 1: 11
|
||||||
|
Part 2: 26
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue