Added day 6 code for part 1 and 2

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-12-07 01:04:09 +01:00
parent 24539f0f2a
commit 89525d6be2
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
3 changed files with 47 additions and 0 deletions

8
day6/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day6"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
day6/input Normal file
View File

@ -0,0 +1 @@
1,4,3,3,1,3,1,1,1,2,1,1,1,4,4,1,5,5,3,1,3,5,2,1,5,2,4,1,4,5,4,1,5,1,5,5,1,1,1,4,1,5,1,1,1,1,1,4,1,2,5,1,4,1,2,1,1,5,1,1,1,1,4,1,5,1,1,2,1,4,5,1,2,1,2,2,1,1,1,1,1,5,5,3,1,1,1,1,1,4,2,4,1,2,1,4,2,3,1,4,5,3,3,2,1,1,5,4,1,1,1,2,1,1,5,4,5,1,3,1,1,1,1,1,1,2,1,3,1,2,1,1,1,1,1,1,1,2,1,1,1,1,2,1,1,1,1,1,1,4,5,1,3,1,4,4,2,3,4,1,1,1,5,1,1,1,4,1,5,4,3,1,5,1,1,1,1,1,5,4,1,1,1,4,3,1,3,3,1,3,2,1,1,3,1,1,4,5,1,1,1,1,1,3,1,4,1,3,1,5,4,5,1,1,5,1,1,4,1,1,1,3,1,1,4,2,3,1,1,1,1,2,4,1,1,1,1,1,2,3,1,5,5,1,4,1,1,1,1,3,3,1,4,1,2,1,3,1,1,1,3,2,2,1,5,1,1,3,2,1,1,5,1,1,1,1,1,1,1,1,1,1,2,5,1,1,1,1,3,1,1,1,1,1,1,1,1,5,5,1

38
day6/src/main.rs Normal file
View File

@ -0,0 +1,38 @@
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));
}
}