diff --git a/rust/src/day06/main.rs b/rust/src/day06/main.rs index a1f9873..9000a4a 100644 --- a/rust/src/day06/main.rs +++ b/rust/src/day06/main.rs @@ -12,6 +12,12 @@ mod part_2; mod solutions; pub fn main() { - println!("Part 1: {}", solve_vertesians_nodeps_const::<4>(INPUT)); - println!("Part 2: {}", solve_vertesians_nodeps_const::<14>(INPUT)); + println!( + "Part 1: {}", + solve_nicopap_vertesians_nodeps_const::<4>(INPUT) + ); + println!( + "Part 2: {}", + solve_nicopap_vertesians_nodeps_const::<14>(INPUT) + ); } diff --git a/rust/src/day06/part_1.rs b/rust/src/day06/part_1.rs index 7e887fd..c11d54c 100644 --- a/rust/src/day06/part_1.rs +++ b/rust/src/day06/part_1.rs @@ -287,23 +287,46 @@ mod tests { } #[bench] - fn bench_vertesians_nodeps_const(bencher: &mut test::Bencher) { - bencher.iter(|| solve_vertesians_nodeps_const::(crate::INPUT)); + fn bench_nicopap_vertesians_nodeps_const(bencher: &mut test::Bencher) { + bencher.iter(|| solve_nicopap_vertesians_nodeps_const::(crate::INPUT)); } #[test] - fn test_vertesians_nodeps_const_with_solution() { + fn test_nicopap_vertesians_nodeps_const_with_solution() { assert_eq!( - solve_vertesians_nodeps_const::(crate::INPUT), + solve_nicopap_vertesians_nodeps_const::(crate::INPUT), 1723 ) } #[test] - fn test_vertesians_nodeps_const_with_sample_solutions() { + fn test_nicopap_vertesians_nodeps_const_with_sample_solutions() { for (sample_input, sample_answer) in SAMPLES { assert_eq!( - solve_vertesians_nodeps_const::(sample_input), + solve_nicopap_vertesians_nodeps_const::(sample_input), + sample_answer + ) + } + } + + #[bench] + fn bench_vertesians_3_1(bencher: &mut test::Bencher) { + bencher.iter(|| solve_vertesians_nodeps_3_1::(crate::INPUT)); + } + + #[test] + fn test_vertesians_3_1_with_solution() { + assert_eq!( + solve_vertesians_nodeps_3_1::(crate::INPUT), + 1723 + ) + } + + #[test] + fn test_vertesians_3_1_with_sample_solutions() { + for (sample_input, sample_answer) in SAMPLES { + assert_eq!( + solve_vertesians_nodeps_3_1::(sample_input), sample_answer ) } diff --git a/rust/src/day06/part_2.rs b/rust/src/day06/part_2.rs index afd5eb6..c4f0e7e 100644 --- a/rust/src/day06/part_2.rs +++ b/rust/src/day06/part_2.rs @@ -287,23 +287,46 @@ mod tests { } #[bench] - fn bench_vertesians_nodeps_const(bencher: &mut test::Bencher) { - bencher.iter(|| solve_vertesians_nodeps_const::(crate::INPUT)); + fn bench_nicopap_vertesians_nodeps_const(bencher: &mut test::Bencher) { + bencher.iter(|| solve_nicopap_vertesians_nodeps_const::(crate::INPUT)); } #[test] - fn test_vertesians_nodeps_const_with_solution() { + fn test_nicopap_vertesians_nodeps_const_with_solution() { assert_eq!( - solve_vertesians_nodeps_const::(crate::INPUT), + solve_nicopap_vertesians_nodeps_const::(crate::INPUT), 3708 ) } #[test] - fn test_vertesians_nodeps_const_with_sample_solutions() { + fn test_nicopap_vertesians_nodeps_const_with_sample_solutions() { for (sample_input, sample_answer) in SAMPLES { assert_eq!( - solve_vertesians_nodeps_const::(sample_input), + solve_nicopap_vertesians_nodeps_const::(sample_input), + sample_answer + ) + } + } + + #[bench] + fn bench_vertesians_3_1(bencher: &mut test::Bencher) { + bencher.iter(|| solve_vertesians_nodeps_3_1::(crate::INPUT)); + } + + #[test] + fn test_vertesians_3_1_with_solution() { + assert_eq!( + solve_vertesians_nodeps_3_1::(crate::INPUT), + 3708 + ) + } + + #[test] + fn test_vertesians_3_1_with_sample_solutions() { + for (sample_input, sample_answer) in SAMPLES { + assert_eq!( + solve_vertesians_nodeps_3_1::(sample_input), sample_answer ) } diff --git a/rust/src/day06/solutions.rs b/rust/src/day06/solutions.rs index 3f6039e..8bdac2f 100644 --- a/rust/src/day06/solutions.rs +++ b/rust/src/day06/solutions.rs @@ -482,17 +482,17 @@ mod vertesians_nodeps_improved { } } -pub const fn solve_vertesians_nodeps_const(input: &'static str) -> usize { +pub const fn solve_nicopap_vertesians_nodeps_const(input: &'static str) -> usize { // For 0ns results // match N { // 4 => nicopap_vertesians_const::RESULT_PART_1, // 14 => nicopap_vertesians_const::RESULT_PART_2, // _ => unreachable!(), // } - nicopap_vertesians_const::process::(input.as_bytes()) + nicopap_vertesians_nodeps_const::process::(input.as_bytes()) } -mod nicopap_vertesians_const { +mod nicopap_vertesians_nodeps_const { pub(super) const RESULT_PART_1: usize = process::<4>(include_bytes!("./input.txt")); pub(super) const RESULT_PART_2: usize = process::<14>(include_bytes!("./input.txt")); @@ -526,3 +526,39 @@ mod nicopap_vertesians_const { flipped_count == (N as u32) } } + +pub fn solve_vertesians_nodeps_3_1(input: &'static str) -> usize { + vertesians_nodeps_3_1::main::(input) +} + +mod vertesians_nodeps_3_1 { + pub(super) fn main(input: &'static str) -> usize { + let data = input + .chars() + .map(|c| 1 << (c as u32 - 0x61)) + .collect::>(); + + assert!(!data.is_empty(), "Received empty input"); + + process::(data) + } + + fn process(data: Vec) -> usize { + for counter in 0.. { + let combined = { + let mut combined = 0; + for i in 0..N { + combined |= data[counter + i]; + } + combined + }; + + // Thanks to Gibonius for the suggestion to use u32::count_ones() + if combined.count_ones() == N as u32 { + return counter + N; + } + } + + panic!("No valid sequence found"); + } +}