-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
262 lines (243 loc) · 7.36 KB
/
main.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#[macro_use]
extern crate lazy_static;
#[derive(Debug, PartialEq, Clone)]
enum SeatState {
Floor,
Empty,
Occupied,
}
use SeatState::*;
lazy_static! {
static ref DIRECTIONS: Vec<(isize, isize)> = vec![
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];
}
#[derive(Debug)]
struct SeatMap {
pub seats: Vec<SeatState>,
pub next_seats: Vec<SeatState>,
pub row_count: usize,
pub row_len: usize,
}
impl SeatMap {
pub fn new(s: &str) -> SeatMap {
let input = s.split('\n').collect::<Vec<_>>();
let row_len = &input[0].len();
let row_count = &input.len();
let seats = input
.iter()
.flat_map(|row| {
row.chars().map(|c| match c {
'L' => Empty,
'.' => Floor,
'#' => Occupied,
_ => panic!(),
})
})
.collect::<Vec<SeatState>>();
SeatMap {
seats: seats,
next_seats: Vec::with_capacity(row_count * row_len),
row_count: *row_count,
row_len: *row_len,
}
}
pub fn get(&self, (row, col): (usize, usize)) -> &SeatState {
self.seats.get(self.row_len * row + col).unwrap()
}
pub fn neighbors(&self, (row, col): (usize, usize)) -> Vec<&SeatState> {
let res = DIRECTIONS
.iter()
.filter(|(dx, dy)| {
row as isize + dx >= 0
&& row as isize + dx < self.row_count as isize
&& col as isize + dy >= 0
&& col as isize + dy < self.row_len as isize
})
.map(|(dx, dy)| self.get(((row as isize + dx) as usize, (col as isize + dy) as usize)))
.collect::<Vec<&SeatState>>();
res
}
pub fn visible_seat(
&self,
(row, col): (usize, usize),
(dx, dy): (isize, isize),
) -> Option<SeatState> {
let mut cur_row = row as isize;
let mut cur_col = col as isize;
loop {
cur_row = cur_row + dx;
cur_col = cur_col + dy;
if cur_row >= 0
&& cur_col >= 0
&& cur_row < self.row_count as isize
&& cur_col < self.row_len as isize
{
match self.get((cur_row as usize, cur_col as usize)) {
Occupied => {
return Some(Occupied);
}
Empty => {
return Some(Empty);
}
Floor => {
// noop
}
};
} else {
return None;
}
}
}
pub fn visible_seats(&self, pos: (usize, usize)) -> Vec<SeatState> {
DIRECTIONS
.iter()
.filter_map(|dir| self.visible_seat(pos, *dir))
.collect::<Vec<SeatState>>()
}
fn calc_next(&self) -> Vec<SeatState> {
(0..self.row_count)
.flat_map(|row| {
(0..self.row_len).map(move |col| {
let pos = (row, col);
let cur = self.get(pos);
match cur {
Floor => Floor,
Empty => {
if self
.neighbors(pos)
.iter()
.all(|s| **s == Empty || **s == Floor)
{
Occupied
} else {
Empty
}
}
Occupied => {
if self
.neighbors(pos)
.iter()
.filter(|s| ***s == Occupied)
.count()
>= 4
{
Empty
} else {
Occupied
}
}
}
})
})
.collect::<Vec<SeatState>>()
}
fn calc_next2(&self) -> Vec<SeatState> {
(0..self.row_count)
.flat_map(|row| {
(0..self.row_len).map(move |col| {
let pos = (row, col);
let cur = self.get(pos);
match cur {
Floor => Floor,
Empty => {
if self
.visible_seats(pos)
.iter()
.all(|s| *s == Empty || *s == Floor)
{
Occupied
} else {
Empty
}
}
Occupied => {
if self
.visible_seats(pos)
.iter()
.filter(|s| **s == Occupied)
.count()
>= 5
{
Empty
} else {
Occupied
}
}
}
})
})
.collect::<Vec<SeatState>>()
}
pub fn apply_rules(&mut self) -> bool {
let next = self.calc_next();
if self.seats != next {
self.seats = next;
true
} else {
false
}
}
pub fn apply_rules_part2(&mut self) -> bool {
let next = self.calc_next2();
if self.seats != next {
self.seats = next;
true
} else {
false
}
}
pub fn occupied_count(&self) -> usize {
(0..self.row_count)
.flat_map(|row| (0..self.row_len).map(move |col| self.get((row, col))))
.filter(|s| **s == Occupied)
.count()
}
#[allow(dead_code)]
pub fn print(&self) {
for row in 0..self.row_count {
let s: String = (0..self.row_len)
.map(|col| self.get((row, col)))
.map(|state| match state {
Occupied => '#',
Empty => 'L',
Floor => '.',
})
.collect();
println!("{}", s);
}
}
}
fn main() {
let input = include_str!("../in.txt");
let mut seat_map1 = SeatMap::new(input);
loop {
if !&seat_map1.apply_rules() {
break;
}
}
println!("Part 1: {}", &seat_map1.occupied_count());
let mut seat_map2 = SeatMap::new(input);
loop {
if !&seat_map2.apply_rules_part2() {
break;
}
}
println!("Part 2: {}", &seat_map2.occupied_count());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_visible1() {
let seat_map = SeatMap::new(include_str!("../test_visible1.txt"));
assert_eq!(seat_map.visible_seats((3, 3)), vec![]);
}
}