Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2016 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f48630c3cf376ca2b2a0ed203d0bafda to your computer and use it in GitHub Desktop.
Save anonymous/f48630c3cf376ca2b2a0ed203d0bafda to your computer and use it in GitHub Desktop.
Shared via Rust Playground
fn rotate(direction: &mut Vec<i32>, turn: &str){
let x = direction[0];
let y = direction[1];
if turn == "L" {
direction[1] = x;
direction[0] = -y;
} else {
direction[1] = -x;
direction[0] = y;
}
}
fn doit () -> Result<i32, std::num::ParseIntError>{
let x = "L1, L5, R1, R3, L4, L5, R5, R1, L2, L2, L3, R4, L2, R3, R1, L2, R5, R3, L4, R4, L3, R3, R3, L2, R1, L3, R2, L1, R4, L2, R4, L4, R5, L3, R1, R1, L1, L3, L2, R1, R3, R2, L1, R4, L4, R2, L189, L4, R5, R3, L1, R47, R4, R1, R3, L3, L3, L2, R70, L1, R4, R185, R5, L4, L5, R4, L1, L4, R5, L3, R2, R3, L5, L3, R5, L1, R5, L4, R1, R2, L2, L5, L2, R4, L3, R5, R1, L5, L4, L3, R4, L3, L4, L1, L5, L5, R5, L5, L2, L1, L2, L4, L1, L2, R3, R1, R1, L2, L5, R2, L3, L5, L4, L2, L1, L2, R3, L1, L4, R3, R3, L2, R5, L1, L3, L3, L3, L5, R5, R1, R2, L3, L2, R4, R1, R1, R3, R4, R3, L3, R3, L5, R2, L2, R4, R5, L4, L3, L1, L5, L1, R1, R2, L1, R3, R4, R5, R2, R3, L2, L1, L5";
let instructions = x.split(", ");
let mut direction = vec![1, 0];
let mut position = vec![0, 0];
for instruction in instructions {
let (turn, distance_str) = instruction.split_at(1);
let distance = try!(distance_str.parse::<i32>());
rotate(&mut direction, turn);
position[0] += direction[0] * distance;
position[1] += direction[1] * distance;
}
return Ok(position[0].abs() + position[1].abs())
}
fn main (){
match doit() {
Ok(v) => println!("distance = {:?}", v),
Err(e) => println!("failed: {:?}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment