advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 44e82d70e523a6b1b9daa3e6977fadae9af2f914
parent 1a536e08d9b7cd194b3745a2d7c151542455a9c4
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Thu,  2 Dec 2021 20:52:40 -0600

Add 2021 day 02

Diffstat:
A2021/day02/input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day02/main.rs | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 1064 insertions(+), 0 deletions(-)
diff --git a/2021/day02/input.txt b/2021/day02/input.txt
@@ -0,0 +1,1000 @@
+forward 1
+down 6
+forward 8
+up 3
+down 1
+down 4
+down 7
+down 3
+down 6
+up 8
+down 3
+forward 8
+up 4
+forward 8
+up 5
+down 3
+up 2
+forward 3
+forward 1
+forward 1
+up 4
+down 6
+forward 5
+down 7
+down 1
+down 5
+down 6
+up 5
+down 2
+forward 7
+down 9
+forward 5
+up 6
+forward 2
+down 2
+down 2
+down 1
+forward 6
+forward 8
+up 5
+down 6
+down 7
+forward 5
+up 7
+down 9
+down 9
+down 3
+down 3
+forward 1
+down 2
+up 8
+down 1
+down 7
+down 4
+forward 6
+up 8
+up 8
+up 5
+up 4
+down 9
+forward 4
+down 3
+down 8
+up 8
+up 7
+down 3
+forward 2
+forward 3
+forward 4
+forward 9
+up 2
+forward 9
+up 5
+up 9
+down 9
+forward 2
+down 7
+down 2
+forward 7
+down 3
+down 9
+down 8
+up 3
+up 4
+down 8
+down 8
+up 7
+up 7
+up 2
+up 9
+up 6
+forward 7
+forward 9
+forward 1
+up 9
+down 2
+forward 9
+down 6
+forward 6
+down 5
+up 2
+forward 5
+forward 2
+down 6
+down 3
+forward 4
+up 9
+down 8
+down 1
+down 9
+down 2
+down 5
+forward 6
+forward 2
+forward 4
+down 5
+down 8
+forward 2
+forward 1
+forward 3
+forward 4
+up 4
+forward 7
+down 4
+up 1
+up 2
+down 5
+down 7
+up 3
+forward 6
+up 2
+down 1
+down 4
+down 3
+forward 8
+forward 4
+down 7
+up 8
+down 8
+down 1
+forward 9
+down 3
+forward 1
+down 2
+up 4
+down 4
+down 5
+up 3
+up 1
+up 2
+forward 7
+forward 4
+forward 3
+down 4
+forward 7
+forward 7
+forward 8
+forward 9
+down 7
+forward 9
+forward 8
+up 2
+up 9
+forward 3
+forward 1
+down 3
+forward 3
+up 7
+up 1
+forward 4
+down 1
+up 9
+up 2
+down 9
+forward 4
+forward 7
+down 2
+forward 7
+forward 6
+up 7
+forward 8
+up 7
+up 2
+down 8
+down 6
+down 6
+forward 7
+down 8
+down 1
+forward 3
+down 1
+down 9
+forward 5
+up 1
+forward 6
+up 8
+down 1
+forward 4
+forward 6
+up 4
+up 8
+forward 5
+forward 4
+forward 7
+down 2
+up 6
+forward 7
+forward 1
+forward 5
+down 6
+forward 3
+forward 4
+forward 2
+forward 8
+down 5
+forward 2
+up 3
+forward 2
+forward 4
+up 2
+down 5
+down 5
+forward 4
+forward 1
+down 1
+up 9
+forward 4
+forward 9
+up 4
+up 4
+forward 6
+down 6
+forward 6
+forward 7
+down 2
+forward 8
+forward 6
+down 4
+down 3
+down 5
+down 5
+up 2
+down 6
+up 5
+up 4
+down 8
+down 7
+down 9
+down 7
+forward 5
+up 5
+forward 4
+forward 8
+down 4
+up 4
+up 7
+forward 8
+up 4
+up 2
+forward 6
+up 3
+down 1
+forward 6
+forward 3
+up 2
+forward 2
+forward 8
+forward 8
+forward 2
+down 9
+down 4
+forward 8
+forward 9
+down 3
+forward 5
+up 7
+down 6
+up 2
+up 6
+up 8
+forward 7
+down 1
+up 7
+down 7
+forward 1
+forward 5
+forward 4
+down 8
+forward 4
+down 8
+forward 1
+down 7
+down 8
+forward 1
+forward 2
+down 3
+down 3
+up 4
+forward 7
+down 2
+forward 9
+up 8
+down 1
+forward 5
+forward 6
+forward 5
+forward 3
+down 6
+down 1
+up 4
+down 9
+forward 8
+up 2
+down 5
+forward 1
+up 2
+down 8
+down 9
+forward 6
+up 8
+down 5
+down 5
+down 7
+up 6
+up 4
+up 3
+up 7
+up 3
+down 5
+forward 9
+up 6
+down 1
+down 8
+down 8
+forward 9
+forward 3
+forward 7
+forward 3
+forward 1
+up 7
+down 3
+down 6
+forward 8
+up 5
+forward 6
+down 6
+forward 3
+down 1
+up 8
+forward 5
+forward 9
+up 1
+up 1
+forward 9
+up 1
+forward 4
+forward 1
+forward 7
+down 6
+up 1
+forward 4
+up 7
+down 2
+down 1
+forward 2
+up 4
+forward 3
+down 4
+up 7
+down 9
+down 9
+forward 8
+down 4
+up 7
+down 4
+forward 2
+up 7
+forward 2
+forward 4
+down 5
+forward 4
+down 6
+forward 9
+forward 1
+forward 5
+forward 7
+up 5
+down 9
+down 5
+down 5
+up 7
+forward 7
+forward 6
+up 7
+forward 8
+up 2
+forward 5
+down 9
+up 3
+down 5
+forward 4
+down 7
+up 8
+up 8
+down 3
+down 2
+down 7
+down 9
+forward 6
+forward 9
+forward 9
+forward 2
+down 2
+forward 4
+forward 7
+up 2
+up 5
+forward 8
+down 1
+down 1
+down 9
+down 2
+forward 1
+up 5
+forward 6
+down 1
+forward 8
+up 2
+up 4
+down 3
+forward 6
+forward 3
+down 4
+forward 9
+down 6
+down 9
+up 2
+down 4
+down 6
+forward 3
+up 4
+down 4
+up 9
+forward 4
+forward 9
+forward 2
+forward 8
+down 9
+up 5
+forward 8
+down 1
+forward 8
+up 1
+down 1
+forward 7
+up 6
+down 2
+up 1
+down 8
+down 4
+forward 3
+down 3
+forward 6
+forward 1
+forward 9
+down 5
+down 9
+down 6
+down 2
+forward 4
+down 6
+down 5
+up 3
+up 1
+down 8
+forward 7
+forward 5
+down 8
+down 4
+down 2
+forward 5
+forward 2
+forward 5
+down 5
+forward 7
+down 9
+down 1
+forward 2
+down 4
+down 1
+forward 6
+up 2
+up 6
+forward 7
+down 1
+up 5
+down 6
+forward 3
+up 3
+forward 5
+forward 5
+down 4
+forward 4
+down 8
+down 2
+up 3
+up 4
+down 9
+up 5
+down 6
+forward 6
+forward 6
+up 9
+up 2
+forward 3
+down 5
+down 9
+forward 2
+up 3
+forward 7
+forward 1
+up 9
+down 8
+up 9
+forward 4
+forward 4
+forward 8
+down 4
+down 2
+down 5
+down 7
+down 9
+up 2
+down 1
+forward 7
+down 4
+down 1
+forward 2
+down 3
+up 5
+up 1
+down 3
+down 4
+up 4
+down 7
+forward 6
+forward 3
+forward 4
+down 5
+up 3
+up 9
+forward 2
+down 8
+up 2
+down 7
+down 2
+up 3
+forward 5
+forward 7
+up 4
+down 3
+down 1
+forward 5
+forward 2
+up 3
+up 6
+forward 8
+down 5
+forward 5
+down 7
+down 5
+up 7
+down 8
+forward 5
+down 9
+up 5
+forward 8
+forward 6
+forward 7
+down 9
+up 9
+forward 3
+down 8
+forward 1
+forward 5
+forward 9
+down 9
+down 2
+down 6
+down 7
+forward 8
+down 6
+down 8
+up 8
+down 2
+forward 2
+up 5
+up 3
+down 1
+down 7
+down 6
+forward 4
+up 5
+forward 6
+up 8
+forward 3
+up 6
+forward 3
+down 5
+forward 8
+forward 8
+up 6
+forward 5
+down 4
+down 7
+down 5
+down 9
+forward 2
+forward 9
+down 9
+forward 8
+down 4
+forward 4
+forward 1
+down 8
+down 1
+down 9
+down 5
+forward 4
+up 6
+down 7
+down 1
+up 4
+down 4
+down 6
+down 4
+up 8
+down 2
+forward 1
+forward 4
+down 1
+down 1
+up 2
+forward 5
+up 5
+down 9
+forward 3
+down 3
+forward 6
+forward 4
+down 6
+forward 2
+up 7
+up 8
+up 2
+up 8
+up 8
+down 8
+forward 4
+down 1
+forward 2
+forward 9
+up 3
+down 2
+down 1
+up 4
+down 9
+down 6
+forward 9
+down 6
+down 8
+down 9
+forward 2
+up 7
+forward 1
+down 8
+down 7
+down 8
+up 3
+forward 3
+up 7
+forward 7
+down 9
+down 3
+down 2
+down 5
+forward 9
+up 1
+down 7
+forward 5
+forward 4
+forward 8
+up 6
+down 7
+forward 5
+up 5
+down 1
+forward 4
+forward 9
+down 2
+forward 8
+up 9
+down 8
+forward 8
+up 6
+forward 3
+forward 1
+up 5
+down 6
+forward 8
+up 7
+down 1
+down 7
+down 3
+forward 7
+down 9
+down 5
+down 2
+forward 2
+down 4
+down 1
+forward 8
+down 4
+up 1
+forward 4
+down 7
+forward 6
+down 5
+up 8
+forward 1
+forward 2
+up 5
+down 7
+forward 6
+down 7
+forward 6
+down 8
+down 2
+up 3
+down 6
+forward 1
+down 5
+down 8
+down 2
+forward 6
+up 4
+forward 4
+down 1
+forward 9
+forward 8
+forward 4
+forward 9
+down 8
+down 8
+up 1
+up 3
+down 8
+down 4
+forward 4
+down 6
+up 7
+down 8
+down 3
+down 2
+down 7
+down 5
+down 7
+forward 3
+down 6
+forward 6
+down 1
+forward 7
+down 9
+forward 2
+down 7
+forward 1
+up 9
+down 5
+up 6
+down 2
+up 4
+forward 5
+forward 2
+forward 6
+down 2
+forward 5
+down 3
+forward 6
+up 7
+up 5
+up 3
+up 5
+up 4
+down 8
+up 8
+forward 3
+down 2
+down 1
+forward 2
+down 8
+down 8
+down 1
+up 7
+down 1
+forward 6
+down 5
+forward 7
+up 3
+up 4
+forward 9
+down 3
+down 1
+down 6
+forward 1
+forward 6
+forward 2
+forward 5
+down 7
+down 5
+down 2
+forward 6
+down 7
+forward 2
+down 4
+down 8
+forward 2
+forward 2
+up 5
+forward 5
+down 5
+down 4
+up 7
+down 3
+forward 2
+down 7
+forward 2
+forward 5
+down 8
+up 3
+forward 4
+forward 4
+down 2
+down 9
+down 5
+forward 2
+forward 7
+down 7
+down 1
+down 5
+down 1
+down 4
+down 9
+down 2
+forward 9
+forward 8
+down 4
+forward 9
+down 9
+down 7
+down 6
+down 2
+forward 5
+down 4
+up 3
+forward 2
+up 5
+forward 7
+up 6
+forward 3
+forward 1
+forward 3
+down 7
+forward 8
+down 1
+forward 1
+forward 5
+forward 5
+up 4
+down 7
+down 8
+forward 4
+down 6
+forward 1
+forward 7
+down 5
+down 9
+forward 2
+down 3
+down 5
+forward 4
+forward 5
+forward 1
+down 1
+down 6
+down 9
+forward 1
+forward 1
+up 5
+forward 3
+up 3
+up 5
+down 2
+forward 6
+forward 3
+down 4
+up 3
+down 8
+down 4
+up 3
+down 7
+forward 7
+up 3
+down 3
+down 8
+down 4
+down 5
+down 3
+up 1
+forward 4
+forward 1
+down 5
+up 8
+up 9
+forward 1
+up 8
+down 3
+forward 8
+up 4
+forward 5
+forward 5
+up 1
+down 6
+down 2
+up 3
+up 7
+forward 7
+down 7
+up 9
+forward 2
+forward 8
+forward 2
+up 7
+forward 3
+down 8
+down 7
+forward 6
+down 9
+forward 4
+forward 9
+forward 2
+down 2
+down 6
+down 8
+forward 6
+down 5
+forward 7
+up 8
+down 6
+forward 5
+down 6
+forward 1
+forward 5
+forward 1
+up 9
+forward 3
+down 9
+down 7
+forward 3
+up 6
+down 9
+down 6
+down 9
+forward 3
+forward 5
+up 3
+up 9
+down 1
+down 8
+down 4
+forward 6
+down 9
+forward 4
+forward 3
+down 8
+forward 5
+forward 6
+forward 5
+forward 1
+forward 7
+forward 8
+down 4
+forward 9
+up 3
+down 3
+forward 6
+down 2
+forward 7
+down 2
+forward 8
+forward 3
diff --git a/2021/day02/main.rs b/2021/day02/main.rs
@@ -0,0 +1,64 @@
+enum Command {
+    Up(i32),
+    Down(i32),
+    Forward(i32),
+}
+
+fn parse_command(s: &str) -> Option<Command> {
+    let mut it = s.split(' ');
+    let dir = it.next().unwrap();
+    let dist = it.next().unwrap().parse::<i32>().unwrap();
+    match dir {
+        "up" => Some(Command::Up(dist)),
+        "down" => Some(Command::Down(dist)),
+        "forward" => Some(Command::Forward(dist)),
+        _ => None,
+    }
+}
+
+fn main() {
+    let input = std::fs::read_to_string("input.txt")
+        .unwrap()
+        .trim()
+        .split('\n')
+        .map(|cmd| parse_command(cmd).unwrap())
+        .collect::<Vec<Command>>();
+    // 1714950
+    println!("Part 1: {}", part_1(&input));
+    // 1281977850
+    println!("Part 2: {}", part_2(&input));
+}
+
+fn part_1(input: &Vec<Command>) -> i32 {
+    let mut depth = 0;
+    let mut pos = 0;
+
+    for cmd in input {
+        match cmd {
+            Command::Up(x) => depth -= x,
+            Command::Down(x) => depth += x,
+            Command::Forward(x) => pos += x,
+        }
+    }
+
+    depth * pos
+}
+
+fn part_2(input: &Vec<Command>) -> i32 {
+    let mut depth = 0;
+    let mut pos = 0;
+    let mut aim = 0;
+
+    for cmd in input {
+        match cmd {
+            Command::Up(x) => aim -= x,
+            Command::Down(x) => aim += x,
+            Command::Forward(x) => {
+                pos += x;
+                depth += aim * x;
+            }
+        }
+    }
+
+    depth * pos
+}