diff --git a/2021/02/Cargo.toml b/2021/02/Cargo.toml new file mode 100644 index 0000000..6f0f217 --- /dev/null +++ b/2021/02/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day-02" +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/2021/02/input b/2021/02/input new file mode 100644 index 0000000..917f466 --- /dev/null +++ b/2021/02/input @@ -0,0 +1,1000 @@ +forward 2 +forward 6 +forward 8 +forward 7 +down 5 +forward 8 +forward 9 +down 2 +forward 6 +down 9 +forward 1 +forward 8 +forward 6 +forward 7 +down 4 +down 5 +forward 1 +up 5 +down 7 +down 7 +down 1 +up 2 +forward 3 +forward 2 +forward 2 +forward 5 +up 5 +forward 4 +forward 9 +forward 6 +down 4 +down 9 +down 2 +up 6 +forward 9 +up 7 +forward 7 +forward 5 +up 3 +forward 4 +forward 9 +up 5 +down 3 +up 6 +down 5 +down 4 +up 6 +forward 9 +forward 6 +down 9 +up 3 +down 7 +up 1 +forward 8 +forward 3 +forward 8 +up 6 +forward 7 +forward 5 +forward 8 +up 2 +forward 2 +forward 7 +forward 7 +down 1 +forward 7 +up 7 +down 3 +forward 9 +down 5 +down 2 +forward 5 +forward 1 +forward 4 +forward 6 +up 2 +up 7 +forward 2 +forward 6 +forward 7 +down 9 +up 8 +down 9 +down 3 +up 8 +down 3 +down 2 +up 6 +forward 3 +forward 9 +down 4 +forward 5 +down 6 +up 8 +forward 1 +down 6 +down 6 +forward 5 +down 6 +forward 8 +up 7 +down 3 +forward 7 +forward 3 +forward 1 +forward 4 +forward 4 +down 3 +up 9 +up 5 +forward 1 +down 2 +up 4 +forward 7 +up 4 +down 3 +down 5 +down 8 +forward 4 +up 8 +forward 7 +up 3 +up 4 +up 9 +forward 1 +forward 1 +down 6 +forward 1 +down 8 +up 4 +forward 9 +forward 9 +down 6 +forward 9 +forward 8 +down 2 +up 3 +up 3 +down 9 +forward 7 +forward 8 +down 4 +forward 1 +up 3 +forward 3 +down 3 +down 9 +down 5 +up 7 +up 2 +forward 7 +forward 2 +forward 5 +forward 4 +down 7 +forward 7 +up 1 +up 3 +down 6 +down 4 +forward 9 +forward 8 +down 5 +down 4 +down 1 +down 5 +forward 9 +forward 8 +down 3 +forward 5 +forward 3 +forward 6 +down 6 +forward 3 +up 9 +forward 4 +down 7 +forward 3 +forward 7 +forward 1 +forward 5 +down 1 +forward 1 +down 6 +up 7 +down 3 +forward 2 +down 4 +forward 6 +up 6 +forward 8 +forward 8 +down 5 +up 4 +forward 7 +forward 6 +up 4 +forward 6 +down 1 +forward 6 +forward 2 +up 4 +down 6 +down 7 +forward 4 +down 4 +forward 1 +down 3 +forward 5 +forward 5 +forward 9 +forward 3 +up 7 +down 7 +forward 7 +forward 5 +down 1 +down 1 +forward 3 +down 8 +forward 1 +forward 2 +forward 9 +forward 1 +forward 3 +down 3 +up 4 +forward 5 +down 1 +forward 3 +up 7 +forward 3 +forward 6 +up 6 +up 3 +forward 9 +forward 5 +down 2 +up 4 +up 3 +forward 3 +forward 7 +down 1 +forward 5 +forward 5 +down 1 +forward 4 +forward 2 +down 1 +down 9 +down 7 +up 1 +forward 2 +down 2 +forward 3 +forward 8 +forward 4 +forward 6 +down 4 +down 1 +forward 5 +forward 1 +forward 7 +down 8 +forward 9 +down 6 +forward 3 +up 5 +up 1 +up 7 +down 5 +forward 7 +forward 5 +forward 5 +up 1 +forward 8 +down 8 +down 7 +forward 9 +forward 9 +down 3 +forward 7 +forward 2 +down 1 +down 6 +down 1 +forward 7 +down 3 +forward 1 +forward 1 +forward 6 +forward 6 +up 9 +down 3 +forward 9 +down 8 +forward 4 +up 6 +down 4 +down 7 +forward 5 +up 3 +forward 1 +forward 8 +up 6 +up 3 +down 2 +forward 2 +forward 5 +forward 1 +down 8 +down 8 +down 3 +forward 5 +forward 4 +forward 4 +forward 5 +up 5 +forward 2 +forward 5 +up 5 +forward 6 +forward 6 +forward 9 +up 5 +forward 4 +up 4 +forward 8 +down 8 +forward 5 +forward 2 +forward 4 +forward 3 +forward 1 +down 1 +down 9 +down 2 +forward 4 +down 3 +down 6 +forward 2 +up 7 +forward 6 +down 4 +up 9 +down 1 +forward 8 +forward 1 +forward 1 +down 9 +down 3 +down 2 +down 7 +up 5 +down 7 +up 9 +down 8 +down 7 +forward 9 +forward 7 +up 4 +forward 5 +up 9 +down 4 +forward 1 +forward 9 +down 7 +up 9 +forward 6 +forward 4 +up 8 +down 2 +forward 1 +up 6 +up 5 +down 4 +forward 8 +down 3 +down 5 +down 6 +up 1 +up 9 +up 7 +up 5 +forward 1 +forward 3 +down 7 +forward 9 +forward 2 +forward 6 +down 4 +down 7 +forward 3 +down 1 +up 5 +forward 3 +down 3 +down 1 +forward 1 +forward 4 +forward 8 +down 4 +down 1 +forward 3 +down 7 +up 9 +down 8 +down 1 +forward 2 +down 6 +down 9 +down 9 +forward 2 +forward 8 +up 2 +down 5 +down 9 +forward 1 +up 9 +down 7 +forward 8 +down 7 +up 4 +forward 8 +down 8 +down 7 +forward 6 +up 7 +down 4 +down 9 +forward 9 +up 8 +down 8 +down 8 +down 8 +down 5 +forward 2 +up 9 +down 2 +up 7 +down 7 +down 3 +down 6 +forward 9 +forward 1 +down 1 +down 5 +up 4 +down 5 +forward 5 +up 2 +forward 5 +down 5 +forward 1 +forward 9 +down 9 +forward 3 +forward 3 +down 8 +down 2 +down 8 +forward 8 +forward 7 +up 6 +down 4 +down 5 +forward 8 +forward 4 +forward 7 +forward 1 +down 9 +down 4 +down 2 +forward 5 +down 3 +down 7 +down 5 +forward 8 +up 1 +down 4 +down 7 +down 7 +forward 2 +up 5 +forward 5 +up 2 +up 4 +down 9 +forward 7 +forward 6 +forward 6 +down 2 +forward 7 +forward 7 +down 7 +forward 8 +down 2 +up 9 +down 1 +forward 9 +down 9 +forward 3 +down 9 +down 2 +forward 9 +forward 8 +down 7 +up 2 +forward 8 +forward 1 +up 2 +down 7 +up 7 +down 8 +up 1 +up 4 +up 2 +up 3 +down 7 +forward 1 +down 8 +down 4 +down 2 +down 4 +up 8 +forward 8 +down 2 +up 5 +up 4 +forward 7 +up 1 +forward 3 +down 8 +down 4 +forward 4 +down 8 +forward 2 +down 1 +up 9 +forward 9 +down 4 +up 2 +down 8 +up 9 +forward 6 +down 7 +up 7 +forward 9 +forward 1 +down 8 +forward 5 +down 9 +forward 6 +down 9 +forward 9 +forward 1 +down 8 +up 4 +forward 9 +forward 3 +down 9 +up 8 +forward 4 +up 8 +forward 7 +down 7 +up 6 +down 7 +down 2 +down 7 +forward 3 +forward 2 +down 6 +down 2 +down 7 +up 4 +forward 5 +down 5 +forward 2 +up 3 +up 8 +forward 8 +forward 1 +forward 7 +down 7 +down 2 +forward 1 +down 7 +down 7 +up 2 +up 7 +up 7 +forward 4 +down 5 +forward 5 +forward 7 +forward 7 +down 7 +down 8 +forward 8 +forward 8 +up 3 +up 9 +forward 2 +down 7 +up 3 +up 1 +up 1 +down 9 +up 5 +down 6 +up 8 +up 3 +up 5 +forward 7 +forward 3 +forward 8 +forward 4 +up 1 +forward 2 +forward 1 +up 5 +forward 9 +forward 8 +down 7 +up 1 +forward 7 +down 8 +forward 1 +forward 9 +forward 9 +forward 9 +forward 8 +down 1 +forward 8 +forward 7 +up 9 +up 3 +forward 8 +forward 2 +up 2 +down 7 +down 6 +forward 4 +forward 3 +forward 6 +up 7 +down 9 +forward 1 +forward 4 +down 1 +forward 4 +up 3 +down 8 +forward 1 +up 6 +forward 8 +forward 2 +forward 1 +forward 8 +forward 4 +down 7 +forward 4 +forward 6 +down 2 +up 4 +forward 4 +forward 3 +down 5 +forward 8 +forward 4 +forward 5 +forward 7 +forward 6 +forward 5 +forward 9 +down 4 +down 9 +forward 6 +up 7 +down 6 +down 3 +down 2 +up 9 +forward 7 +down 4 +down 5 +forward 2 +forward 3 +forward 2 +forward 9 +forward 7 +forward 8 +down 9 +down 7 +down 9 +down 7 +forward 5 +forward 2 +down 5 +forward 6 +down 1 +down 2 +down 6 +forward 9 +down 3 +up 6 +down 4 +down 5 +forward 3 +forward 7 +down 8 +forward 2 +forward 5 +down 9 +down 3 +up 5 +down 6 +forward 6 +up 3 +down 6 +down 1 +down 8 +down 5 +down 3 +forward 3 +up 6 +up 7 +forward 8 +forward 9 +forward 2 +forward 6 +forward 2 +forward 3 +down 7 +down 3 +down 3 +down 6 +down 2 +forward 4 +forward 3 +forward 8 +up 1 +down 9 +forward 5 +up 3 +down 7 +down 6 +forward 8 +forward 1 +up 6 +forward 3 +forward 1 +up 9 +forward 6 +forward 3 +down 9 +down 4 +down 9 +forward 5 +down 8 +down 3 +forward 1 +forward 1 +down 9 +down 6 +down 3 +up 7 +down 3 +forward 5 +down 2 +forward 7 +forward 2 +forward 5 +up 7 +forward 4 +forward 4 +up 3 +down 6 +down 7 +up 1 +down 6 +forward 1 +forward 9 +down 7 +down 8 +forward 5 +down 1 +down 9 +up 5 +up 4 +up 3 +forward 6 +down 6 +forward 4 +forward 8 +up 6 +up 2 +down 9 +forward 2 +forward 5 +forward 1 +forward 3 +forward 9 +up 3 +forward 2 +forward 1 +forward 3 +forward 3 +up 9 +forward 3 +forward 7 +down 6 +forward 2 +down 8 +up 9 +forward 8 +forward 5 +forward 2 +up 8 +down 9 +up 5 +forward 3 +down 4 +forward 1 +up 9 +down 4 +down 5 +up 4 +down 6 +down 4 +down 6 +down 4 +forward 4 +down 2 +down 1 +down 6 +forward 2 +down 1 +down 3 +forward 4 +down 3 +down 5 +down 5 +up 1 +up 4 +down 4 +down 4 +down 5 +down 4 +down 5 +forward 5 +down 8 +down 5 +down 5 +down 9 +up 1 +up 5 +forward 5 +down 1 +down 9 +down 4 +down 3 +forward 3 +down 2 +forward 9 +down 3 +forward 1 +down 9 +down 5 +up 7 +forward 3 +forward 1 +forward 2 +down 5 +forward 8 +down 3 +down 3 +forward 6 +down 8 +down 3 +down 8 +up 9 +forward 3 +down 6 +forward 4 +down 6 +down 4 +up 5 +forward 1 +up 6 +up 2 +forward 2 +down 8 +forward 7 +forward 8 +down 6 +down 7 +forward 7 +up 3 +forward 3 +up 6 +forward 3 +down 1 +down 7 +forward 9 +forward 5 +up 1 +forward 7 +forward 1 +down 3 +forward 1 +up 4 +up 2 +up 1 +down 8 +forward 9 +forward 3 +forward 4 +up 7 +forward 5 +down 1 +down 8 +down 3 +down 4 +down 6 +down 5 +forward 4 +down 4 +down 2 +down 4 +down 3 +down 3 +forward 4 +up 3 +forward 6 +down 7 +forward 4 +up 2 +down 7 +forward 8 +up 9 +forward 6 +forward 8 +down 1 +down 6 +forward 6 +down 6 +down 9 +up 8 +forward 8 +up 5 +forward 6 +forward 9 +forward 4 +up 2 +forward 3 +down 7 +down 8 +down 4 +up 8 +forward 8 +forward 1 +up 5 +up 4 +up 1 +down 9 +down 9 +up 2 +forward 9 +down 7 +down 2 +up 2 +down 1 +forward 6 +forward 2 +down 5 +down 8 +forward 6 +down 2 +down 3 +forward 6 +forward 7 +up 8 +down 4 +forward 5 +down 9 +down 2 +down 7 +down 9 +down 5 +forward 9 +forward 2 +down 6 +forward 7 +up 6 +forward 3 +up 2 +forward 9 +forward 2 diff --git a/2021/02/src/main.rs b/2021/02/src/main.rs new file mode 100644 index 0000000..3021359 --- /dev/null +++ b/2021/02/src/main.rs @@ -0,0 +1,65 @@ +use std::fs; + +#[derive(Debug, Clone, Copy)] +enum Instruction { + Forward(u64), + Up(u64), + Down(u64), +} + +impl Instruction { + fn parse(s: &str) -> Self { + use Instruction::*; + + let parts: Vec<&str> = s.split(" ").collect(); + let amount: u64 = parts[1].parse().expect("Unable to parse amount"); + + match parts[0] { + "forward" => Forward(amount), + "up" => Up(amount), + "down" => Down(amount), + _ => panic!("Unable to parse Instruction {}", parts[0]), + } + } +} + +fn main() { + use Instruction::*; + + let instructions: Vec = fs::read_to_string("input") + .expect("Unable to read input") + .split("\n") + .filter(|s| !s.is_empty()) + .map(Instruction::parse) + .collect::>(); + + let mut horizontal = 0; + let mut depth = 0; + + for instruction in instructions.clone() { + match instruction { + Forward(v) => horizontal += v, + Up(v) => depth -= v, + Down(v) => depth += v, + } + } + + println!("Part 1: {}", horizontal * depth); + + let mut horizontal = 0; + let mut depth = 0; + let mut aim = 0; + + for instruction in instructions.clone() { + match instruction { + Forward(v) => { + horizontal += v; + depth += aim * v; + } + Up(v) => aim -= v, + Down(v) => aim += v, + } + } + + println!("Part 2: {}", horizontal * depth); +}