-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwhere-am-i-going.rs
More file actions
111 lines (88 loc) · 2.38 KB
/
where-am-i-going.rs
File metadata and controls
111 lines (88 loc) · 2.38 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::io;
macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}
fn read_int() -> i32 {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
parse_input!(input_line, i32)
}
fn read_str() -> String {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').to_string()
}
struct Pos {
x: i32,
y: i32
}
impl Pos {
fn move_dir(&mut self, dir: &Pos) {
self.x += dir.x;
self.y += dir.y;
}
fn copy(&self) -> Pos {
Pos { x:self.x, y: self.y }
}
}
fn advance(pos: &mut Pos, dir: &Pos, grid: &mut Vec<Vec<char>>) -> bool {
let mut tmp_pos = pos.copy();
tmp_pos.move_dir(dir);
if '#' == grid[tmp_pos.x as usize][tmp_pos.y as usize] {
grid[tmp_pos.x as usize][tmp_pos.y as usize] = '.';
*pos = tmp_pos.copy();
return true
}
false
}
fn get_diff(heading: &i32) -> Pos {
match heading {
0 => Pos{ x: -1, y: 0}, // North
1 => Pos{ x: 0, y: 1}, // East
2 => Pos{ x: 1, y: 0}, // South
_ => Pos{ x: 0, y: -1} // West
}
}
fn determine_turn(heading: &mut i32, pos: &mut Pos, grid: &mut Vec<Vec<char>>) -> char {
let heading_next = (*heading + 1) % 4;
if advance(pos, &get_diff(&heading_next), grid) {
*heading = heading_next;
return 'R'
}
let heading_next = (*heading + 3) % 4;
if advance(pos, &get_diff(&heading_next), grid) {
*heading = heading_next;
return 'L'
}
'U'
}
fn main() {
let h = read_int();
let w = read_int();
let mut grid = Vec::<Vec<char>>::new();
grid.push(vec!['.'; (w + 2) as usize]);
for _ in 0..h {
let mut row: Vec<char> = vec!['.'];
row.extend(read_str().chars());
row.push('.');
grid.push(row);
}
grid.push(vec!['.'; (w + 2) as usize]);
let mut heading = 1;
let mut pos = Pos{ x: 1, y: 0 };
let mut dir = get_diff(&heading);
let mut repeat = 0;
loop {
while advance(&mut pos, &dir, &mut grid) {
repeat+=1;
}
print!("{}", repeat);
let turn = determine_turn(&mut heading, &mut pos, &mut grid);
if 'U' == turn {
break;
}
print!("{}", turn);
repeat = 1;
dir = get_diff(&heading);
}
}