summaryrefslogtreecommitdiffstats
path: root/day09a/src/main.rs
blob: efe843c3aecbae020da3be9ad009050b0e3b00e4 (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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/// --- Day 9: Rope Bridge ---
///
/// This rope bridge creaks as you walk along it.  You aren't sure how old it is, or whether it can
/// even support your weight.
///
/// It seems to support the Elves just fine, though.  The bridge spans a gorge which was carved out
/// by the massive river far below you.
///
/// You step carefully; as you do, the ropes stretch and twist.  You decide to distract yourself by
/// modeling rope physics; maybe you can even figure out where not to step.
///
/// Consider a rope with a knot at each end; these knots mark the head and the tail of the rope.  If
/// the head moves far enough away from the tail, the tail is pulled toward the head.
///
/// Due to nebulous reasoning involving Planck lengths, you should be able to model the positions
/// of the knots on a two-dimensional grid.  Then, by following a hypothetical series of motions
/// (your puzzle input) for the head, you can determine how the tail will move.
///
/// Due to the aforementioned Planck lengths, the rope must be quite short; in fact, the head (H)
/// and tail (T) must always be touching (diagonally adjacent and even overlapping both count as
/// touching):
///
/// ```
/// ....
/// .TH.
/// ....
///
/// ....
/// .H..
/// ..T.
/// ....
///
/// ...
/// .H. (H covers T)
/// ...
/// ```
///
/// If the head is ever two steps directly up, down, left, or right from the tail, the tail must
/// also move one step in that direction so it remains close enough:
///
/// ```
/// .....    .....    .....
/// .TH.. -> .T.H. -> ..TH.
/// .....    .....    .....
///
/// ...    ...    ...
/// .T.    .T.    ...
/// .H. -> ... -> .T.
/// ...    .H.    .H.
/// ...    ...    ...
/// ```
///
/// Otherwise, if the head and tail aren't touching and aren't in the same row or column, the tail
/// always moves one step diagonally to keep up:
///
/// ```
/// .....    .....    .....
/// .....    ..H..    ..H..
/// ..H.. -> ..... -> ..T..
/// .T...    .T...    .....
/// .....    .....    .....
///
/// .....    .....    .....
/// .....    .....    .....
/// ..H.. -> ...H. -> ..TH.
/// .T...    .T...    .....
/// .....    .....    .....
/// ```
///
/// You just need to work out where the tail goes as the head follows a series of motions.  Assume
/// the head and the tail both start at the same position, overlapping.
///
/// For example:
///
/// ```
/// R 4
/// U 4
/// L 3
/// D 1
/// R 4
/// D 1
/// L 5
/// R 2
/// ```
///
/// This series of motions moves the head right four steps, then up four steps, then left three
/// steps, then down one step, and so on.  After each step, you'll need to update the position of
/// the tail if the step means the head is no longer adjacent to the tail.  Visually, these motions
/// occur as follows (s marks the starting position as a reference point):
///
/// ```
/// == Initial State ==
///
/// ......
/// ......
/// ......
/// ......
/// H.....  (H covers T, s)
///
/// == R 4 ==
///
/// ......
/// ......
/// ......
/// ......
/// TH....  (T covers s)
///
/// ......
/// ......
/// ......
/// ......
/// sTH...
///
/// ......
/// ......
/// ......
/// ......
/// s.TH..
///
/// ......
/// ......
/// ......
/// ......
/// s..TH.
///
/// == U 4 ==
///
/// ......
/// ......
/// ......
/// ....H.
/// s..T..
///
/// ......
/// ......
/// ....H.
/// ....T.
/// s.....
///
/// ......
/// ....H.
/// ....T.
/// ......
/// s.....
///
/// ....H.
/// ....T.
/// ......
/// ......
/// s.....
///
/// == L 3 ==
///
/// ...H..
/// ....T.
/// ......
/// ......
/// s.....
///
/// ..HT..
/// ......
/// ......
/// ......
/// s.....
///
/// .HT...
/// ......
/// ......
/// ......
/// s.....
///
/// == D 1 ==
///
/// ..T...
/// .H....
/// ......
/// ......
/// s.....
///
/// == R 4 ==
///
/// ..T...
/// ..H...
/// ......
/// ......
/// s.....
///
/// ..T...
/// ...H..
/// ......
/// ......
/// s.....
///
/// ......
/// ...TH.
/// ......
/// ......
/// s.....
///
/// ......
/// ....TH
/// ......
/// ......
/// s.....
///
/// == D 1 ==
///
/// ......
/// ....T.
/// .....H
/// ......
/// s.....
///
/// == L 5 ==
///
/// ......
/// ....T.
/// ....H.
/// ......
/// s.....
///
/// ......
/// ....T.
/// ...H..
/// ......
/// s.....
///
/// ......
/// ......
/// ..HT..
/// ......
/// s.....
///
/// ......
/// ......
/// .HT...
/// ......
/// s.....
///
/// ......
/// ......
/// HT....
/// ......
/// s.....
///
/// == R 2 ==
///
/// ......
/// ......
/// .H....  (H covers T)
/// ......
/// s.....
///
/// ......
/// ......
/// .TH...
/// ......
/// s.....
/// ```
///
/// After simulating the rope, you can count up all of the positions the tail visited at least
/// once.  In this diagram, s again marks the starting position (which the tail also visited) and #
/// marks other positions the tail visited:
///
/// ```
/// ..##..
/// ...##.
/// .####.
/// ....#.
/// s###..
/// ```
///
/// So, there are 13 positions the tail visited at least once.
///
/// Simulate your complete hypothetical series of motions.  How many positions does the tail of the
/// rope visit at least once?
use clap::Parser;
use itertools::Itertools;

use std::collections::HashSet;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;

const FILEPATH: &'static str = "examples/input.txt";

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
    #[clap(short, long, default_value = FILEPATH)]
    file: PathBuf,
}

#[derive(Copy, Clone, Debug)]
enum Direction {
    Up,
    Down,
    Left,
    Right,
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct Coord {
    x: i32,
    y: i32,
}

#[derive(Clone, Debug)]
struct Command {
    dir: Direction,
    amount: u32,
}

#[derive(Clone, Debug)]
struct State {
    head: Coord,
    tail: Coord,
    tail_visited: HashSet<Coord>,
}

impl Coord {
    fn new() -> Self {
        Self { x: 0, y: 0 }
    }
}

impl Command {
    fn parse(dir: &str, amount: &str) -> Self {
        use Direction::*;
        let d = match dir {
            "U" => Up,
            "D" => Down,
            "L" => Left,
            "R" => Right,
            _ => panic!("unknown direction {}", dir),
        };
        Self {
            dir: d,
            amount: amount.parse::<u32>().unwrap(),
        }
    }
}

impl State {
    fn new() -> Self {
        let mut hs = HashSet::new();
        hs.insert(Coord::new());
        State {
            head: Coord::new(),
            tail: Coord::new(),
            tail_visited: hs,
        }
    }

    fn update_tail(&mut self) {
        match (self.head.x - self.tail.x, self.head.y - self.tail.y) {
            (0, 0)
            | (1, 0)
            | (-1, 0)
            | (0, 1)
            | (0, -1)
            | (1, 1)
            | (-1, -1)
            | (1, -1)
            | (-1, 1) => return,
            (xdiff, ydiff) => {
                self.tail.x += i32::signum(xdiff);
                self.tail.y += i32::signum(ydiff);
                self.tail_visited.insert(self.tail);
            },
        }
    }

    fn shift(&mut self, dir: Direction) {
        use Direction::*;
        match dir {
            Up => self.head.y += 1,
            Down => self.head.y -= 1,
            Right => self.head.x += 1,
            Left => self.head.x -= 1,
        }
        self.update_tail();
    }

    fn process(&mut self, cmd: &Command) {
        for _ in 0..cmd.amount {
            self.shift(cmd.dir);
        }
    }
}

fn main() {
    let args = Cli::parse();

    let file = File::open(&args.file).unwrap();
    let reader = BufReader::new(file);
    let mut state = State::new();
    let _ = reader
        .lines()
        .map(|l| {
            l.unwrap()
                .split_whitespace()
                .map(|s| s.to_owned())
                .collect_tuple::<(String, String)>()
                .unwrap()
        })
        .map(|(dir, amount)| Command::parse(dir.as_str(), amount.as_str()))
        .scan(&mut state, |state, cmd| {
            state.process(&cmd);
            Some(())
        })
        .last();

    let res = state.tail_visited.len();
    println!("{res:?}");
}