summary refs log tree commit diff
path: root/11/src/main.rs
blob: 1ea63659a7b3db4ceffb24337eaf5165a622388f (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
use advent_lib::prelude::*;

use std::convert::TryFrom;


#[derive(Clone,Debug,PartialEq)]
pub enum CellState {
  Floor,
  EmptySeat,
  FullSeat,
}


pub fn debug_grid(grid: &Vec<Vec<CellState>>) {
  let mut output = String::new();

  for row in grid {
    for cell in row {
      match cell {
        CellState::Floor => {
          output.push_str(".");
        }
        CellState::EmptySeat => {
          output.push_str("L");
        }
        CellState::FullSeat => {
          output.push_str("#");
        }
      }
    }
    output.push_str("\n");
  }
  output.push_str("\n");
  println!("{}", output);
}


fn main() -> Result<()> {
  let mut args = std::env::args();
  if args.len() != 2 {
    eprintln!("Usage: advent input");
  }
  let _ = args.next();
  let filename = args.next().unwrap();

  let input = advent_lib::read_lines_file(&filename)?;

  let mut original_grid: Vec<Vec<CellState>> = Vec::new();

  for line in &input {
    let mut row: Vec<CellState> = Vec::new();

    for c in line.chars() {
      if c == 'L' {
        row.push(CellState::EmptySeat);
      } else {
        row.push(CellState::Floor);
      }
    }

    original_grid.push(row);
  }

  let mut grid = original_grid.clone();
  let nearby_seats = compute_nearby_seats(&original_grid);

  loop {
    let (any_changed, new_grid) = simulate_one(&grid);
    if !any_changed {
      break;
    }
    grid = new_grid;
  }

  println!("{}", count_full_seats(&grid));

  let mut grid = original_grid.clone();

  let mut n_iterations = 0;

  loop {
    let (any_changed, new_grid) = simulate_distancey_one(&grid, &nearby_seats);
    if !any_changed {
      break;
    }
    grid = new_grid;
    n_iterations += 1;
    if n_iterations % 1000 == 0 {
      println!("iteration {}", n_iterations);
      debug_grid(&grid);
    }
  }

  println!("{}", count_full_seats(&grid));

  Ok(())
}


fn simulate_one(input_grid: &Vec<Vec<CellState>>) -> (bool, Vec<Vec<CellState>>) {
  let mut output_grid: Vec<Vec<CellState>> = Vec::new();
  let mut any_changed = false;

  for y in 0 .. input_grid.len() {
    let mut output_row: Vec<CellState> = Vec::new();

    for x in 0 .. input_grid[y].len() {
      let mut adjacent_count = 0;

      let mut nearby_x_min = x;
      if nearby_x_min > 0 {
        nearby_x_min -= 1;
      }

      let mut nearby_x_max = x;
      if nearby_x_max + 1 < input_grid[y].len() {
        nearby_x_max += 1;
      }

      let mut nearby_y_min = y;
      if nearby_y_min > 0 {
        nearby_y_min -= 1;
      }

      let mut nearby_y_max = y;
      if nearby_y_max + 1 < input_grid.len() {
        nearby_y_max += 1;
      }

      for nearby_x in nearby_x_min .. nearby_x_max + 1 {
        for nearby_y in nearby_y_min .. nearby_y_max + 1 {
          if nearby_x == x && nearby_y == y {
            continue;
          }

          let nearby_cell = &input_grid[nearby_y][nearby_x];

          if *nearby_cell == CellState::FullSeat {
            adjacent_count += 1;
          }
        }
      }

      let input_cell = &input_grid[y][x];
      if *input_cell == CellState::Floor {
        output_row.push(CellState::Floor);
      } else if *input_cell == CellState::EmptySeat && adjacent_count == 0 {
        output_row.push(CellState::FullSeat);
        any_changed = true;
      } else if *input_cell == CellState::FullSeat && adjacent_count >= 4 {
        output_row.push(CellState::EmptySeat);
        any_changed = true;
      } else {
        output_row.push(input_cell.clone());
      }
    }

    output_grid.push(output_row);
  }

  (any_changed, output_grid)
}


fn simulate_distancey_one(
    input_grid: &Vec<Vec<CellState>>,
    nearby_seats: &Vec<Vec<Vec<(usize, usize)>>>)
  -> (bool, Vec<Vec<CellState>>)
{
  let mut output_grid: Vec<Vec<CellState>> = Vec::new();
  let mut any_changed = false;

  for y in 0 .. input_grid.len() {
    let mut output_row: Vec<CellState> = Vec::new();

    for x in 0 .. input_grid[y].len() {
      let mut adjacent_count = 0;

      for (nearby_x, nearby_y) in &nearby_seats[y][x] {
        let nearby_cell = &input_grid[*nearby_y][*nearby_x];

        if *nearby_cell == CellState::FullSeat {
          adjacent_count += 1;
        }
      }

      let input_cell = &input_grid[y][x];
      if *input_cell == CellState::Floor {
        output_row.push(CellState::Floor);
      } else if *input_cell == CellState::EmptySeat && adjacent_count == 0 {
        output_row.push(CellState::FullSeat);
        any_changed = true;
      } else if *input_cell == CellState::FullSeat && adjacent_count >= 5 {
        output_row.push(CellState::EmptySeat);
        any_changed = true;
      } else {
        output_row.push(input_cell.clone());
      }
    }

    output_grid.push(output_row);
  }

  (any_changed, output_grid)
}


fn compute_nearby_seats(input_grid: &Vec<Vec<CellState>>)
  -> Vec<Vec<Vec<(usize, usize)>>>
{
  let mut nearby_seats: Vec<Vec<Vec<(usize, usize)>>> = Vec::new();

  let mut max_dimension: isize = isize::try_from(input_grid.len()).unwrap();
  if isize::try_from(input_grid[0].len()).unwrap() > max_dimension {
    max_dimension = isize::try_from(input_grid[0].len()).unwrap();
  }

  for y in 0 .. input_grid.len() {
    let mut nearby_seats_row: Vec<Vec<(usize, usize)>> = Vec::new();

    for x in 0 .. input_grid[y].len() {
      let mut nearby_seats_one: Vec<(usize, usize)> = Vec::new();

      for (dx, dy) in [
        (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1) ].iter()
      {
        for distance in 1 .. max_dimension {
          let nearby_y = isize::try_from(y).unwrap() + dy * distance;
          let nearby_x = isize::try_from(x).unwrap() + dx * distance;

          if nearby_y < 0 || usize::try_from(nearby_y).unwrap() >= input_grid.len() {
            break;
          }

          if nearby_x < 0 || usize::try_from(nearby_x).unwrap()
              >= input_grid[usize::try_from(nearby_y).unwrap()].len()
          {
            break;
          }

          let nearby_cell = &input_grid[usize::try_from(nearby_y).unwrap()]
            [usize::try_from(nearby_x).unwrap()];

          if *nearby_cell == CellState::FullSeat || *nearby_cell == CellState::EmptySeat {
            nearby_seats_one.push((usize::try_from(nearby_x).unwrap(),
                usize::try_from(nearby_y).unwrap()));
            break;
          }
        }
      }

      nearby_seats_row.push(nearby_seats_one);
    }

    nearby_seats.push(nearby_seats_row);
  }

  nearby_seats
}


fn count_full_seats(grid: &Vec<Vec<CellState>>) -> i64 {
  let mut count = 0;

  for row in grid {
    for cell in row {
      if *cell == CellState::FullSeat {
        count += 1;
      }
    }
  }

  count
}