Added day 6 code for part 1 and 2
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
24539f0f2a
commit
89525d6be2
8
day6/Cargo.toml
Normal file
8
day6/Cargo.toml
Normal 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
1
day6/input
Normal 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
38
day6/src/main.rs
Normal 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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user