Skip to content

Instantly share code, notes, and snippets.

@kngwyu
Created June 2, 2016 15:41
Show Gist options
  • Save kngwyu/26478affd96c6856c68cb6f2a356cdda to your computer and use it in GitHub Desktop.
Save kngwyu/26478affd96c6856c68cb6f2a356cdda to your computer and use it in GitHub Desktop.
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