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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright 2023 Mario Finelli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Advent of Code 2023 Day 14: <https://adventofcode.com/2023/day/14>
//!
//! Today's challenge was not terribly difficult. After a relatively easy part
//! one, part two is a classic Advent of Code problem where in order to avoid
//! needing to run one billion (expensive) iterations of a loop we can instead
//! find a cycle that we are sure that there will be because of the way that
//! the input is crafted.

use std::collections::HashMap;

/// The solution for the day fourteen challenge.
///
/// We take the input as a string and an integer for the part which changes
/// whether we do a single tilt or 1,000,000,000 cycles of tilt north, west,
/// south, and then east. We start by parsing the input into a
/// [`std::collections::HashMap`] grid of, cube-shaped rocks, rounded rocks,
/// and empty spaces. Then, in part one we just run a single north tilt and
/// then count the load. In part two we do one billion cycles but we keep track
/// of the end states that we've seen and at which count in the cycle so that
/// when we find the loop, we can short-circuit the loop and then we count the
/// load.
///
/// # Example
/// ```rust
/// # use aoc::y23d14::y23d14;
/// // probably read this from the input file...
/// let input = "...\n.#.\nO..";
/// assert_eq!(y23d14(input, 1), 3);
/// assert_eq!(y23d14(input, 2), 1);
/// ```
pub fn y23d14(input: &str, part: u32) -> i32 {
    let mut total = 0;
    let mut map = HashMap::new();
    let mut seen = HashMap::new();

    let lines: Vec<_> = input.lines().collect();
    let rows: i32 = lines.len().try_into().unwrap();
    let mut cols: i32 = 0;

    for (y, line) in lines.iter().enumerate() {
        let y: i32 = y.try_into().unwrap();

        for (x, c) in line.chars().enumerate() {
            if y == 0 {
                cols = x.try_into().unwrap();
            }

            let x: i32 = x.try_into().unwrap();

            map.insert((x, y), c);
        }
    }

    cols += 1;

    if part == 1 {
        tilt('N', rows, cols, &mut map);
    } else {
        let cycles = 1000000000;
        for cycle in 1..cycles + 1 {
            tilt('N', rows, cols, &mut map);
            tilt('W', rows, cols, &mut map);
            tilt('S', rows, cols, &mut map);
            tilt('E', rows, cols, &mut map);

            let grid = map_to_string(rows, cols, &map);
            if let Some(seen_at) = seen.insert(grid, cycle) {
                if (cycles - cycle) % (cycle - seen_at) == 0 {
                    break;
                }
            }
        }
    }

    for ((_, y), c) in map {
        if c == 'O' {
            total += rows - y;
        }
    }

    total
}

/// This function is responsible for modifying given map to its state after
/// applying a tilt in the desired direction. It basically works by going
/// row-by-row or column-by-column depending on the direction and then checking
/// each space until we find a boulder and then attempting to move it as far
/// as possible (either until we reach an edge, or another boulder) based on
/// what is currently in its adjacent tile.
fn tilt(dir: char, rows: i32, cols: i32, map: &mut HashMap<(i32, i32), char>) {
    if dir == 'N' {
        for x in 0..cols {
            for y in 0..rows {
                let c = map.get(&(x, y)).unwrap();

                if *c == 'O' {
                    let mut current = y;

                    loop {
                        match map.get(&(x, current - 1)) {
                            None => break,
                            Some(above) => {
                                if *above == 'O' || *above == '#' {
                                    break;
                                }

                                map.insert((x, current - 1), 'O');
                                map.insert((x, current), '.');

                                current -= 1;
                            }
                        }
                    }
                }
            }
        }
    } else if dir == 'S' {
        for x in 0..cols {
            for y in 0..rows {
                let y = rows - 1 - y;
                let c = map.get(&(x, y)).unwrap();

                if *c == 'O' {
                    let mut current = y;

                    loop {
                        match map.get(&(x, current + 1)) {
                            None => break,
                            Some(below) => {
                                if *below == 'O' || *below == '#' {
                                    break;
                                }

                                map.insert((x, current + 1), 'O');
                                map.insert((x, current), '.');

                                current += 1;
                            }
                        }
                    }
                }
            }
        }
    } else if dir == 'E' {
        for y in 0..rows {
            for x in 0..cols {
                let x = cols - 1 - x;
                let c = map.get(&(x, y)).unwrap();

                if *c == 'O' {
                    let mut current = x;

                    loop {
                        match map.get(&(current + 1, y)) {
                            None => break,
                            Some(right) => {
                                if *right == 'O' || *right == '#' {
                                    break;
                                }

                                map.insert((current + 1, y), 'O');
                                map.insert((current, y), '.');

                                current += 1;
                            }
                        }
                    }
                }
            }
        }
    } else {
        for y in 0..rows {
            for x in 0..cols {
                let c = map.get(&(x, y)).unwrap();

                if *c == 'O' {
                    let mut current = x;

                    loop {
                        match map.get(&(current - 1, y)) {
                            None => break,
                            Some(left) => {
                                if *left == 'O' || *left == '#' {
                                    break;
                                }

                                map.insert((current - 1, y), 'O');
                                map.insert((current, y), '.');

                                current -= 1;
                            }
                        }
                    }
                }
            }
        }
    }
}

/// This function returns the grid as its string representation which is useful
/// for debugging but also used to store in our list of seen states when we're
/// trying to find a cycle.
fn map_to_string(
    rows: i32,
    cols: i32,
    map: &HashMap<(i32, i32), char>,
) -> String {
    let mut s = "".to_string();

    for y in 0..rows {
        for x in 0..cols {
            s = format!("{}{}", s, map.get(&(x, y)).unwrap());
        }

        s = format!("{}\n", s);
    }

    s
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_map_to_string() {
        let grid = HashMap::from([
            ((0, 0), '.'),
            ((1, 0), '#'),
            ((2, 0), '#'),
            ((0, 1), '.'),
            ((1, 1), 'O'),
            ((2, 1), '.'),
            ((0, 2), '.'),
            ((1, 2), 'O'),
            ((2, 2), '.'),
        ]);
        assert_eq!(map_to_string(3, 3, &grid), ".##\n.O.\n.O.\n");
    }

    #[test]
    fn test_tilt() {
        let mut grid = HashMap::from([
            ((0, 0), '.'),
            ((1, 0), '#'),
            ((2, 0), '#'),
            ((0, 1), '.'),
            ((1, 1), '.'),
            ((2, 1), 'O'),
            ((0, 2), 'O'),
            ((1, 2), '.'),
            ((2, 2), '.'),
        ]);

        tilt('N', 3, 3, &mut grid);

        assert_eq!(*grid.get(&(0, 0)).unwrap(), 'O');
        assert_eq!(*grid.get(&(1, 0)).unwrap(), '#');
        assert_eq!(*grid.get(&(2, 0)).unwrap(), '#');
        assert_eq!(*grid.get(&(0, 1)).unwrap(), '.');
        assert_eq!(*grid.get(&(1, 1)).unwrap(), '.');
        assert_eq!(*grid.get(&(2, 1)).unwrap(), 'O');
        assert_eq!(*grid.get(&(0, 2)).unwrap(), '.');
        assert_eq!(*grid.get(&(1, 2)).unwrap(), '.');
        assert_eq!(*grid.get(&(2, 2)).unwrap(), '.');
    }

    #[test]
    fn it_works() {
        let input = concat!(
            "O....#....\n",
            "O.OO#....#\n",
            ".....##...\n",
            "OO.#O....O\n",
            ".O.....O#.\n",
            "O.#..O.#.#\n",
            "..O..#O..O\n",
            ".......O..\n",
            "#....###..\n",
            "#OO..#....\n",
        );

        assert_eq!(y23d14(input, 1), 136);
        assert_eq!(y23d14(input, 2), 64);
    }

    #[test]
    fn the_solution() {
        let contents = fs::read_to_string("input/2023/day14.txt").unwrap();

        assert_eq!(y23d14(&contents, 1), 109098);
        assert_eq!(y23d14(&contents, 2), 100064);
    }
}