feat: solve day 6, part 1 & 2
This commit is contained in:
parent
4727416702
commit
cb974c7daa
1
input/6.txt
Normal file
1
input/6.txt
Normal file
@ -0,0 +1 @@
|
||||
3,1,4,2,1,1,1,1,1,1,1,4,1,4,1,2,1,1,2,1,3,4,5,1,1,4,1,3,3,1,1,1,1,3,3,1,3,3,1,5,5,1,1,3,1,1,2,1,1,1,3,1,4,3,2,1,4,3,3,1,1,1,1,5,1,4,1,1,1,4,1,4,4,1,5,1,1,4,5,1,1,2,1,1,1,4,1,2,1,1,1,1,1,1,5,1,3,1,1,4,4,1,1,5,1,2,1,1,1,1,5,1,3,1,1,1,2,2,1,4,1,3,1,4,1,2,1,1,1,1,1,3,2,5,4,4,1,3,2,1,4,1,3,1,1,1,2,1,1,5,1,2,1,1,1,2,1,4,3,1,1,1,4,1,1,1,1,1,2,2,1,1,5,1,1,3,1,2,5,5,1,4,1,1,1,1,1,2,1,1,1,1,4,5,1,1,1,1,1,1,1,1,1,3,4,4,1,1,4,1,3,4,1,5,4,2,5,1,2,1,1,1,1,1,1,4,3,2,1,1,3,2,5,2,5,5,1,3,1,2,1,1,1,1,1,1,1,1,1,3,1,1,1,3,1,4,1,4,2,1,3,4,1,1,1,2,3,1,1,1,4,1,2,5,1,2,1,5,1,1,2,1,2,1,1,1,1,4,3,4,1,5,5,4,1,1,5,2,1,3
|
81
src/day6.rs
Normal file
81
src/day6.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::num::ParseIntError;
|
||||
|
||||
pub fn parse_input(input: &str) -> Result<Vec<i32>, ParseIntError> {
|
||||
input.trim_end().split_terminator(',')
|
||||
.map(|s| s.parse())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn simulate_step(fishes: &mut Vec<i32>) {
|
||||
for i in 0..fishes.len() {
|
||||
if fishes[i] == 0 {
|
||||
fishes[i] = 7;
|
||||
fishes.push(9);
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..fishes.len() {
|
||||
if fishes[i] > 0 {
|
||||
fishes[i] -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part1(input: &[i32]) -> u32 {
|
||||
let mut fishes = input.to_vec();
|
||||
|
||||
for _ in 0..80 {
|
||||
simulate_step(&mut fishes)
|
||||
}
|
||||
|
||||
fishes.len() as u32
|
||||
}
|
||||
|
||||
// Instead of storing each fishes cycle as individual values group them up
|
||||
// by there cycles. Because it dosen't matter where the fish is in the list.
|
||||
// So just make an array of size 9 for the 9 possible fish cycle timers.
|
||||
// And one extra group, for accounting for the delay that when the timer is 0,
|
||||
// they produce a new fish only on the next turn.
|
||||
pub fn part2(input: &[i32]) -> u64 {
|
||||
let mut groups: [u64; 10] = [0; 10];
|
||||
|
||||
for fish in input.iter() {
|
||||
groups[*fish as usize + 1] += 1;
|
||||
}
|
||||
|
||||
for _ in 0..256 {
|
||||
for i in 1..10 {
|
||||
groups[i-1] += groups[i];
|
||||
groups[i] = 0;
|
||||
}
|
||||
groups[7] += groups[0];
|
||||
groups[9] += groups[0];
|
||||
groups[0] = 0;
|
||||
}
|
||||
|
||||
let mut count: u64 = 0;
|
||||
for amount in groups.iter() {
|
||||
count += amount;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
let input = [3,4,3,1,2];
|
||||
let result = part1(&input);
|
||||
assert_eq!(result, 5934);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
let input = [3,4,3,1,2];
|
||||
let result = part2(&input);
|
||||
assert_eq!(result, 26984457539 as u64);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
mod day6;
|
||||
|
||||
use std::{env, process};
|
||||
use std::fs::File;
|
||||
|
Loading…
Reference in New Issue
Block a user