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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
use std::collections::{HashMap, HashSet};
static DAY: u8 = 6;
fn main() {
let input = advent::read_lines(DAY);
println!("{DAY}a: {}", Map::new(&input).distinct_positions());
println!("{DAY}b: {}", 0);
}
#[derive(Eq, PartialEq, Hash, Clone)]
struct Position {
x: isize,
y: isize,
}
#[derive(Eq, PartialEq)]
enum Object {
Obstruction,
Floor,
Guard,
}
enum Direction {
Up,
Down,
Left,
Right,
}
impl Direction {
fn turn_right(&self) -> Direction {
match *self {
Direction::Up => Direction::Right,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
Direction::Right => Direction::Down,
}
}
}
impl Object {
fn new(c: char) -> Object {
match c {
'#' => Object::Obstruction,
'.' => Object::Floor,
'^' => Object::Guard,
_ => unimplemented!(),
}
}
}
struct Guard {
pos: Position,
direction: Direction,
}
impl Guard {
fn next_pos(&self) -> Position {
match self.direction {
Direction::Up => Position { x: self.pos.x, y: self.pos.y - 1 },
Direction::Down => Position { x: self.pos.x, y: self.pos.y + 1 },
Direction::Left => Position { x: self.pos.x - 1, y: self.pos.y },
Direction::Right => Position { x: self.pos.x + 1, y: self.pos.y },
}
}
}
struct Map {
map: HashMap<Position, Object>,
guard: Guard,
max_dimension: Position,
}
impl Map {
fn new(input: &[String]) -> Map {
let mut map = HashMap::new();
let mut guard = Guard { pos: Position { x: 0, y: 0 }, direction: Direction::Up };
for (y, line) in input.iter().enumerate() {
for (x, c) in line.chars().enumerate() {
let pos = Position { x: x as isize, y: y as isize };
let obj = Object::new(c);
if obj == Object::Guard {
guard.pos = pos.clone();
map.insert(pos, Object::Floor);
} else {
map.insert(pos, Object::new(c));
}
}
}
Map { map, guard, max_dimension: Position { x: input[0].len() as isize, y: input.len() as isize } }
}
fn distinct_positions(&mut self) -> usize {
let mut visited = HashSet::new();
loop {
let next_pos = self.guard.next_pos();
if next_pos.x < 0 || next_pos.x >= self.max_dimension.x || next_pos.y < 0 || next_pos.y >= self.max_dimension.y {
break;
}
match self.map.get(&next_pos).unwrap() {
Object::Floor => {
self.guard.pos = next_pos.clone();
visited.insert(next_pos);
},
Object::Obstruction => {
self.guard.direction = self.guard.direction.turn_right();
continue;
},
_ => unimplemented!(),
}
}
visited.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = [
"....#.....",
".........#",
"..........",
"..#.......",
".......#..",
"..........",
".#..^.....",
"........#.",
"#.........",
"......#...",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(Map::new(&input).distinct_positions(), 41);
}
}
|