1
0
Fork 0

day(06): refactor

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-06 10:15:19 +01:00
parent 65ac67b459
commit 33185bdae3
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,9 +1,8 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::str::FromStr;
use aoc_2022::*; use aoc_2022::*;
use color_eyre::eyre::{Report, Result}; use color_eyre::eyre::Result;
use itertools::Itertools; use itertools::Itertools;
use tracing::*; use tracing::*;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
@ -11,30 +10,25 @@ use tracing_subscriber::EnvFilter;
type Input = String; type Input = String;
type Output = usize; type Output = usize;
fn part_1(input: &str) -> Output { fn unique_marker_index(buffer: &Input, n: usize) -> Output {
let chars: Vec<_> = input.chars().collect(); let chars: Vec<_> = buffer.chars().collect();
chars chars
.windows(4) .windows(n)
.enumerate() .enumerate()
.map(|(i, chars)| (i, chars.iter().collect::<HashSet<_>>())) .map(|(i, chars)| (i, chars.iter().collect::<HashSet<_>>()))
.find_or_first(|(_, s)| s.len() == 4) .find_or_first(|(_, s)| s.len() == n)
.unwrap() .unwrap()
.0 .0
+ 4 + n
}
fn part_1(input: &Input) -> Output {
unique_marker_index(input, 4)
} }
fn part_2(input: &Input) -> Output { fn part_2(input: &Input) -> Output {
let chars: Vec<_> = input.chars().collect(); unique_marker_index(input, 14)
chars
.windows(14)
.enumerate()
.map(|(i, chars)| (i, chars.iter().collect::<HashSet<_>>()))
.find_or_first(|(_, s)| s.len() == 14)
.unwrap()
.0
+ 14
} }
fn parse_input(pathname: &str) -> Input { fn parse_input(pathname: &str) -> Input {