Rewrote code for part 2

This commit is contained in:
2021-12-07 00:25:05 +01:00
parent 8e0fe281e2
commit 6983ebdf26

View File

@@ -1,40 +1,48 @@
use std::{fs::read_to_string};
const ITERATIONS: u32 = 256;
const NEW_FISH_LIFETIME: u8 = 8;
fn main() {
let input_char_vec = read_to_string("./input.txt").expect("ERROR reading file");
let split_input = input_char_vec.split(",").collect::<Vec<&str>>();
let mut fish: Vec<u8> = split_input.iter().map(
let starting_fishes: Vec<u8> = split_input.iter().map(
|x| x.parse::<u8>().unwrap()
).collect();
print_fish(&fish, -1);
let mut fish: Vec<u64> = vec![0,0,0,0,0,0,0,0,0];
for _i in 1..ITERATIONS + 1 {
for fi in 0..fish.len() {
let current_timer = fish[fi];
if current_timer == 0 {
fish[fi] = 6;
fish.push(NEW_FISH_LIFETIME);
} else {
fish[fi] = current_timer - 1;
}
}
println!("Interation {} of {}, {}", _i, ITERATIONS, fish.len());
//print_fish(&fish, _i as i32);
for fi in starting_fishes{
fish[fi as usize] = fish[fi as usize] + 1;
}
print_fish(&fish, -1);
println!("Final Number of Fish: {}", fish.len());
for _i in 1..ITERATIONS + 1 {
let zero_fish = fish[0];
for timer in 1..=8 {
fish[timer-1] = fish[timer];
}
fish[6] = fish[6] + zero_fish;
fish[8] = zero_fish;
print_fish(&fish, _i as i32);
}
let mut sum: u128 = 0;
for f in fish {
sum += f as u128;
}
println!("Final Number of Fish: {}", sum);
}
fn print_fish(fish: &Vec<u8>, iteration: i32){
fn print_fish(fish: &Vec<u64>, iteration: i32){
let fish_copy = fish.to_owned();
print!("Iteration: {}, Fish: ", iteration);
for f in fish_copy {
print!("{}, ", f);
for f in 0..fish_copy.len() {
print!("{},{} ", f, fish_copy[f]);
}
print!("\n");
}