-
Notifications
You must be signed in to change notification settings - Fork 7
/
12.rs
47 lines (43 loc) · 1.5 KB
/
12.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io::stdin;
const DIRS: [(i32, i32); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];
fn part1(actions: &Vec<(String, i32)>) -> i32 {
let (mut y, mut x, mut dir) = (0, 0, 0);
for (action, num) in actions {
match action.as_str() {
"N" => y -= num,
"S" => y += num,
"W" => x -= num,
"E" => x += num,
"F" => {y += DIRS[dir as usize].0 * num; x += DIRS[dir as usize].1 * num },
"R" => dir = (dir + num / 90 + 4) % 4,
"L" => dir = (dir - num / 90 + 4) % 4,
_ => panic!(),
}
}
y.abs() + x.abs()
}
fn part2(actions: &Vec<(String, i32)>) -> i32 {
let (mut y, mut x) = (0, 0);
let (mut wpy, mut wpx) = (-1, 10);
for (action, num) in actions {
match (action.as_str(), num) {
("N", _) => wpy -= num,
("S", _) => wpy += num,
("W", _) => wpx -= num,
("E", _) => wpx += num,
("F", _) => {y += wpy * num; x += wpx * num },
("R", 90) | ("L", 270) => {(wpy, wpx) = (wpx, -wpy)},
("R", 270) | ("L", 90) => {(wpy, wpx) = (-wpx, wpy)},
("R", 180) | ("L", 180) => {(wpy, wpx) = (-wpy, -wpx)},
_ => panic!(),
}
}
y.abs() + x.abs()
}
fn main() {
let actions: Vec<(String, i32)> = stdin().lines()
.filter_map(Result::ok)
.map(|l| (l[0..1].to_owned(), l[1..l.len()].parse().unwrap()))
.collect();
println!("{}\n{}", part1(&actions), part2(&actions));
}