Complete day 6

This commit is contained in:
Tyler Hallada 2021-12-06 01:04:47 -05:00
parent 840f8547e8
commit d1cf74355f
5 changed files with 108 additions and 0 deletions

8
Cargo.lock generated
View File

@ -35,3 +35,11 @@ dependencies = [
"anyhow",
"common",
]
[[package]]
name = "day06"
version = "0.1.0"
dependencies = [
"anyhow",
"common",
]

10
crates/day06/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "day06"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0"
common = { path = "../common" }

View 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

View File

@ -0,0 +1 @@
3,4,3,1,2

88
crates/day06/src/main.rs Normal file
View File

@ -0,0 +1,88 @@
use anyhow::Result;
use common::instrument;
use std::collections::HashMap;
const INPUT: &str = include_str!("input/input.txt");
fn simulate_day(fish: &mut Vec<u8>) {
for i in 0..fish.len() {
if fish[i] == 0 {
fish[i] = 6;
fish.push(8)
} else {
fish[i] -= 1;
}
}
}
fn simulate_day_with_map(fish_map: &HashMap<u8, u64>) -> HashMap<u8, u64> {
let mut new_map = HashMap::new();
for (day, count) in fish_map.into_iter() {
if *day == 0 {
let day8 = new_map.entry(8).or_insert(0);
*day8 += count;
let day6 = new_map.entry(6).or_insert(0);
*day6 += count;
} else {
let day_minus = new_map.entry(day - 1).or_insert(0);
*day_minus += count;
}
}
new_map
}
fn solve_part1(input: &str) -> Result<usize> {
let mut fish = input
.trim()
.split(',')
.map(|num| num.parse::<u8>())
.collect::<Result<Vec<_>, std::num::ParseIntError>>()?;
for _ in 0..80 {
simulate_day(&mut fish);
}
Ok(fish.len())
}
fn solve_part2(input: &str) -> Result<u64> {
let fish = input
.trim()
.split(',')
.map(|num| num.parse::<u8>())
.collect::<Result<Vec<_>, std::num::ParseIntError>>()?;
let mut fish_map: HashMap<u8, u64> = HashMap::new();
for f in fish.iter() {
let count = fish_map.entry(*f).or_insert(0);
*count += 1;
}
for _ in 0..256 {
fish_map = simulate_day_with_map(&fish_map);
}
Ok(fish_map.values().sum())
}
fn main() {
instrument!(solve_part1(INPUT).unwrap(), solve_part2(INPUT).unwrap());
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_INPUT: &str = include_str!("input/test.txt");
#[test]
fn solves_part1() {
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 5934);
}
#[test]
fn solves_part2() {
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 26984457539);
}
}