low memory rework for day6 part 2
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
blackbeard420 2021-12-10 14:18:12 -05:00
parent a613072c49
commit 9136b1e64d
Signed by: blackbeard420
GPG Key ID: 88C719E09CDDA4A5

View File

@ -1,5 +1,6 @@
use std::fs;
use std::io::BufRead;
use std::collections::HashMap;
static PATH: &'static str = "../../inputs/input-day6";
@ -41,15 +42,50 @@ fn part1() {
println!("total: {}", x.len());
}
fn part2() {
let mut x = load_file().unwrap();
fn get_total(v: &HashMap<u8, u64>) -> u64 {
let mut total = 0;
for i in 0..256 {
run_day(&mut x);
println!("day: {} fish: {}", i+1, x.len());
for x in v {
total += x.1;
}
println!("total: {}", x.len());
return total;
}
fn run_day_2(v: &HashMap<u8, u64>) -> HashMap<u8, u64> {
let mut state = HashMap::new();
for i in (0..=8).rev() {
if v.contains_key(&i) {
if let Some(cnt) = v.get(&i) {
if i == 0 {
let c = state.entry(6).or_insert(0);
*c += cnt;
state.insert(8, *cnt);
} else {
state.insert(i - 1, *cnt);
}
}
}
}
return state;
}
fn part2() {
let x = load_file().unwrap();
let mut fish = HashMap::<u8, u64>::new();
for val in x {
let n = fish.entry(val).or_insert(0);
*n += 1;
}
for _ in 0..256 {
fish = run_day_2(&fish);
}
println!("total: {}", get_total(&fish));
}
fn main() {