From 452531e4bca4d33230aa058048493e7f81e29830 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Fri, 2 Dec 2016 08:01:18 -0800 Subject: [PATCH] [2016][rust] organize files --- 2016/rust/src/day_01.rs | 43 ++++++++++++++++++++++++++++++++++++++ 2016/rust/src/lib.rs | 1 + 2016/rust/src/main.rs | 46 +++-------------------------------------- 3 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 2016/rust/src/day_01.rs create mode 100644 2016/rust/src/lib.rs diff --git a/2016/rust/src/day_01.rs b/2016/rust/src/day_01.rs new file mode 100644 index 0000000..b4597da --- /dev/null +++ b/2016/rust/src/day_01.rs @@ -0,0 +1,43 @@ +use std::collections::HashSet; +use std::f32::consts::PI; + +pub fn solve(input: &str) -> String { + let mut turtle = (-PI / 2.0, (0, 0)); + let mut visited: HashSet<(i32, i32)> = HashSet::new(); + + 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!(); + } + }; + + for _ in 0..blocks { + let movement = turtle.0.sin_cos(); + (turtle.1).0 += movement.1 as i32; + (turtle.1).1 += movement.0 as i32; + + if visited.contains(&turtle.1) { + return ((turtle.1).0.abs() + (turtle.1).1.abs()).to_string() + } else { + visited.insert(turtle.1); + } + } + } + + ((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()); + assert_eq!(solve("R8, R4, R4, R8"), "4".to_string()); +} diff --git a/2016/rust/src/lib.rs b/2016/rust/src/lib.rs new file mode 100644 index 0000000..83a7f75 --- /dev/null +++ b/2016/rust/src/lib.rs @@ -0,0 +1 @@ +pub mod day_01; diff --git a/2016/rust/src/main.rs b/2016/rust/src/main.rs index 07ea0d4..3742d65 100644 --- a/2016/rust/src/main.rs +++ b/2016/rust/src/main.rs @@ -1,52 +1,12 @@ -use std::collections::HashSet; 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)); - let mut visited: HashSet<(i32, i32)> = HashSet::new(); - - 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!(); - } - }; - - for _ in 0..blocks { - let movement = turtle.0.sin_cos(); - (turtle.1).0 += movement.1 as i32; - (turtle.1).1 += movement.0 as i32; - - if visited.contains(&turtle.1) { - return ((turtle.1).0.abs() + (turtle.1).1.abs()).to_string() - } else { - visited.insert(turtle.1); - } - } - } - - ((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()); - assert_eq!(solve("R8, R4, R4, R8"), "4".to_string()); -} +extern crate advent_of_code_2016; +use advent_of_code_2016::*; fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).ok(); - println!("{}", solve(&input)); + println!("{}", day_01::solve(&input)); }