1
0
Fork 0

day(06): add initial solution

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-06 10:08:24 +01:00
parent 4bbb98661c
commit 65ac67b459
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 119 additions and 0 deletions

1
samples/day06.txt Normal file
View file

@ -0,0 +1 @@
mjqjpqmgbljsphdztnvjfqwrcgsmlb

118
src/bin/day06.rs Normal file
View file

@ -0,0 +1,118 @@
use std::collections::HashSet;
use std::str::FromStr;
use aoc_2022::*;
use color_eyre::eyre::{Report, Result};
use itertools::Itertools;
use tracing::*;
use tracing_subscriber::EnvFilter;
type Input = String;
type Output = usize;
fn part_1(input: &str) -> Output {
let chars: Vec<_> = input.chars().collect();
chars
.windows(4)
.enumerate()
.map(|(i, chars)| (i, chars.iter().collect::<HashSet<_>>()))
.find_or_first(|(_, s)| s.len() == 4)
.unwrap()
.0
+ 4
}
fn part_2(input: &Input) -> Output {
let chars: Vec<_> = input.chars().collect();
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 {
file_to_string(pathname)
}
fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_target(false)
.with_file(true)
.with_line_number(true)
.without_time()
.compact()
.init();
color_eyre::install()?;
let input = parse_input("inputs/day06.txt");
info!("Part 1: {}", part_1(&input));
info!("Part 2: {}", part_2(&input));
Ok(())
}
#[cfg(test)]
mod day_06 {
use super::*;
#[test]
fn test_part_1() {
let sample = parse_input("samples/day06.txt");
assert_eq!(part_1(&sample), 7);
}
#[test]
fn test_part_1_example_1() {
assert_eq!(part_1(&"bvwbjplbgvbhsrlpgdmjqwftvncz".to_string()), 5);
}
#[test]
fn test_part_1_example_2() {
assert_eq!(part_1(&"nppdvjthqldpwncqszvftbrmjlhg".to_string()), 6);
}
#[test]
fn test_part_1_example_3() {
assert_eq!(part_1(&"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg".to_string()), 10);
}
#[test]
fn test_part_1_example_4() {
assert_eq!(part_1(&"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw".to_string()), 11);
}
#[test]
fn test_part_2() {
let sample = parse_input("samples/day06.txt");
assert_eq!(part_2(&sample), 19);
}
#[test]
fn test_part_2_example_1() {
assert_eq!(part_2(&"bvwbjplbgvbhsrlpgdmjqwftvncz".to_string()), 23);
}
#[test]
fn test_part_2_example_2() {
assert_eq!(part_2(&"nppdvjthqldpwncqszvftbrmjlhg".to_string()), 23);
}
#[test]
fn test_part_2_example_3() {
assert_eq!(part_2(&"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg".to_string()), 29);
}
#[test]
fn test_part_2_example_4() {
assert_eq!(part_2(&"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw".to_string()), 26);
}
}