From b5e5566d5e4fe59cef379440cbd9029ea8754bbd Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 1 Dec 2016 15:45:35 -0800 Subject: [PATCH] [2016][rust][1.0] --- 2016/input/day_01.txt | 1 + 2016/rust/.gitignore | 1 + 2016/rust/Cargo.lock | 4 ++++ 2016/rust/Cargo.toml | 6 ++++++ 2016/rust/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 2016/input/day_01.txt create mode 100644 2016/rust/.gitignore create mode 100644 2016/rust/Cargo.lock create mode 100644 2016/rust/Cargo.toml create mode 100644 2016/rust/src/main.rs diff --git a/2016/input/day_01.txt b/2016/input/day_01.txt new file mode 100644 index 0000000..f3c7fb8 --- /dev/null +++ b/2016/input/day_01.txt @@ -0,0 +1 @@ +R4, R3, L3, L2, L1, R1, L1, R2, R3, L5, L5, R4, L4, R2, R4, L3, R3, L3, R3, R4, R2, L1, R2, L3, L2, L1, R3, R5, L1, L4, R2, L4, R3, R1, R2, L5, R2, L189, R5, L5, R52, R3, L1, R4, R5, R1, R4, L1, L3, R2, L2, L3, R4, R3, L2, L5, R4, R5, L2, R2, L1, L3, R3, L4, R4, R5, L1, L1, R3, L5, L2, R76, R2, R2, L1, L3, R189, L3, L4, L1, L3, R5, R4, L1, R1, L1, L1, R2, L4, R2, L5, L5, L5, R2, L4, L5, R4, R4, R5, L5, R3, L1, L3, L1, L1, L3, L4, R5, L3, R5, R3, R3, L5, L5, R3, R4, L3, R3, R1, R3, R2, R2, L1, R1, L3, L3, L3, L1, R2, L1, R4, R4, L1, L1, R3, R3, R4, R1, L5, L2, R2, R3, R2, L3, R4, L5, R1, R4, R5, R4, L4, R1, L3, R1, R3, L2, L3, R1, L2, R3, L3, L1, L3, R4, L4, L5, R3, R5, R4, R1, L2, R3, R5, L5, L4, L1, L1 \ No newline at end of file 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 "] + +[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::().parse::().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)); +}