Created
June 2, 2016 15:41
-
-
Save kngwyu/26478affd96c6856c68cb6f2a356cdda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
let s = next_line(); | |
let s = s.trim(); | |
let t = getint(); | |
let mut cnt = 0; | |
let mut x = 0; | |
let mut y = 0; | |
for c in s.chars() { | |
match c { | |
'L' => x = x - 1, | |
'R' => x = x + 1, | |
'D' => y = y - 1, | |
'U' => y = y + 1, | |
_ => cnt = cnt + 1, | |
} | |
} | |
let x = if x < 0 { x * -1} else {x}; | |
let y = if y < 0 { y * -1} else {y}; | |
if t == 1 { | |
println!("{}", x + y + cnt); | |
} else { | |
let dist = x + y - cnt; | |
let dist = if dist < 0 {(dist * -1) % 2} else { dist }; | |
println!("{}", dist); | |
} | |
} | |
fn next_line() -> String { | |
let mut input = String::new(); | |
io::stdin().read_line(&mut input).unwrap(); | |
input | |
} | |
fn getint() -> i32 { | |
let res = next_line(); | |
let res = res.trim().parse().unwrap(); | |
res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment