From f420e96477066c76be8ab6edf0612d4de76dbb08 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Fri, 3 Dec 2021 21:48:01 +0100 Subject: [PATCH] Added day 2 code for part 1 and 2 Signed-off-by: Louis Vallat --- day2/Cargo.toml | 8 + day2/input | 1000 ++++++++++++++++++++++++++++++++++++++++++++++ day2/src/main.rs | 57 +++ 3 files changed, 1065 insertions(+) create mode 100644 day2/Cargo.toml create mode 100644 day2/input create mode 100644 day2/src/main.rs diff --git a/day2/Cargo.toml b/day2/Cargo.toml new file mode 100644 index 0000000..8aa34bb --- /dev/null +++ b/day2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day2" +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/day2/input b/day2/input new file mode 100644 index 0000000..dec59fb --- /dev/null +++ b/day2/input @@ -0,0 +1,1000 @@ +forward 2 +forward 5 +forward 5 +down 4 +forward 8 +down 1 +forward 2 +forward 2 +down 9 +down 2 +forward 6 +forward 3 +down 9 +down 2 +forward 4 +up 2 +forward 7 +up 6 +forward 8 +forward 4 +up 5 +forward 3 +down 9 +forward 5 +forward 8 +forward 6 +forward 6 +forward 8 +forward 8 +forward 5 +down 2 +forward 8 +down 4 +down 6 +down 6 +forward 7 +forward 6 +up 3 +up 5 +down 5 +down 3 +up 2 +down 7 +down 2 +forward 1 +forward 6 +up 1 +down 7 +forward 8 +down 7 +down 6 +forward 5 +up 6 +forward 9 +down 9 +up 3 +down 6 +down 1 +forward 6 +up 1 +down 1 +up 3 +up 7 +forward 1 +down 5 +forward 7 +forward 3 +down 9 +up 8 +forward 1 +down 4 +up 1 +forward 9 +forward 7 +down 2 +forward 8 +down 2 +down 5 +down 2 +up 1 +down 5 +forward 4 +forward 8 +forward 3 +forward 3 +forward 1 +down 1 +forward 1 +down 7 +up 7 +down 3 +down 1 +down 7 +down 4 +forward 7 +forward 5 +down 9 +down 5 +forward 3 +up 7 +down 2 +up 1 +forward 1 +forward 2 +down 7 +down 3 +forward 7 +up 4 +forward 3 +up 8 +down 9 +down 4 +forward 5 +forward 6 +down 3 +down 5 +down 4 +down 9 +down 8 +forward 6 +down 3 +down 1 +down 9 +down 6 +forward 9 +forward 2 +up 5 +forward 5 +forward 4 +down 9 +up 7 +up 2 +forward 1 +forward 8 +forward 6 +up 8 +down 9 +down 2 +forward 7 +down 6 +forward 7 +up 9 +up 7 +down 8 +up 1 +forward 2 +forward 9 +down 9 +forward 6 +down 2 +down 1 +up 3 +forward 4 +down 3 +down 4 +down 9 +up 8 +up 8 +forward 3 +up 7 +forward 9 +forward 7 +up 2 +up 8 +forward 2 +down 6 +forward 3 +forward 1 +down 7 +down 2 +forward 9 +forward 9 +down 3 +forward 2 +forward 8 +down 6 +forward 6 +forward 5 +forward 1 +forward 6 +down 8 +down 7 +up 9 +down 6 +up 7 +down 2 +up 8 +up 8 +down 3 +down 7 +up 2 +up 2 +down 6 +down 6 +forward 2 +down 6 +forward 8 +forward 9 +down 3 +forward 6 +down 9 +forward 1 +up 6 +down 3 +up 5 +forward 9 +forward 7 +forward 9 +forward 5 +up 6 +down 3 +down 3 +down 3 +forward 1 +up 5 +forward 3 +forward 2 +down 1 +forward 7 +down 1 +forward 6 +forward 5 +forward 9 +up 6 +forward 1 +up 8 +down 7 +forward 6 +forward 3 +forward 1 +up 6 +forward 4 +up 6 +down 7 +forward 8 +forward 4 +forward 6 +forward 5 +up 6 +down 7 +up 9 +down 7 +forward 3 +down 7 +forward 6 +down 6 +down 6 +forward 8 +up 8 +forward 8 +forward 2 +down 8 +forward 4 +forward 9 +forward 7 +down 4 +down 7 +forward 4 +forward 3 +forward 2 +forward 2 +forward 5 +down 6 +forward 1 +forward 6 +up 8 +up 3 +up 9 +forward 3 +up 8 +forward 3 +up 8 +down 9 +up 7 +forward 9 +forward 1 +down 7 +forward 9 +down 6 +forward 4 +down 3 +up 9 +forward 5 +down 4 +up 6 +down 1 +up 9 +forward 2 +up 2 +down 1 +forward 7 +down 6 +down 4 +down 7 +down 4 +down 8 +up 9 +down 6 +up 3 +down 7 +forward 3 +forward 1 +down 1 +forward 2 +forward 6 +down 6 +forward 5 +down 8 +down 8 +down 6 +down 9 +forward 6 +forward 7 +up 8 +forward 5 +up 8 +down 6 +up 3 +forward 9 +up 9 +forward 2 +up 6 +forward 2 +down 8 +forward 3 +down 5 +down 8 +up 6 +down 3 +down 7 +down 4 +forward 5 +forward 8 +down 8 +forward 6 +down 8 +up 1 +forward 5 +down 3 +down 2 +up 1 +forward 3 +forward 7 +forward 6 +forward 9 +up 8 +down 2 +down 1 +down 7 +forward 4 +forward 2 +down 9 +down 3 +down 5 +up 6 +down 2 +forward 8 +up 1 +up 5 +down 6 +down 7 +forward 3 +down 4 +up 6 +down 9 +up 6 +down 2 +down 2 +down 6 +down 3 +forward 9 +down 6 +forward 5 +forward 6 +forward 4 +down 6 +forward 3 +down 9 +up 8 +forward 2 +up 3 +forward 1 +up 8 +forward 1 +down 6 +down 3 +forward 5 +forward 4 +down 9 +up 3 +down 6 +forward 4 +down 8 +down 3 +down 7 +forward 1 +forward 2 +forward 8 +down 2 +down 6 +up 9 +forward 2 +forward 4 +forward 8 +down 3 +forward 9 +down 7 +up 8 +forward 1 +down 6 +down 3 +forward 5 +forward 5 +forward 2 +down 2 +down 2 +down 2 +up 4 +forward 2 +forward 5 +forward 1 +forward 5 +down 3 +forward 8 +up 1 +forward 9 +up 5 +forward 2 +down 4 +forward 8 +forward 9 +up 6 +up 1 +down 7 +down 3 +forward 9 +down 3 +down 3 +forward 3 +forward 6 +up 4 +forward 2 +down 9 +forward 5 +forward 3 +forward 5 +down 6 +up 6 +down 7 +forward 2 +down 4 +forward 6 +forward 5 +forward 8 +down 4 +up 3 +up 4 +down 9 +forward 9 +down 7 +down 1 +down 9 +down 6 +down 8 +forward 7 +down 3 +up 3 +up 5 +forward 5 +up 6 +down 6 +forward 9 +down 7 +forward 6 +up 1 +forward 7 +forward 8 +down 9 +down 1 +forward 4 +up 2 +down 3 +up 3 +down 6 +forward 6 +down 7 +down 2 +forward 8 +forward 2 +forward 3 +forward 5 +up 8 +up 6 +down 1 +up 7 +down 1 +down 8 +forward 5 +up 7 +down 1 +forward 8 +down 6 +down 2 +up 7 +down 1 +forward 1 +up 8 +forward 5 +down 7 +down 8 +forward 4 +down 6 +down 2 +forward 4 +forward 7 +down 5 +down 1 +down 8 +down 3 +up 8 +down 6 +down 1 +down 1 +forward 4 +down 8 +up 5 +forward 3 +forward 3 +forward 5 +forward 8 +up 7 +forward 5 +down 8 +forward 9 +up 9 +forward 1 +down 2 +up 9 +down 7 +up 1 +up 3 +forward 9 +forward 8 +forward 3 +forward 8 +down 8 +down 5 +down 7 +up 7 +forward 9 +up 8 +down 9 +down 9 +down 1 +up 4 +up 4 +up 3 +forward 3 +down 7 +down 3 +forward 5 +up 9 +down 3 +up 2 +forward 1 +forward 4 +up 9 +forward 6 +down 6 +down 6 +forward 2 +forward 2 +down 4 +forward 9 +up 6 +down 8 +down 8 +down 9 +up 4 +down 9 +down 3 +down 7 +forward 4 +up 4 +up 5 +down 2 +up 2 +forward 1 +up 8 +forward 2 +down 6 +forward 9 +down 9 +forward 9 +up 8 +down 5 +up 3 +up 9 +down 8 +forward 6 +forward 2 +forward 7 +down 9 +down 8 +up 9 +down 5 +up 5 +down 1 +forward 3 +down 2 +down 5 +down 4 +down 1 +down 5 +down 5 +down 5 +forward 8 +down 2 +down 3 +down 4 +down 2 +up 8 +up 1 +down 5 +forward 8 +down 6 +forward 9 +down 6 +down 6 +down 5 +forward 5 +forward 3 +forward 7 +down 6 +forward 4 +down 8 +up 9 +up 6 +forward 5 +down 4 +forward 8 +forward 1 +forward 8 +up 5 +up 5 +forward 6 +down 3 +down 7 +up 7 +down 5 +forward 1 +forward 3 +up 3 +down 3 +down 3 +down 5 +forward 9 +down 2 +up 5 +up 2 +forward 1 +down 1 +forward 5 +down 8 +forward 8 +forward 9 +forward 3 +forward 2 +forward 4 +down 5 +up 1 +down 5 +up 1 +up 4 +forward 2 +forward 4 +down 6 +up 2 +up 4 +forward 4 +down 7 +forward 2 +up 1 +forward 4 +up 4 +forward 9 +down 1 +down 4 +up 1 +forward 2 +forward 3 +down 6 +forward 9 +forward 3 +down 2 +forward 9 +up 4 +forward 5 +forward 2 +down 2 +forward 5 +down 5 +down 3 +down 7 +down 7 +up 3 +down 7 +forward 9 +forward 7 +up 4 +forward 7 +up 8 +down 8 +forward 5 +down 2 +down 9 +up 8 +forward 4 +forward 4 +forward 2 +up 4 +down 5 +forward 3 +forward 6 +forward 5 +forward 3 +forward 7 +up 9 +forward 1 +forward 2 +up 7 +down 4 +forward 8 +down 2 +forward 1 +down 1 +down 9 +up 2 +down 2 +forward 8 +forward 5 +up 9 +forward 5 +down 2 +forward 2 +down 7 +forward 5 +down 1 +forward 9 +forward 8 +forward 8 +forward 9 +forward 8 +down 9 +forward 8 +down 1 +down 2 +down 9 +up 2 +forward 8 +forward 8 +down 9 +forward 8 +up 8 +forward 2 +down 5 +up 3 +forward 2 +up 3 +down 6 +forward 5 +up 7 +forward 2 +forward 7 +forward 7 +down 8 +forward 3 +up 5 +down 8 +down 3 +down 8 +up 6 +down 6 +up 4 +forward 7 +up 6 +up 1 +down 5 +up 8 +forward 8 +forward 9 +down 8 +forward 6 +down 8 +down 9 +down 8 +forward 9 +up 1 +down 1 +down 8 +down 5 +down 2 +up 7 +forward 4 +down 4 +forward 6 +down 1 +forward 5 +forward 3 +forward 9 +up 8 +down 4 +down 6 +down 4 +down 1 +up 9 +forward 9 +down 9 +forward 7 +down 2 +down 8 +forward 2 +forward 5 +down 6 +down 1 +down 1 +up 3 +forward 9 +up 7 +down 1 +down 6 +forward 6 +up 2 +down 4 +down 7 +forward 3 +down 9 +down 1 +forward 7 +forward 2 +forward 6 +down 4 +forward 7 +forward 8 +forward 3 +forward 7 +up 8 +forward 5 +up 8 +down 8 +forward 3 +forward 8 +forward 7 +forward 3 +down 4 +forward 9 +forward 4 +up 6 +forward 4 +forward 6 +up 4 +forward 6 +forward 2 +down 3 +down 3 +down 4 +down 1 +down 9 +up 1 +up 9 +down 7 +up 9 +forward 5 +down 2 +up 7 +forward 9 +up 1 +down 5 +up 8 +forward 9 +down 1 +up 3 +forward 3 +forward 9 +up 7 +forward 3 +down 7 +forward 3 +forward 5 +forward 4 +up 3 +forward 3 +down 8 +forward 7 +up 5 +forward 9 +down 6 +up 6 +up 1 +forward 8 +down 9 +forward 9 +forward 9 +down 5 +down 7 +forward 7 +down 5 +down 3 +forward 4 +up 6 +forward 6 +forward 2 +down 8 +down 6 +forward 1 +forward 1 +down 6 +forward 1 +down 6 +down 2 +forward 1 +forward 6 +up 4 +up 7 +up 3 +down 6 +forward 1 +forward 9 +forward 4 +down 4 +forward 3 +down 8 +down 6 +down 6 +forward 2 +forward 8 +forward 6 +down 4 +up 2 +down 1 +up 4 +down 8 +forward 9 +down 4 +down 8 +up 8 +forward 2 +up 9 +down 9 +forward 2 +down 8 +forward 3 +down 1 +up 9 +forward 6 +down 9 +forward 2 +down 2 +up 5 +down 4 +down 5 +down 7 +forward 4 +down 4 +forward 6 +forward 2 +down 8 +up 3 +up 9 +forward 3 +forward 6 +down 6 +down 2 +down 5 +down 4 +forward 1 +up 2 +down 3 +forward 6 +forward 5 +down 4 +down 3 +forward 2 +forward 4 +down 9 +down 3 +down 4 +up 6 +forward 8 +up 2 +up 8 +up 4 +forward 7 +forward 5 +forward 9 +down 6 +forward 7 +forward 9 +up 3 +forward 6 diff --git a/day2/src/main.rs b/day2/src/main.rs new file mode 100644 index 0000000..d785617 --- /dev/null +++ b/day2/src/main.rs @@ -0,0 +1,57 @@ +use std::{fs, env, vec}; + + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> Vec<(String, i32)> { + let mut r:Vec<(String, i32)> = vec![]; + let v = s.split("\n").into_iter(); + for e in v { + let p = e.split_whitespace().collect::>(); + if p.len() == 2 && p[1].parse::().is_ok() { + r.push((p[0].to_owned(), p[1].parse::().unwrap())); + } + } + return r; +} + +fn compute_horizontal_depth_aim(t: (String, i32), a: (i32, i32, i32)) -> (i32, i32, i32) { + return match t.0.as_str() { + "up" => (a.0, a.1, a.2 - t.1), + "down" => (a.0, a.1, a.2 + t.1), + "forward" => (a.0 + t.1, a.1 + a.2 * t.1, a.2), + _ => a + }; +} + +fn compute_horizontal_depth(t: (String, i32), a: (i32, i32)) -> (i32, i32) { + return match t.0.as_str() { + "up" => (a.0, a.1 - t.1), + "down" => (a.0, a.1 + t.1), + "forward" => (a.0 + t.1, a.1), + _ => a + }; +} + +fn get_horizontal_depth_aim(v: Vec<(String, i32)>) -> (i32, i32, i32) { + return v.into_iter().fold((0, 0, 0), |acc, e| compute_horizontal_depth_aim(e, acc)); +} + +fn get_horizontal_depth(v: Vec<(String, i32)>) -> (i32, i32) { + return v.into_iter().fold((0, 0), |acc, e| compute_horizontal_depth(e, acc)); +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let v = parse_input(&read_input(&arg)); + let r1 = get_horizontal_depth(v.clone()); + let r2 = get_horizontal_depth_aim(v); + println!("[{}]", &arg); + println!("\tPart 1 {} x {} = {}", r1.0, r1.1, r1.0 * r1.1); + println!("\tPart 2 {} x {} = {}", r2.0, r2.1, r2.0 * r2.1); + } +} +