From 57bf7b2194f931b3af4457dcc33770c80c7109e5 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sat, 4 Dec 2021 01:18:16 +0000 Subject: [PATCH] 2021 Day 2 done in Rust Code is currently fairly ugly, will try to refactor in a more functional fashion --- 2021/Day 2/input.txt | 1000 ++++++++++++++++++++++++++ 2021/Day 2/rust_solution/Cargo.lock | 5 + 2021/Day 2/rust_solution/Cargo.toml | 9 + 2021/Day 2/rust_solution/src/main.rs | 43 ++ 4 files changed, 1057 insertions(+) create mode 100644 2021/Day 2/input.txt create mode 100644 2021/Day 2/rust_solution/Cargo.lock create mode 100644 2021/Day 2/rust_solution/Cargo.toml create mode 100644 2021/Day 2/rust_solution/src/main.rs diff --git a/2021/Day 2/input.txt b/2021/Day 2/input.txt new file mode 100644 index 0000000..171b7da --- /dev/null +++ b/2021/Day 2/input.txt @@ -0,0 +1,1000 @@ +forward 6 +down 8 +down 5 +down 9 +forward 2 +down 5 +down 5 +forward 1 +forward 7 +down 8 +up 2 +down 4 +up 8 +down 8 +forward 3 +forward 4 +down 1 +forward 5 +up 7 +down 7 +down 8 +forward 2 +up 3 +forward 1 +forward 6 +forward 9 +forward 7 +forward 8 +forward 2 +forward 3 +up 2 +up 8 +down 1 +forward 7 +down 7 +down 2 +forward 6 +down 1 +forward 5 +down 3 +forward 6 +down 7 +up 1 +up 3 +forward 7 +forward 6 +forward 8 +down 4 +down 2 +up 5 +down 2 +forward 2 +up 5 +forward 6 +down 3 +down 1 +down 5 +forward 6 +up 6 +down 7 +down 8 +down 2 +forward 3 +down 5 +down 4 +forward 7 +forward 9 +up 9 +up 8 +up 4 +forward 8 +forward 5 +down 4 +up 2 +forward 9 +up 5 +down 5 +up 9 +forward 2 +forward 3 +down 6 +down 8 +forward 8 +up 5 +down 5 +forward 7 +forward 6 +forward 8 +up 3 +forward 3 +forward 1 +up 8 +down 8 +down 2 +down 4 +up 7 +up 2 +up 9 +up 4 +forward 6 +down 8 +down 1 +forward 6 +forward 6 +down 4 +down 2 +up 7 +down 9 +down 9 +up 2 +up 7 +down 4 +down 2 +forward 1 +down 1 +up 5 +up 5 +forward 9 +up 3 +down 7 +forward 7 +down 4 +down 8 +up 1 +down 4 +down 7 +forward 5 +up 9 +forward 5 +forward 1 +forward 8 +forward 6 +forward 5 +forward 1 +down 4 +down 6 +forward 5 +forward 2 +forward 3 +down 1 +up 2 +up 9 +forward 4 +up 8 +down 7 +down 8 +up 7 +down 2 +forward 7 +up 1 +forward 5 +forward 1 +forward 8 +forward 1 +up 8 +down 6 +down 7 +forward 2 +down 8 +down 8 +forward 8 +up 8 +down 6 +down 7 +down 4 +down 7 +forward 6 +up 3 +forward 3 +down 2 +down 8 +down 3 +down 9 +forward 9 +forward 7 +down 6 +down 4 +forward 6 +down 2 +down 7 +up 7 +up 8 +forward 2 +forward 8 +down 3 +up 2 +forward 9 +down 2 +up 3 +down 1 +down 1 +down 4 +down 8 +up 2 +up 8 +forward 2 +forward 1 +up 1 +forward 7 +down 8 +down 1 +down 7 +up 3 +down 3 +forward 8 +forward 2 +forward 7 +down 2 +up 9 +up 3 +up 5 +down 4 +up 3 +forward 4 +up 5 +down 9 +down 9 +forward 2 +forward 2 +down 2 +down 8 +down 3 +down 5 +forward 6 +down 6 +up 5 +down 2 +down 4 +down 9 +down 3 +forward 7 +down 1 +forward 1 +down 4 +up 1 +down 9 +forward 5 +up 2 +down 3 +forward 8 +forward 9 +up 9 +down 2 +forward 8 +down 4 +down 5 +forward 6 +forward 5 +forward 4 +down 6 +down 9 +down 2 +forward 9 +down 4 +up 8 +up 9 +up 2 +up 5 +up 5 +forward 9 +up 1 +forward 6 +forward 7 +forward 8 +forward 9 +up 2 +forward 3 +forward 4 +forward 6 +forward 9 +up 5 +up 5 +down 3 +forward 1 +forward 3 +forward 2 +forward 3 +forward 6 +forward 7 +down 4 +down 2 +down 1 +forward 2 +down 5 +forward 3 +forward 6 +down 8 +down 9 +forward 4 +forward 6 +down 6 +down 6 +forward 3 +down 6 +down 8 +down 1 +forward 7 +forward 9 +down 2 +down 5 +forward 1 +forward 3 +down 2 +forward 1 +down 8 +down 1 +forward 4 +down 8 +forward 5 +forward 1 +down 7 +down 7 +forward 3 +forward 1 +forward 6 +forward 7 +forward 5 +up 1 +forward 2 +down 9 +forward 3 +up 1 +forward 2 +down 1 +down 6 +down 3 +forward 7 +down 5 +down 4 +down 1 +forward 9 +forward 9 +down 5 +forward 7 +forward 3 +forward 5 +down 1 +forward 6 +down 8 +up 2 +forward 6 +down 3 +forward 2 +forward 9 +forward 4 +down 1 +down 3 +forward 9 +forward 3 +forward 8 +forward 9 +up 3 +up 1 +forward 1 +forward 2 +down 8 +down 9 +down 2 +down 1 +down 3 +down 2 +forward 9 +forward 7 +down 5 +forward 1 +forward 6 +forward 3 +forward 9 +down 2 +forward 8 +down 5 +down 1 +forward 5 +forward 3 +down 6 +forward 6 +down 8 +forward 2 +up 5 +forward 1 +down 2 +down 6 +forward 9 +forward 7 +down 1 +down 3 +down 6 +up 3 +down 4 +forward 8 +forward 1 +forward 7 +down 2 +down 5 +down 9 +forward 6 +down 5 +forward 5 +up 1 +down 5 +forward 8 +up 9 +forward 2 +down 6 +forward 2 +forward 7 +up 2 +down 9 +down 7 +up 7 +down 6 +up 5 +forward 1 +down 8 +forward 8 +forward 1 +forward 7 +down 9 +down 6 +forward 3 +down 6 +down 1 +down 1 +down 1 +down 3 +down 7 +down 7 +down 3 +down 5 +forward 4 +down 4 +forward 7 +forward 5 +down 9 +down 9 +forward 7 +down 3 +down 9 +down 4 +forward 3 +down 7 +down 2 +forward 2 +down 6 +forward 9 +forward 9 +forward 5 +up 4 +down 7 +down 2 +up 9 +up 4 +forward 8 +forward 1 +down 8 +up 5 +down 4 +down 3 +forward 2 +down 7 +down 2 +down 1 +down 9 +forward 7 +forward 7 +up 8 +up 4 +down 3 +down 8 +forward 6 +forward 5 +forward 5 +forward 5 +down 3 +down 8 +forward 4 +forward 7 +forward 1 +up 3 +up 9 +down 6 +up 4 +down 7 +forward 8 +forward 4 +forward 3 +up 8 +up 3 +down 3 +forward 6 +down 2 +forward 7 +forward 4 +forward 8 +down 3 +down 9 +down 9 +down 2 +forward 8 +up 4 +down 3 +forward 8 +forward 5 +forward 7 +down 6 +up 9 +forward 3 +down 2 +forward 5 +forward 2 +down 7 +forward 6 +forward 2 +up 9 +down 1 +down 1 +forward 4 +up 1 +forward 9 +down 3 +down 4 +down 2 +forward 3 +forward 3 +forward 3 +up 7 +up 8 +down 5 +forward 1 +forward 7 +up 9 +up 3 +down 3 +down 8 +forward 6 +up 5 +up 5 +forward 4 +down 2 +down 8 +down 1 +forward 6 +down 3 +forward 3 +forward 6 +forward 1 +up 3 +up 1 +down 5 +down 2 +down 7 +down 1 +forward 9 +down 4 +down 8 +forward 9 +forward 7 +forward 8 +down 1 +down 2 +up 7 +down 5 +down 2 +down 1 +up 4 +up 8 +up 7 +down 4 +forward 3 +down 2 +down 2 +forward 5 +forward 4 +down 8 +up 4 +forward 4 +up 1 +down 3 +down 9 +down 9 +down 3 +up 8 +forward 1 +forward 6 +down 6 +down 2 +forward 8 +down 3 +forward 8 +forward 2 +forward 9 +up 3 +forward 6 +down 5 +forward 6 +forward 2 +up 7 +down 9 +forward 2 +up 2 +forward 7 +down 1 +down 5 +down 6 +forward 8 +down 6 +forward 4 +forward 1 +forward 3 +forward 4 +up 4 +forward 4 +down 4 +forward 2 +forward 5 +forward 2 +forward 5 +down 9 +up 2 +up 1 +down 2 +up 4 +up 5 +forward 2 +down 3 +down 9 +forward 3 +down 8 +down 9 +forward 5 +down 3 +forward 5 +down 3 +up 8 +forward 7 +forward 1 +down 2 +down 7 +forward 3 +down 8 +forward 9 +down 4 +down 1 +down 7 +down 4 +up 5 +forward 1 +down 4 +forward 1 +forward 8 +up 1 +up 5 +up 2 +up 2 +down 4 +down 7 +forward 2 +down 8 +up 8 +down 9 +down 3 +down 6 +down 3 +down 1 +forward 7 +up 8 +forward 5 +up 5 +down 8 +down 1 +down 8 +down 6 +down 5 +forward 2 +up 5 +down 6 +forward 9 +up 6 +down 5 +down 7 +up 9 +down 1 +forward 4 +up 6 +forward 2 +down 5 +down 5 +forward 2 +up 6 +forward 1 +down 8 +forward 4 +up 8 +down 3 +forward 8 +down 8 +forward 5 +down 6 +down 3 +forward 1 +down 4 +down 8 +up 1 +down 1 +down 2 +up 9 +forward 2 +forward 3 +down 7 +down 2 +forward 7 +up 8 +down 2 +down 8 +down 9 +up 1 +down 5 +down 5 +down 4 +down 8 +down 9 +up 5 +forward 2 +down 4 +down 3 +down 2 +forward 5 +forward 8 +down 8 +down 1 +forward 9 +down 5 +forward 5 +down 2 +up 3 +up 9 +down 1 +down 9 +forward 7 +up 7 +forward 3 +up 6 +forward 8 +down 2 +down 1 +down 7 +forward 5 +down 8 +down 4 +forward 7 +forward 4 +down 6 +forward 9 +down 3 +forward 2 +down 3 +down 1 +down 1 +up 1 +up 3 +down 6 +forward 3 +up 9 +down 4 +up 2 +down 3 +up 1 +down 8 +down 5 +forward 7 +forward 2 +forward 9 +down 8 +down 5 +down 6 +up 3 +forward 2 +up 8 +down 4 +forward 7 +down 8 +down 6 +down 4 +forward 7 +up 9 +down 4 +forward 2 +forward 5 +down 3 +up 6 +up 6 +down 2 +down 4 +forward 8 +forward 5 +forward 3 +forward 5 +down 5 +down 5 +down 6 +forward 3 +forward 7 +forward 1 +down 8 +down 5 +forward 7 +up 7 +down 9 +down 9 +down 9 +up 6 +down 2 +down 3 +forward 1 +up 7 +up 8 +forward 5 +down 1 +down 3 +down 3 +forward 5 +down 7 +down 1 +up 2 +down 2 +down 3 +forward 7 +down 9 +forward 6 +down 5 +forward 2 +down 5 +forward 6 +up 3 +down 8 +up 2 +forward 5 +forward 1 +forward 5 +forward 8 +forward 6 +forward 9 +forward 6 +up 6 +up 5 +down 8 +down 3 +down 5 +down 2 +forward 9 +forward 8 +down 1 +up 1 +up 6 +down 6 +forward 4 +down 3 +forward 6 +forward 1 +up 5 +down 6 +up 9 +down 7 +down 2 +down 9 +down 5 +forward 5 +up 2 +forward 8 +down 2 +down 8 +forward 6 +down 4 +forward 8 +down 7 +down 8 +down 1 +forward 3 +down 6 +down 9 +down 3 +forward 3 +down 8 +forward 8 +down 7 +forward 6 +forward 8 +down 8 +up 7 +down 1 +forward 2 +forward 3 +down 5 +up 8 +down 3 +down 4 +down 7 +forward 9 +forward 7 +forward 1 +down 3 +forward 9 +down 8 +forward 2 +down 2 +down 9 +down 4 +down 3 +up 6 +up 9 +down 3 +down 2 +forward 5 +down 3 +down 2 +down 8 +forward 6 +forward 5 +up 4 +forward 9 +forward 8 +forward 9 +down 2 +forward 2 +up 6 +forward 1 +down 5 +forward 2 +down 8 +up 2 +up 3 +down 3 +up 2 +up 1 +up 5 +forward 1 +forward 2 +down 8 +up 3 +down 9 +forward 7 +up 5 +down 4 +down 4 +up 3 +forward 2 +up 5 +down 4 +down 4 +up 5 +forward 8 +down 8 +down 6 +forward 7 +down 1 +down 3 +down 1 +forward 3 +down 5 +down 3 +forward 3 +up 2 +forward 2 +down 9 +up 8 +forward 8 +up 8 +forward 1 +forward 9 +forward 3 +down 8 +down 3 +forward 8 +forward 4 +down 2 +forward 2 +down 2 +down 5 +down 7 +down 5 +forward 8 +up 3 +forward 1 +down 1 +forward 3 +down 9 +forward 2 +forward 2 +forward 7 +down 7 +down 2 +forward 9 +up 5 +up 7 +forward 8 +forward 1 +down 7 +down 8 +down 3 +forward 6 diff --git a/2021/Day 2/rust_solution/Cargo.lock b/2021/Day 2/rust_solution/Cargo.lock new file mode 100644 index 0000000..38ad082 --- /dev/null +++ b/2021/Day 2/rust_solution/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rust_solution" +version = "0.1.0" diff --git a/2021/Day 2/rust_solution/Cargo.toml b/2021/Day 2/rust_solution/Cargo.toml new file mode 100644 index 0000000..811accc --- /dev/null +++ b/2021/Day 2/rust_solution/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust_solution" +version = "0.1.0" +authors = ["Teo-CD"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/Day 2/rust_solution/src/main.rs b/2021/Day 2/rust_solution/src/main.rs new file mode 100644 index 0000000..f6e0eea --- /dev/null +++ b/2021/Day 2/rust_solution/src/main.rs @@ -0,0 +1,43 @@ +use std::fs; + +fn first_puzzle(input: &String) { + let mut position = [0; 2]; + for line in input.lines() { + if line.contains("forward") { + // Surely there is a better way to parse a string for an integer ? + position[0] += line.rsplit(' ').next().unwrap().parse::().unwrap(); + } else if line.contains("up") { + position[1] -= line.rsplit(' ').next().unwrap().parse::().unwrap(); + } else if line.contains("down") { + position[1] += line.rsplit(' ').next().unwrap().parse::().unwrap(); + } + } + + println!("Position : {}, {}", position[0], position[1]); + println!("Multiplied : {}", position[0] * position[1]); +} + +fn second_puzzle(input: &String) { + let mut position = [0; 3]; + for line in input.lines() { + if line.contains("forward") { + let forward_move = line.rsplit(' ').next().unwrap().parse::().unwrap(); + position[0] += forward_move; + position[1] += forward_move * position[2]; + } else if line.contains("up") { + position[2] -= line.rsplit(' ').next().unwrap().parse::().unwrap(); + } else if line.contains("down") { + position[2] += line.rsplit(' ').next().unwrap().parse::().unwrap(); + } + } + + println!("Position : {}, {}", position[0], position[1]); + println!("Multiplied : {}", position[0] * position[1]); +} + +fn main() { + let contents = fs::read_to_string("../input.txt").expect("Could not open file."); + first_puzzle(&contents); + second_puzzle(&contents); + 0; +}