advent-of-code-2021/day6/src/main.rs
Louis Vallat 89525d6be2
Added day 6 code for part 1 and 2
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
2021-12-07 01:04:09 +01:00

39 lines
962 B
Rust

use std::{fs, env};
fn read_input(path: &str) -> String {
return fs::read_to_string(path).expect("Cannot read file.");
}
fn parse_input(s: &str) -> Vec<i32> {
return s.replace("\n", "").split(",")
.filter_map(|e| e.parse::<i32>().ok()).collect();
}
fn smart_compute_x_days(v: &Vec<i32>, x: i32) -> i128 {
let mut l: Vec<i128> = vec![0;9];
for i in v {
l[*i as usize] += 1;
}
for _d in 1..=x {
let n = l[0];
for i in 1..9 {
l[i - 1] = l[i];
}
l[6] += n;
l[8] = n;
}
return l.iter().sum();
}
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter().skip(1) {
let vec_in = parse_input(&read_input(&arg));
println!("[{}]", &arg);
println!("\t[Part 1] => Answer is '{}'.", smart_compute_x_days(&vec_in, 80));
println!("\t[Part 2] => Answer is '{}'.", smart_compute_x_days(&vec_in, 256));
}
}