summary refs log tree commit diff
path: root/17/src/main.rs
blob: 9924b02c28479285eaa89d5b501dc492d5cd74b7 (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
use advent_lib::prelude::*;

use std::convert::TryFrom;


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 input = advent_lib::group_lines_by_blanks(input);

  let mut initial_state: Vec<Vec<bool>> = Vec::new();

  for line in &input[0] {
    let mut row = Vec::new();
    for c in line.chars() {
      if c == '#' {
        row.push(true);
      } else {
        row.push(false);
      }
    }
    initial_state.push(row);
  }

  do_4d(&initial_state, false);
  do_4d(&initial_state, true);

  Ok(())
}


fn do_4d(initial_state: &Vec<Vec<bool>>, use_w: bool) {
  let max_iterations = 6;

  let mut hyperspace = init_4d(initial_state, max_iterations, use_w);

  for _ in 0 .. max_iterations {
    hyperspace = step_4d(&hyperspace, use_w);
  }

  let active_count = count_active_4d(&hyperspace);
  println!("{}", active_count);
}


fn init_4d(initial_state: &Vec<Vec<bool>>, max_iterations: usize, use_w: bool)
  -> Vec<Vec<Vec<Vec<bool>>>>
{
  let initial_height = initial_state.len();
  let initial_width = initial_state[0].len();

  let mut hyperspace: Vec<Vec<Vec<Vec<bool>>>> = Vec::new();
  for w in 0 .. max_iterations * 2 + 1 {
    if w > 0 && !use_w {
      continue;
    }

    let mut space = Vec::new();
    for z in 0 .. max_iterations * 2 + 1 {
      let mut plane = Vec::new();
      for y in 0 .. initial_height + max_iterations * 2 + 1 {
        let y_shifted = isize::try_from(y).unwrap()
          - isize::try_from(max_iterations).unwrap();

        let mut row = Vec::new();
        for x in 0 .. initial_width + max_iterations * 2 + 1 {
          let x_shifted = isize::try_from(x).unwrap()
            - isize::try_from(max_iterations).unwrap();

          if ((use_w && w == max_iterations) || (!use_w && w == 0))
            && z == max_iterations
            && y_shifted >= 0 && y_shifted < isize::try_from(initial_height).unwrap()
            && x_shifted >= 0 && x_shifted < isize::try_from(initial_width).unwrap()
          {
            row.push(initial_state[usize::try_from(y_shifted).unwrap()]
                                  [usize::try_from(x_shifted).unwrap()]);
          } else {
            row.push(false);
          }
        }

        plane.push(row);
      }

      space.push(plane);
    }

    hyperspace.push(space);
  }

  hyperspace
}


fn step_4d(input_hyperspace: &Vec<Vec<Vec<Vec<bool>>>>, use_w: bool)
  -> Vec<Vec<Vec<Vec<bool>>>>
{
  let mut output_hyperspace = Vec::new();

  for w in 0 .. input_hyperspace.len() {
    if w > 0 && !use_w {
      continue;
    }

    let input_space = &input_hyperspace[w];
    let mut output_space = Vec::new();

    for z in 0 .. input_space.len() {
      let input_plane = &input_space[z];
      let mut output_plane = Vec::new();

      for y in 0 .. input_plane.len() {
        let input_row = &input_plane[y];
        let mut output_row = Vec::new();

        for x in 0 .. input_row.len() {
          let mut adjacent_count = 0;

          for dw in [-1, 0, 1].iter() {
            for dz in [-1, 0, 1].iter() {
              for dy in [-1, 0, 1].iter() {
                for dx in [-1, 0, 1].iter() {
                  if *dw == 0 && *dz == 0 && *dy == 0 && *dx == 0 {
                   continue;
                  }

                  let shifted_w = isize::try_from(w).unwrap() + dw;
                  let shifted_z = isize::try_from(z).unwrap() + dz;
                  let shifted_y = isize::try_from(y).unwrap() + dy;
                  let shifted_x = isize::try_from(x).unwrap() + dx;

                  if shifted_w < 0
                    || shifted_w >= isize::try_from(input_hyperspace.len()).unwrap()
                  {
                    continue;
                  }
                  if shifted_z < 0
                    || shifted_z >= isize::try_from(input_space.len()).unwrap()
                  {
                    continue;
                  }
                  if shifted_y < 0
                    || shifted_y >= isize::try_from(input_plane.len()).unwrap()
                  {
                    continue;
                  }
                  if shifted_x < 0
                    || shifted_x >= isize::try_from(input_row.len()).unwrap()
                  {
                    continue;
                  }

                  if input_hyperspace[usize::try_from(shifted_w).unwrap()]
                                [usize::try_from(shifted_z).unwrap()]
                                [usize::try_from(shifted_y).unwrap()]
                                [usize::try_from(shifted_x).unwrap()]
                  {
                    adjacent_count += 1;
                  }
                }
              }
            }
          }

          let mut output_cell = false;

          if input_row[x] {
            if adjacent_count >= 2 && adjacent_count <= 3 {
              output_cell = true;
            }
          } else {
            if adjacent_count == 3 {
              output_cell = true;
            }
          }

          output_row.push(output_cell);
        }

        output_plane.push(output_row);
      }

      output_space.push(output_plane);
    }

    output_hyperspace.push(output_space);
  }

  output_hyperspace
}


fn count_active_4d(hyperspace: &Vec<Vec<Vec<Vec<bool>>>>) -> usize {
  let mut count = 0;

  for space in hyperspace {
    for plane in space {
      for row in plane {
        for cell in row {
          if *cell {
            count += 1;
          }
        }
      }
    }
  }

  count
}


pub fn debug_4d(hyperspace: &Vec<Vec<Vec<Vec<bool>>>>, use_w: bool) {
  let mut output = String::new();

  for w in 0 .. hyperspace.len() {
    if w > 0 && !use_w {
      continue;
    }

    let space = &hyperspace[w];
    for z in 0 .. space.len() {
      let plane = &space[z];

      if w > 0 || z > 0 {
        output.push_str("\n");
      }
      output.push_str(&format!("w: {}, z: {}\n", w, z));

      for row in plane {
        for cell in row {
          if *cell {
            output.push_str("#");
          } else {
            output.push_str(".");
          }
        }
        output.push_str("\n");
      }
    }
  }
  println!("{}", output);
}