[2016][rust][1.0]

profile
Alpha Chen 8 years ago
parent ddbface9e5
commit b5e5566d5e

@ -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

@ -0,0 +1 @@
target

@ -0,0 +1,4 @@
[root]
name = "advent_of_code_2016"
version = "0.1.0"

@ -0,0 +1,6 @@
[package]
name = "advent_of_code_2016"
version = "0.1.0"
authors = ["Alpha Chen <alpha.chen@gmail.com>"]
[dependencies]

@ -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));
}
Loading…
Cancel
Save