From 89525d6be22e4e6b2124b93fa2df833e3e201ba1 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 7 Dec 2021 01:04:09 +0100 Subject: [PATCH] Added day 6 code for part 1 and 2 Signed-off-by: Louis Vallat --- day6/Cargo.toml | 8 ++++++++ day6/input | 1 + day6/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 day6/Cargo.toml create mode 100644 day6/input create mode 100644 day6/src/main.rs diff --git a/day6/Cargo.toml b/day6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/day6/Cargo.toml @@ -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] diff --git a/day6/input b/day6/input new file mode 100644 index 0000000..70f9ad7 --- /dev/null +++ b/day6/input @@ -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 diff --git a/day6/src/main.rs b/day6/src/main.rs new file mode 100644 index 0000000..9fd9022 --- /dev/null +++ b/day6/src/main.rs @@ -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 { + return s.replace("\n", "").split(",") + .filter_map(|e| e.parse::().ok()).collect(); +} + +fn smart_compute_x_days(v: &Vec, x: i32) -> i128 { + let mut l: Vec = 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 = 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)); + } +} +