From 65ac67b459c967e2840bb6e34762d38dcf07837c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 6 Dec 2022 10:08:24 +0100 Subject: [PATCH] day(06): add initial solution Signed-off-by: Matej Focko --- samples/day06.txt | 1 + src/bin/day06.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 samples/day06.txt create mode 100644 src/bin/day06.rs diff --git a/samples/day06.txt b/samples/day06.txt new file mode 100644 index 0000000..5a2b0a7 --- /dev/null +++ b/samples/day06.txt @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb \ No newline at end of file diff --git a/src/bin/day06.rs b/src/bin/day06.rs new file mode 100644 index 0000000..d3ec75c --- /dev/null +++ b/src/bin/day06.rs @@ -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::>())) + .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::>())) + .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); + } +}