aoc

commit 272d6bd60122c8e81369c4b0c0b141502b77ebb5

Author: Honza Pokorny <honza@pokorny.ca>

Add day 2

 2021/day-02/Cargo.lock | 49 +++++++++++++++++++++++++
 2021/day-02/Cargo.toml | 10 +++++
 2021/day-02/src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++


diff --git a/2021/day-02/Cargo.lock b/2021/day-02/Cargo.lock
new file mode 100644
index 0000000000000000000000000000000000000000..5757063ffd40d03cb7bf8a2c85d2d568d0d46e01
--- /dev/null
+++ b/2021/day-02/Cargo.lock
@@ -0,0 +1,49 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "day-02"
+version = "0.1.0"
+dependencies = [
+ "lazy_static",
+ "regex",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
+name = "regex"
+version = "1.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"




diff --git a/2021/day-02/Cargo.toml b/2021/day-02/Cargo.toml
new file mode 100644
index 0000000000000000000000000000000000000000..09712f40055d0c9b928b9fe11f52c22e105bee68
--- /dev/null
+++ b/2021/day-02/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "day-02"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+regex = "1"
+lazy_static = "1.4.0"




diff --git a/2021/day-02/src/main.rs b/2021/day-02/src/main.rs
new file mode 100644
index 0000000000000000000000000000000000000000..4ad3ec5b8677348cd357906a3c505a201dbb4875
--- /dev/null
+++ b/2021/day-02/src/main.rs
@@ -0,0 +1,84 @@
+use lazy_static::lazy_static;
+use regex::Regex;
+use std::io;
+use std::io::prelude::*;
+use std::str::FromStr;
+
+#[derive(Debug, Clone)]
+enum Instruction {
+    Forward(i32),
+    Down(i32),
+    Up(i32),
+}
+
+#[derive(Debug, Clone)]
+struct InstructionParseError;
+
+impl FromStr for Instruction {
+    type Err = InstructionParseError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        lazy_static! {
+            static ref RE: Regex = Regex::new(r"^(?P<direction>\w+) (?P<value>\d+)$").unwrap();
+        }
+
+        match RE.captures(s) {
+            Some(caps) => {
+                let direction = caps.name("direction").unwrap().as_str();
+                let value: i32 = caps.name("value").unwrap().as_str().parse().unwrap();
+
+                match direction {
+                    "forward" => Ok(Instruction::Forward(value)),
+                    "down" => Ok(Instruction::Down(value)),
+                    "up" => Ok(Instruction::Up(value)),
+                    _ => Err(InstructionParseError),
+                }
+            }
+
+            None => Err(InstructionParseError),
+        }
+    }
+}
+
+#[derive(Debug, Clone)]
+struct Position {
+    horizontal: i32,
+    depth: i32,
+    aim: i32,
+}
+
+fn main() -> io::Result<()> {
+    let stdin = io::stdin();
+    let lines = stdin.lock().lines();
+
+    let instructions = lines.map(|x| x.unwrap().parse().unwrap());
+
+    let res = instructions.fold(
+        Position {
+            horizontal: 0,
+            depth: 0,
+            aim: 0,
+        },
+        |acc, i| match i {
+            Instruction::Forward(v) => Position {
+                horizontal: acc.horizontal + v,
+                depth: acc.depth + (acc.aim * v),
+                aim: acc.aim,
+            },
+            Instruction::Down(v) => Position {
+                horizontal: acc.horizontal,
+                depth: acc.depth,
+                aim: acc.aim + v,
+            },
+            Instruction::Up(v) => Position {
+                horizontal: acc.horizontal,
+                depth: acc.depth,
+                aim: acc.aim - v,
+            },
+        },
+    );
+
+    println!("{:?} {:?}", res, res.horizontal * res.depth);
+
+    Ok(())
+}