|
| 1 | +//! Day 6: Tuning Trouble |
| 2 | +//! |
| 3 | +//! solution to the day 06 of AoC2022 |
| 4 | +//! |
| 5 | +//! https://adventofcode.com/2022/day/6 |
| 6 | +
|
| 7 | +use { |
| 8 | + crate::helpers::{read_lines, Answer}, |
| 9 | + std::time::SystemTime, |
| 10 | +}; |
| 11 | + |
| 12 | +/// solves the part 1 of day 05 and return its result and elapsed time |
| 13 | +pub fn pt1(filename: &str) -> Answer { |
| 14 | + let time = SystemTime::now(); |
| 15 | + |
| 16 | + let signal = read_lines(filename).next().expect("could not read input"); |
| 17 | + |
| 18 | + let answer = end_of_marker(&signal, 4); |
| 19 | + |
| 20 | + Answer::new( |
| 21 | + answer.to_string(), |
| 22 | + time.elapsed().unwrap().as_millis() as u32, |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +/// solves the part 2 of day 05 and return its result and elapsed time |
| 27 | +pub fn pt2(filename: &str) -> Answer { |
| 28 | + let time = SystemTime::now(); |
| 29 | + |
| 30 | + let signal = read_lines(filename).next().expect("could not read input"); |
| 31 | + |
| 32 | + let answer = end_of_marker(&signal, 14); |
| 33 | + |
| 34 | + Answer::new( |
| 35 | + answer.to_string(), |
| 36 | + time.elapsed().unwrap().as_millis() as u32, |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +/// The position of the last digit of the first `size` unique character sequence |
| 41 | +fn end_of_marker(signal: &str, size: usize) -> usize { |
| 42 | + signal |
| 43 | + .chars() |
| 44 | + .collect::<Vec<char>>() |
| 45 | + .windows(size) |
| 46 | + .enumerate() |
| 47 | + .find(|(_, window)| !(1..size).any(|i| window.iter().skip(i).any(|c| *c == window[i - 1]))) |
| 48 | + .expect("could not find marker") |
| 49 | + .0 |
| 50 | + + size |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::{end_of_marker, pt1, pt2}; |
| 56 | + |
| 57 | + const FILENAME: &str = "./data/examples/example06.txt"; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn pt01() { |
| 61 | + let answer = pt1(FILENAME); |
| 62 | + assert_eq!(String::from("7"), answer.value()); |
| 63 | + } |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn pt02() { |
| 67 | + let answer = pt2(FILENAME); |
| 68 | + assert_eq!(String::from("19"), answer.value()); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_end_of_marker() { |
| 73 | + let example_1 = String::from("bvwbjplbgvbhsrlpgdmjqwftvncz"); |
| 74 | + let example_2 = String::from("nppdvjthqldpwncqszvftbrmjlhg"); |
| 75 | + let example_3 = String::from("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"); |
| 76 | + let example_4 = String::from("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"); |
| 77 | + |
| 78 | + assert_eq!(5, end_of_marker(&example_1, 4)); |
| 79 | + assert_eq!(6, end_of_marker(&example_2, 4)); |
| 80 | + assert_eq!(10, end_of_marker(&example_3, 4)); |
| 81 | + assert_eq!(11, end_of_marker(&example_4, 4)); |
| 82 | + |
| 83 | + assert_eq!(23, end_of_marker(&example_1, 14)); |
| 84 | + assert_eq!(23, end_of_marker(&example_2, 14)); |
| 85 | + assert_eq!(29, end_of_marker(&example_3, 14)); |
| 86 | + assert_eq!(26, end_of_marker(&example_4, 14)); |
| 87 | + } |
| 88 | +} |
0 commit comments