[rs] Solve 2022_06 part 2

This commit is contained in:
Joscha 2022-12-06 09:50:12 +01:00
parent 889410ee83
commit a2a69a373a
7 changed files with 30 additions and 16 deletions

View file

@ -1 +1,2 @@
Part 1: 1707
Part 2: 3697

View file

@ -1,17 +1,25 @@
pub fn solve(input: String) {
let part1 = input
.chars()
.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
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();
}
})
.unwrap();
println!("Part 1: {part1}");
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) {
println!("Part 1: {}", scan(&input, 4) + 1);
println!("Part 2: {}", scan(&input, 14) + 1);
}

View file

@ -1 +1,2 @@
Part 1: 7
Part 2: 19

View file

@ -1 +1,2 @@
Part 1: 5
Part 2: 23

View file

@ -1 +1,2 @@
Part 1: 6
Part 2: 23

View file

@ -1 +1,2 @@
Part 1: 10
Part 2: 29

View file

@ -1 +1,2 @@
Part 1: 11
Part 2: 26