-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
164 lines (132 loc) · 4.08 KB
/
11.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
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::time::{Instant, Duration};
//const INPUT_PATH: &str = "11.input.sample";
//const WIDTH: i32 = 10;
//const HEIGHT: i32 = 10;
const INPUT_PATH: &str = "11.input";
const WIDTH: usize = 92;
const HEIGHT: usize = 94;
const DIRECTIONS: [(i32, i32); 8] = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1),
];
#[derive(Copy, Clone)]
struct Coord {
x: usize,
y: usize,
}
struct Seat {
coord: Coord,
visible_coords: Vec<Coord>,
}
fn main() {
let coords = parse_input();
let start = Instant::now();
let seats = seat_data(&coords);
solve(&seats);
let duration = start.elapsed();
print_duration(duration);
//print_seats(&seats);
}
fn print_duration(duration: Duration) {
if duration.as_micros() < 1000 {
println!("{} microseconds", duration.as_micros());
} else {
println!("{} milliseconds", duration.as_millis());
}
}
fn solve(seats: &Vec<Seat>) {
let mut current_grid = [[false; WIDTH]; HEIGHT];
let mut next_grid = [[false; WIDTH]; HEIGHT];
let mut any_changed = true;
let mut iteration_count = 0;
while any_changed {
any_changed = false;
iteration_count += 1;
for seat in seats {
let coord = seat.coord;
let current_value = current_grid[coord.y][coord.x];
let mut occupied_count = 0;
for visible in &seat.visible_coords {
if current_grid[visible.y][visible.x] {
occupied_count += 1;
if !current_value { break; }
if current_value && occupied_count >= 5 { break; }
}
}
let mut next_value = current_value;
if current_value && occupied_count >= 5 {
next_value = false;
any_changed = true;
} else if !current_value && occupied_count == 0 {
next_value = true;
any_changed = true;
}
next_grid[coord.y][coord.x] = next_value;
}
let temp_grid = current_grid;
current_grid = next_grid;
next_grid = temp_grid;
}
let num_occupied_seats = current_grid.iter().fold(0, |grid_acc, &row|
grid_acc + row.iter().fold(0, |row_acc, seat|
row_acc + match *seat {true => 1, false => 0}
)
);
println!("iteration_count:{} occupied_seats:{}", iteration_count, num_occupied_seats);
}
#[allow(dead_code)]
fn print_seats(seats: &Vec<Seat>) {
for seat in seats {
print!("({}, {}): ", seat.coord.x, seat.coord.y);
for visible in &seat.visible_coords {
print!("({}, {}) ", visible.x, visible.y);
}
println!("");
}
}
fn seat_data(coords: &Vec<Coord>) -> Vec<Seat> {
let mut seat_data = Vec::with_capacity(coords.len());
let mut grid = [[false; WIDTH]; HEIGHT];
for coord in coords {
grid[coord.y][coord.x] = true;
}
for coord in coords {
let mut visible_coords = Vec::with_capacity(8);
for direction in &DIRECTIONS {
let mut x = coord.x as i32;
let mut y = coord.y as i32;
loop {
x += direction.0;
y += direction.1;
if (x < 0) || (x >= WIDTH as i32) || (y < 0) || (y >= HEIGHT as i32) {
break;
}
if grid[y as usize][x as usize] {
visible_coords.push(Coord {x: x as usize, y: y as usize});
break;
}
}
}
seat_data.push(Seat {
coord: *coord,
visible_coords: visible_coords
})
}
seat_data
}
fn parse_input() -> Vec<Coord> {
let mut coord = vec![];
let file = File::open(INPUT_PATH).unwrap();
let reader = BufReader::new(file);
for (y, line) in reader.lines().enumerate() {
for (x, char) in line.unwrap().chars().enumerate() {
if char == 'L' {
coord.push(Coord {x: x, y: y});
}
}
}
coord
}