summary refs log tree commit diff
path: root/02/src
diff options
context:
space:
mode:
Diffstat (limited to '02/src')
-rw-r--r--02/src/main.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/02/src/main.rs b/02/src/main.rs
new file mode 100644
index 0000000..92ba083
--- /dev/null
+++ b/02/src/main.rs
@@ -0,0 +1,70 @@
+use advent_lib::prelude::*;
+
+
+fn main() -> Result<()> {
+  let mut args = std::env::args();
+  if args.len() != 2 {
+    eprintln!("Usage: advent input");
+  }
+  let _ = args.next();
+  let filename = args.next().unwrap();
+
+  let input = advent_lib::read_lines_file(&filename)?;
+
+  {
+    let mut depth = 0;
+    let mut horizontal = 0;
+
+    for line in &input {
+      let mut words = line.split(' ');
+      let command = words.next().unwrap();
+      let parameter = words.next().unwrap().parse::<i64>().unwrap();
+
+      match command {
+        "forward" => {
+          horizontal += parameter;
+        },
+        "down" => {
+          depth += parameter;
+        },
+        "up" => {
+          depth -= parameter;
+        },
+        _ => { },
+      }
+    }
+
+    println!("{}", horizontal * depth);
+  }
+
+  {
+    let mut aim = 0;
+    let mut depth = 0;
+    let mut horizontal = 0;
+
+    for line in &input {
+      let mut words = line.split(' ');
+      let command = words.next().unwrap();
+      let parameter = words.next().unwrap().parse::<i64>().unwrap();
+
+      match command {
+        "forward" => {
+          horizontal += parameter;
+          depth += aim * parameter;
+        },
+        "down" => {
+          aim += parameter;
+        },
+        "up" => {
+          aim -= parameter;
+        },
+        _ => { },
+      }
+    }
+
+    println!("{}", horizontal * depth);
+  }
+
+  Ok(())
+}
+