Rewrote code for part 2
This commit is contained in:
@@ -1,40 +1,48 @@
|
|||||||
use std::{fs::read_to_string};
|
use std::{fs::read_to_string};
|
||||||
|
|
||||||
const ITERATIONS: u32 = 256;
|
const ITERATIONS: u32 = 256;
|
||||||
const NEW_FISH_LIFETIME: u8 = 8;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input_char_vec = read_to_string("./input.txt").expect("ERROR reading file");
|
let input_char_vec = read_to_string("./input.txt").expect("ERROR reading file");
|
||||||
let split_input = input_char_vec.split(",").collect::<Vec<&str>>();
|
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()
|
|x| x.parse::<u8>().unwrap()
|
||||||
).collect();
|
).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 starting_fishes{
|
||||||
for fi in 0..fish.len() {
|
fish[fi as usize] = fish[fi as usize] + 1;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
let fish_copy = fish.to_owned();
|
||||||
print!("Iteration: {}, Fish: ", iteration);
|
print!("Iteration: {}, Fish: ", iteration);
|
||||||
for f in fish_copy {
|
for f in 0..fish_copy.len() {
|
||||||
print!("{}, ", f);
|
print!("{},{} ", f, fish_copy[f]);
|
||||||
}
|
}
|
||||||
print!("\n");
|
print!("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user