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 }