diff options
-rw-r--r-- | data/2021/6.txt | 1 | ||||
-rw-r--r-- | src/2021/6/mod.rs | 41 | ||||
-rw-r--r-- | src/2021/mod.rs | 4 |
3 files changed, 46 insertions, 0 deletions
diff --git a/data/2021/6.txt b/data/2021/6.txt new file mode 100644 index 0000000..70f9ad7 --- /dev/null +++ b/data/2021/6.txt @@ -0,0 +1 @@ +1,4,3,3,1,3,1,1,1,2,1,1,1,4,4,1,5,5,3,1,3,5,2,1,5,2,4,1,4,5,4,1,5,1,5,5,1,1,1,4,1,5,1,1,1,1,1,4,1,2,5,1,4,1,2,1,1,5,1,1,1,1,4,1,5,1,1,2,1,4,5,1,2,1,2,2,1,1,1,1,1,5,5,3,1,1,1,1,1,4,2,4,1,2,1,4,2,3,1,4,5,3,3,2,1,1,5,4,1,1,1,2,1,1,5,4,5,1,3,1,1,1,1,1,1,2,1,3,1,2,1,1,1,1,1,1,1,2,1,1,1,1,2,1,1,1,1,1,1,4,5,1,3,1,4,4,2,3,4,1,1,1,5,1,1,1,4,1,5,4,3,1,5,1,1,1,1,1,5,4,1,1,1,4,3,1,3,3,1,3,2,1,1,3,1,1,4,5,1,1,1,1,1,3,1,4,1,3,1,5,4,5,1,1,5,1,1,4,1,1,1,3,1,1,4,2,3,1,1,1,1,2,4,1,1,1,1,1,2,3,1,5,5,1,4,1,1,1,1,3,3,1,4,1,2,1,3,1,1,1,3,2,2,1,5,1,1,3,2,1,1,5,1,1,1,1,1,1,1,1,1,1,2,5,1,1,1,1,3,1,1,1,1,1,1,1,1,5,5,1 diff --git a/src/2021/6/mod.rs b/src/2021/6/mod.rs new file mode 100644 index 0000000..ddf7855 --- /dev/null +++ b/src/2021/6/mod.rs @@ -0,0 +1,41 @@ +pub fn part1() -> anyhow::Result<i64> { + let mut fishes: Vec<u8> = data_lines!()? + .next() + .unwrap()? + .split(',') + .map(|s| s.parse()) + .collect::<Result<_, _>>()?; + for _ in 0..80 { + let mut new = 0; + for fish in fishes.iter_mut() { + if *fish == 0 { + *fish = 6; + new += 1; + } else { + *fish -= 1; + } + } + fishes.resize(fishes.len() + new, 8); + } + Ok(fishes.len().try_into()?) +} + +pub fn part2() -> anyhow::Result<i64> { + let fishes: Vec<u8> = data_lines!()? + .next() + .unwrap()? + .split(',') + .map(|s| s.parse()) + .collect::<Result<_, _>>()?; + let mut by_age = std::collections::VecDeque::new(); + by_age.resize(9, 0); + for fish in fishes { + by_age[fish as usize] += 1; + } + for _ in 0..256 { + let new = by_age.pop_front().unwrap(); + by_age[6] += new; + by_age.push_back(new); + } + Ok(by_age.iter().sum()) +} diff --git a/src/2021/mod.rs b/src/2021/mod.rs index a84c726..f4bc3c5 100644 --- a/src/2021/mod.rs +++ b/src/2021/mod.rs @@ -8,6 +8,8 @@ mod day3; mod day4; #[path = "5/mod.rs"] mod day5; +#[path = "6/mod.rs"] +mod day6; // NEXT MOD pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> { @@ -22,6 +24,8 @@ pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> { (4, 2) => day4::part2(), (5, 1) => day5::part1(), (5, 2) => day5::part2(), + (6, 1) => day6::part1(), + (6, 2) => day6::part2(), // NEXT PART _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)), } |