[2016][rust][1.0]
change ommxplyusqvzqomwzxxqlnkswqpskxsp
commit 43784a9a645c7b60ff8267f01a2e74c58e23976d
author Alpha Chen <alpha.chen@gmail.com>
date
parent tyqmusty
diff --git a/2016/rust/.gitignore b/2016/rust/.gitignore
new file mode 100644
index 0000000..eb5a316
--- /dev/null
+++ b/2016/rust/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/2016/rust/Cargo.lock b/2016/rust/Cargo.lock
new file mode 100644
index 0000000..5abecf0
--- /dev/null
+++ b/2016/rust/Cargo.lock
@@ -0,0 +1,4 @@
+[root]
+name = "advent_of_code_2016"
+version = "0.1.0"
+
diff --git a/2016/rust/Cargo.toml b/2016/rust/Cargo.toml
new file mode 100644
index 0000000..5f59c0b
--- /dev/null
+++ b/2016/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "advent_of_code_2016"
+version = "0.1.0"
+authors = ["Alpha Chen <alpha.chen@gmail.com>"]
+
+[dependencies]
diff --git a/2016/rust/src/main.rs b/2016/rust/src/main.rs
new file mode 100644
index 0000000..de5a945
--- /dev/null
+++ b/2016/rust/src/main.rs
@@ -0,0 +1,41 @@
+use std::io;
+use std::io::Read;
+use std::f32::consts::PI;
+
+fn solve(input: &str) -> String {
+  let mut turtle = (-PI / 2.0, (0, 0));
+
+  for step in input.split(", ") {
+    let mut chars = step.chars();
+    let dir = chars.next().unwrap();
+    let blocks = chars.collect::<String>().parse::<f32>().unwrap();
+
+    turtle.0 += match dir {
+      'R' => PI / 2.0,
+      'L' => -PI / 2.0,
+      _ => {
+        unreachable!();
+      }
+    };
+
+    let movement = turtle.0.sin_cos();
+    (turtle.1).0 += (blocks*movement.1) as i32;
+    (turtle.1).1 += (blocks*movement.0) as i32;
+  }
+
+  ((turtle.1).0.abs() + (turtle.1).1.abs()).to_string()
+}
+
+#[test]
+fn test_solve() {
+  assert_eq!(solve("R2, L3"), "5".to_string());
+  assert_eq!(solve("R2, R2, R2"), "2".to_string());
+  assert_eq!(solve("R5, L5, R5, R3"), "12".to_string());
+}
+
+fn main() {
+  let mut input = String::new();
+  io::stdin().read_to_string(&mut input).ok();
+
+  println!("{}", solve(&input));
+}