Rust Day 6 - Another solution

This commit is contained in:
Tobias Berger 2022-12-06 14:54:06 +01:00
parent 24f05860a2
commit 24b28937f9
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
4 changed files with 105 additions and 17 deletions

View file

@ -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)
);
}

View file

@ -287,23 +287,46 @@ mod tests {
}
#[bench]
fn bench_vertesians_nodeps_const(bencher: &mut test::Bencher) {
bencher.iter(|| solve_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT));
fn bench_nicopap_vertesians_nodeps_const(bencher: &mut test::Bencher) {
bencher.iter(|| solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT));
}
#[test]
fn test_vertesians_nodeps_const_with_solution() {
fn test_nicopap_vertesians_nodeps_const_with_solution() {
assert_eq!(
solve_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT),
solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(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::<WINDOW_SIZE>(sample_input),
solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(sample_input),
sample_answer
)
}
}
#[bench]
fn bench_vertesians_3_1(bencher: &mut test::Bencher) {
bencher.iter(|| solve_vertesians_nodeps_3_1::<WINDOW_SIZE>(crate::INPUT));
}
#[test]
fn test_vertesians_3_1_with_solution() {
assert_eq!(
solve_vertesians_nodeps_3_1::<WINDOW_SIZE>(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::<WINDOW_SIZE>(sample_input),
sample_answer
)
}

View file

@ -287,23 +287,46 @@ mod tests {
}
#[bench]
fn bench_vertesians_nodeps_const(bencher: &mut test::Bencher) {
bencher.iter(|| solve_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT));
fn bench_nicopap_vertesians_nodeps_const(bencher: &mut test::Bencher) {
bencher.iter(|| solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT));
}
#[test]
fn test_vertesians_nodeps_const_with_solution() {
fn test_nicopap_vertesians_nodeps_const_with_solution() {
assert_eq!(
solve_vertesians_nodeps_const::<WINDOW_SIZE>(crate::INPUT),
solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(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::<WINDOW_SIZE>(sample_input),
solve_nicopap_vertesians_nodeps_const::<WINDOW_SIZE>(sample_input),
sample_answer
)
}
}
#[bench]
fn bench_vertesians_3_1(bencher: &mut test::Bencher) {
bencher.iter(|| solve_vertesians_nodeps_3_1::<WINDOW_SIZE>(crate::INPUT));
}
#[test]
fn test_vertesians_3_1_with_solution() {
assert_eq!(
solve_vertesians_nodeps_3_1::<WINDOW_SIZE>(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::<WINDOW_SIZE>(sample_input),
sample_answer
)
}

View file

@ -482,17 +482,17 @@ mod vertesians_nodeps_improved {
}
}
pub const fn solve_vertesians_nodeps_const<const N: usize>(input: &'static str) -> usize {
pub const fn solve_nicopap_vertesians_nodeps_const<const N: usize>(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::<N>(input.as_bytes())
nicopap_vertesians_nodeps_const::process::<N>(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<const N: usize>(input: &'static str) -> usize {
vertesians_nodeps_3_1::main::<N>(input)
}
mod vertesians_nodeps_3_1 {
pub(super) fn main<const N: usize>(input: &'static str) -> usize {
let data = input
.chars()
.map(|c| 1 << (c as u32 - 0x61))
.collect::<Vec<u32>>();
assert!(!data.is_empty(), "Received empty input");
process::<N>(data)
}
fn process<const N: usize>(data: Vec<u32>) -> 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");
}
}