Added Day 6 Part 2
This commit is contained in:
parent
38789fb184
commit
d9ce8d9531
32
src/main.rs
32
src/main.rs
|
@ -15,26 +15,36 @@ fn parse_input(input: &'static str) -> Vec<u8> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn solution(fishes: Vec<u8>) -> u64 {
|
||||
let mut day_stat = [0u64; 9];
|
||||
|
||||
fn solution<const N: usize>(fishes: Vec<u8>) -> u64 {
|
||||
let mut fish_stat = [0u64; 9];
|
||||
for fish in fishes {
|
||||
day_stat[fish as usize] += 1;
|
||||
fish_stat[fish as usize] += 1;
|
||||
}
|
||||
|
||||
for _ in 0..80 {
|
||||
day_stat.rotate_left(1);
|
||||
|
||||
day_stat[6] += day_stat[8];
|
||||
// Unrolled loop to improve performance
|
||||
let blocks = N / 9;
|
||||
for _ in 0..blocks {
|
||||
fish_stat[7] += fish_stat[0];
|
||||
fish_stat[8] += fish_stat[1];
|
||||
fish_stat[0] += fish_stat[2];
|
||||
fish_stat[1] += fish_stat[3];
|
||||
fish_stat[2] += fish_stat[4];
|
||||
fish_stat[3] += fish_stat[5];
|
||||
fish_stat[4] += fish_stat[6];
|
||||
fish_stat[5] += fish_stat[7];
|
||||
fish_stat[6] += fish_stat[8];
|
||||
}
|
||||
for day in blocks * 9..N {
|
||||
fish_stat[(day + 7) % 9] += fish_stat[day % 9];
|
||||
}
|
||||
|
||||
day_stat.into_iter().sum::<u64>()
|
||||
fish_stat.into_iter().sum::<u64>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for input in INPUTS {
|
||||
let input = parse_input(input);
|
||||
let result = solution(input);
|
||||
let result = solution::<256>(input);
|
||||
println!("Result {}", result);
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +53,7 @@ fn main() {
|
|||
fn solution_bench(b: &mut test::Bencher) {
|
||||
let input = parse_input(INPUTS[1]);
|
||||
b.iter(|| {
|
||||
let result = solution(input.clone());
|
||||
let result = solution::<256>(input.clone());
|
||||
test::black_box(result);
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue