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::cmp::*;
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Tile { Walk, Wall, Skip }
#[allow(dead_code)]
impl Tile {
pub fn from_byte(b: u8) -> Tile {
match b {
b'.' => Tile::Walk,
b'#' => Tile::Wall,
b' ' => Tile::Skip,
_ => panic!("not a valid tile {}", b)
}
}
pub fn to_char(&self) -> char {
match self {
Tile::Walk => '.',
Tile::Wall => '#',
Tile::Skip => ' '
}
}
}
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Direction { Up, Down, Right, Left }
#[allow(dead_code)]
impl Direction {
pub fn from_char(c: char) -> Direction {
match c {
'U' => Direction::Up,
'D' => Direction::Down,
'R' => Direction::Right,
'L' => Direction::Left,
_ => panic!("invalid char for direction")
}
}
pub fn turn(&self, t: Direction) -> Direction {
match t {
Direction::Right => match self {
Direction::Up => Direction::Right,
Direction::Right => Direction::Down,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up
},
Direction::Left => match self {
Direction::Up => Direction::Left,
Direction::Left => Direction::Down,
Direction::Down => Direction::Right,
Direction::Right => Direction::Up
}
_ => panic!("cannot turn up or down")
}
}
pub fn step(&self) -> (i64, i64) {
match self {
Direction::Up => (-1, 0),
Direction::Down => (1, 0),
Direction::Right => (0, 1),
Direction::Left => (0, -1)
}
}
pub fn value(&self) -> usize {
match self {
Direction::Right => 0,
Direction::Down => 1,
Direction::Left => 2,
Direction::Up => 3
}
}
pub fn opposite(&self) -> Direction {
match self {
Direction::Right => Direction::Left,
Direction::Left => Direction::Right,
Direction::Up => Direction::Down,
Direction::Down => Direction::Up
}
}
pub fn is_positive(&self) -> bool {
*self == Direction::Down || *self == Direction::Right
}
}
pub fn read_instruction_line_from_stdin() -> (Vec<Direction>, Vec<usize>) {
let mut dir = vec![];
let mut step = vec![];
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
let mut i = 0;
while i < line.len()-1 {
match line.chars().nth(i).unwrap() {
'0'..='9' => {
let j = line[i..].find(|c: char| !c.is_digit(10)).unwrap() + i;
step.push(line[i..j].parse::<usize>().unwrap());
i = j;
},
c => {
dir.push(Direction::from_char(c));
i += 1;
}
}
}
(dir, step)
}
|