Skip to content

Instantly share code, notes, and snippets.

@gastonfartek
Last active December 7, 2021 01:41
Show Gist options
  • Save gastonfartek/bca78ecc9bbffa392b1d3361596dc811 to your computer and use it in GitHub Desktop.
Save gastonfartek/bca78ecc9bbffa392b1d3361596dc811 to your computer and use it in GitHub Desktop.
adventofcode day6 part2
use std::collections::HashMap;
static DATA: &str = "3,5,1,2,5,4,1,5,1,2,5,5,1,3,1,5,1,3,2,1,5,1,1,1,2,3,1,3,1,2,1,1,5,1,5,4,5,5,3,3,1,5,1,1,5,5,1,3,5,5,3,2,2,4,1,5,3,4,2,5,4,1,2,2,5,1,1,2,4,4,1,3,1,3,1,1,2,2,1,1,5,1,1,4,4,5,5,1,2,1,4,1,1,4,4,3,4,2,2,3,3,2,1,3,3,2,1,1,1,2,1,4,2,2,1,5,5,3,4,5,5,2,5,2,2,5,3,3,1,2,4,2,1,5,1,1,2,3,5,5,1,1,5,5,1,4,5,3,5,2,3,2,4,3,1,4,2,5,1,3,2,1,1,3,4,2,1,1,1,1,2,1,4,3,1,3,1,2,4,1,2,4,3,2,3,5,5,3,3,1,2,3,4,5,2,4,5,1,1,1,4,5,3,5,3,5,1,1,5,1,5,3,1,2,3,4,1,1,4,1,2,4,1,5,4,1,5,4,2,1,5,2,1,3,5,5,4,5,5,1,1,4,1,2,3,5,3,3,1,1,1,4,3,1,1,4,1,5,3,5,1,4,2,5,1,1,4,4,4,2,5,1,2,5,2,1,3,1,5,1,2,1,1,5,2,4,2,1,3,5,5,4,1,1,1,5,5,2,1,1";
fn main() {
let input: Vec<i8> = DATA.split(",").map(|x| x.parse::<i8>().unwrap()).collect();
let mut fish_per_day_map: HashMap<i8, i64> = HashMap::new();
for day in input {
*fish_per_day_map.entry(day).or_insert(0) += 1;
}
let mut counter = 0;
while counter < 256 {
let mut new_ones: HashMap<i8, i64> = HashMap::new();
for (&key, _) in &fish_per_day_map {
new_ones.entry(key).or_insert(0);
let number_of_fish_to_add = fish_per_day_map.get(&key).unwrap();
if key == 0 {
*new_ones.entry(6).or_insert(0) += *number_of_fish_to_add;
*new_ones.entry(8).or_insert(0) += *number_of_fish_to_add;
} else {
*new_ones.entry(key - 1).or_insert(0) += *number_of_fish_to_add;
}
}
counter += 1;
fish_per_day_map = new_ones
}
let mut total: i64 = 0;
for value in fish_per_day_map.values() {
total += value
}
println!("{}", total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment