Day 1.0
change
commit 97b34cd57acd3567741e773de95aa595d3097207
author Alpha Chen <alpha.chen@gmail.com>
date
parent af554e7b
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 919a7ec..83b29ae 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -1,4 +1,5 @@
 use std::io::Error;
+use std::str::Chars;
 
 pub trait Day {
     fn new(String) -> Self;
@@ -15,6 +16,49 @@ impl Day for Day01 {
     }
 
     fn solve(self) -> Result<i32, Error> {
-        Ok(1)
+        let elevator = Elevator::new(self.input);
+        Ok(elevator.run().last().unwrap_or(0))
+    }
+}
+
+struct Elevator {
+    instructions: String,
+}
+
+impl<'a> Elevator {
+    fn new(input: String) -> Self {
+        Elevator { instructions: input }
+    }
+
+    fn run(self: &'a Self) -> ElevatorIterator<'a> {
+        ElevatorIterator {
+            chars: self.instructions.chars(),
+            floor: 0,
+        }
+    }
+}
+
+struct ElevatorIterator<'a> {
+    chars: Chars<'a>,
+    floor: i32,
+}
+
+impl<'a> Iterator for ElevatorIterator<'a> {
+    type Item = i32;
+
+    fn next(&mut self) -> Option<i32> {
+        match self.chars.next() {
+            Some('(') => {
+                self.floor += 1;
+                Some(self.floor)
+            },
+            Some(')') => {
+                self.floor -= 1;
+                Some(self.floor)
+            },
+            _ => {
+                None
+            },
+        }
     }
 }