From d35e9844e05aab36744f493756516a35e2ec9753 Mon Sep 17 00:00:00 2001 From: pjht Date: Tue, 6 Dec 2022 08:23:57 -0600 Subject: [PATCH] Day 6 --- src/day06.rs | 32 ++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 33 insertions(+) create mode 100644 src/day06.rs diff --git a/src/day06.rs b/src/day06.rs new file mode 100644 index 0000000..10201f2 --- /dev/null +++ b/src/day06.rs @@ -0,0 +1,32 @@ +use aoc_runner_derive::aoc; + +fn check_unique(input: &[char]) -> bool { + for i in 0..input.len() - 1 { + if input[i + 1..input.len()].contains(&input[i]) { + return false; + } + } + true +} + +#[aoc(day6, part1)] +fn solve_part1(input: &str) -> usize { + let chars = input.chars().collect::>(); + for (i, window) in chars.windows(4).enumerate() { + if check_unique(window) { + return i + 4; + } + } + panic!(); +} + +#[aoc(day6, part2)] +fn solve_part2(input: &str) -> usize { + let chars = input.chars().collect::>(); + for (i, window) in chars.windows(14).enumerate() { + if check_unique(window) { + return i + 14; + } + } + panic!(); +} diff --git a/src/lib.rs b/src/lib.rs index 2fe9df9..2eb43e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,5 +5,6 @@ mod day02; mod day03; mod day04; mod day05; +mod day06; aoc_lib! { year = 2022 }