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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! # [Day 13: Packet Scanners](http://adventofcode.com/2017/day/13)
//!
//! You need to cross a vast *firewall*. The firewall consists of several
//! layers, each with a *security scanner* that moves back and forth across
//! the layer. To succeed, you must not be detected by a scanner.

use std::str::FromStr;
use std::ops;

/// Within each layer, a security scanner moves back and forth within its
/// range. Each security scanner starts at the top and moves down until it
/// reaches the bottom, then moves up until it reaches the top, and repeats.
/// A security scanner takes *one picosecond* to move one step. Drawing
/// scanners as `S`, the first few picoseconds look like this:
///
/// ```text
/// Picosecond 0:
///  0   1   2   3   4   5   6
/// [S] [S] ... ... [S] ... [S]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
/// Picosecond 1:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
/// Picosecond 2:
///  0   1   2   3   4   5   6
/// [ ] [S] ... ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
/// Picosecond 3:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] ... [ ]
/// [S] [S]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [S]     [S]
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Layer {
    range: usize,
    position: usize,
    forward: bool,
}

impl Layer {
    fn step(&mut self) {
        if self.forward {
            if self.position < self.range - 1 {
                self.position += 1;
            } else {
                self.forward = false;
                self.position -= 1;
            }
        } else {
            if self.position > 0 {
                self.position -= 1;
            } else {
                self.forward = true;
                self.position += 1;
            }
        }
    }
}

impl FromStr for Layer {
    type Err = ::std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Layer {
            range: s.parse::<usize>()?,
            position: 0,
            forward: true,
        })
    }
}

#[cfg(test)]
#[test]
fn test_layers() {
    let input = "0: 3
1: 2
4: 4
6: 4
";
    let mut firewall = Firewall::new(&input);

    // Picosecond 0
    assert_eq!(firewall[0].unwrap().position, 0);
    assert_eq!(firewall[1].unwrap().position, 0);
    assert_eq!(firewall[4].unwrap().position, 0);
    assert_eq!(firewall[6].unwrap().position, 0);

    // Picosecond 1
    firewall.step();
    assert_eq!(firewall[0].unwrap().position, 1);
    assert_eq!(firewall[1].unwrap().position, 1);
    assert_eq!(firewall[4].unwrap().position, 1);
    assert_eq!(firewall[6].unwrap().position, 1);

    // Picosecond 2
    firewall.step();
    assert_eq!(firewall[0].unwrap().position, 2);
    assert_eq!(firewall[1].unwrap().position, 0);
    assert_eq!(firewall[4].unwrap().position, 2);
    assert_eq!(firewall[6].unwrap().position, 2);

    // Picosecond 3
    firewall.step();
    assert_eq!(firewall[0].unwrap().position, 1);
    assert_eq!(firewall[1].unwrap().position, 1);
    assert_eq!(firewall[4].unwrap().position, 3);
    assert_eq!(firewall[6].unwrap().position, 3);
}

/// By studying the firewall briefly, you are able to record (in your puzzle
/// input) the *depth* of each layer and the *range* of the scanning area
/// for the scanner within it, written as `depth: range`. Each layer has a
/// thickness of exactly `1`. A layer at depth `0` begins immediately inside
/// the firewall; a layer at depth `1` would start immediately after that.
///
/// For example, suppose you've recorded the following:
///
/// ```text
/// 0: 3
/// 1: 2
/// 4: 4
/// 6: 4
/// ```
///
/// This means that there is a layer immediately inside the firewall (with
/// range `3`), a second layer immediately after that (with range `2`), a
/// third layer which begins at depth `4` (with range `4`), and a fourth
/// layer which begins at depth 6 (also with range `4`). Visually, it might
/// look like this:
///
/// ```text
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
/// ```
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Firewall(Vec<Option<Layer>>);

impl Firewall {
    fn new(input: &str) -> Firewall {
        Firewall(
            input.lines()
            .fold(Vec::new(), |mut firewall, line| {
                let mut vals = line.split(": ");
                let depth: usize = vals.next().map(FromStr::from_str).unwrap().unwrap();
                let layer = vals.next().map(FromStr::from_str).unwrap().unwrap();

                if firewall.len() < depth + 1 {
                    firewall.resize(depth + 1, None);
                }

                firewall[depth] = Some(layer);
                firewall
            })
        )
    }

    fn len(&self) -> usize {
        self.0.len()
    }

    pub fn step(&mut self) {
        for layer in &mut self.0 {
            layer.iter_mut().for_each(|x| x.step());
        }
    }
}

impl ops::Index<usize> for Firewall {
    type Output = Option<Layer>;
    fn index(&self, i: usize) -> &Self::Output {
        &self.0[i]
    }
}

impl ops::IndexMut<usize> for Firewall {
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        &mut self.0[i]
    }
}

#[cfg(test)]
#[test]
fn test_firewall() {
    let input = "0: 3
1: 2
4: 4
6: 4
";
    let firewall = Firewall::new(&input);

    assert_eq!(firewall.len(), 7);
    assert_eq!(firewall[0].unwrap().range, 3);
    assert_eq!(firewall[1].unwrap().range, 2);
    assert_eq!(firewall[2], None);
    assert_eq!(firewall[3], None);
    assert_eq!(firewall[4].unwrap().range, 4);
    assert_eq!(firewall[5], None);
    assert_eq!(firewall[6].unwrap().range, 4);
}

/// Your plan is to hitch a ride on a packet about to move through the
/// firewall. The packet will travel along the top of each layer, and it
/// moves at *one layer per picosecond*. Each picosecond, the packet moves
/// one layer forward (its first move takes it into layer 0), and then the
/// scanners move one step. If there is a scanner at the top of the layer
/// *as your packet enters it*, you are *caught*. (If a scanner moves into
/// the top of its layer while you are there, you are *not* caught: it
/// doesn't have time to notice you before you leave.) If you were to do
/// this in the configuration above, marking your current position with
/// parentheses, your passage through the firewall would look like this:
///
/// ```text
/// Initial state:
///  0   1   2   3   4   5   6
/// [S] [S] ... ... [S] ... [S]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
/// Picosecond 0:
///  0   1   2   3   4   5   6
/// (S) [S] ... ... [S] ... [S]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// ( ) [ ] ... ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 1:
///  0   1   2   3   4   5   6
/// [ ] ( ) ... ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] (S) ... ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
///
/// Picosecond 2:
///  0   1   2   3   4   5   6
/// [ ] [S] (.) ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] (.) ... [ ] ... [ ]
/// [S] [S]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [S]     [S]
///
///
/// Picosecond 3:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... (.) [ ] ... [ ]
/// [S] [S]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [S]     [S]
///
///  0   1   2   3   4   5   6
/// [S] [S] ... (.) [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [S]     [S]
///                 [ ]     [ ]
///
///
/// Picosecond 4:
///  0   1   2   3   4   5   6
/// [S] [S] ... ... ( ) ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [S]     [S]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... ( ) ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 5:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] (.) [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [S] ... ... [S] (.) [S]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 6:
///  0   1   2   3   4   5   6
/// [ ] [S] ... ... [S] ... (S)
/// [ ] [ ]         [ ]     [ ]
/// [S]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] ... ( )
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
/// ```
///
/// In this situation, you are *caught* in layers `0` and `6`, because your
/// packet entered the layer when its scanner was at the top when you
/// entered it. You are *not* caught in layer `1`, since the scanner moved
/// into the top of the layer once you were already there.
///
/// The *severity* of getting caught on a layer is equal to its *depth*
/// multiplied by its *range*. (Ignore layers in which you do not get
/// caught.) The severity of the whole trip is the sum of these values. In
/// the example above, the trip severity is `0*3 + 6*4 = 24`.
///
/// ```
/// # use advent_solutions::advent2017::day13::part1;
/// # let input = "0: 3
/// # 1: 2
/// # 4: 4
/// # 6: 4
/// # ";
/// assert_eq!(part1(&input), 24);
/// ```
///
/// Given the details of the firewall you've recorded, if you leave
/// immediately, *what is the severity of your whole trip*?
pub fn part1(input: &str) -> usize {
    let mut firewall = Firewall::new(&input);
    let mut severity = 0;

    for depth in 0..firewall.len() {
        if let Some(layer) = firewall[depth] {
            if layer.position == 0 {
                severity += depth * layer.range;
            }
        }

        firewall.step();
    }

    severity
}

/// Now, you need to pass through the firewall without being caught - easier
/// said than done.
///
/// You can't control the <span title="Seriously, what network stack doesn't
/// let you adjust the speed of light?">speed of the packet</span>, but you
/// can *delay* it any number of picoseconds. For each picosecond you delay
/// the packet before beginning your trip, all security scanners move one step.
/// You're not in the firewall during this time; you don't enter layer `0`
/// until you stop delaying the packet.
///
/// In the example above, if you delay `10` picoseconds (picoseconds `0` -
/// `9`), you won't get caught:
///
/// ```text
/// State after delaying:
///  0   1   2   3   4   5   6
/// [ ] [S] ... ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
/// Picosecond 10:
///  0   1   2   3   4   5   6
/// ( ) [S] ... ... [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// ( ) [ ] ... ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 11:
///  0   1   2   3   4   5   6
/// [ ] ( ) ... ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [S] (S) ... ... [S] ... [S]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 12:
///  0   1   2   3   4   5   6
/// [S] [S] (.) ... [S] ... [S]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] (.) ... [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///
/// Picosecond 13:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... (.) [ ] ... [ ]
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [S] ... (.) [ ] ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
///
/// Picosecond 14:
///  0   1   2   3   4   5   6
/// [ ] [S] ... ... ( ) ... [ ]
/// [ ] [ ]         [ ]     [ ]
/// [S]             [S]     [S]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... ( ) ... [ ]
/// [S] [S]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [S]     [S]
///
///
/// Picosecond 15:
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] (.) [ ]
/// [S] [S]         [ ]     [ ]
/// [ ]             [ ]     [ ]
///                 [S]     [S]
///
///  0   1   2   3   4   5   6
/// [S] [S] ... ... [ ] (.) [ ]
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [S]     [S]
///                 [ ]     [ ]
///
///
/// Picosecond 16:
///  0   1   2   3   4   5   6
/// [S] [S] ... ... [ ] ... ( )
/// [ ] [ ]         [ ]     [ ]
/// [ ]             [S]     [S]
///                 [ ]     [ ]
///
///  0   1   2   3   4   5   6
/// [ ] [ ] ... ... [ ] ... ( )
/// [S] [S]         [S]     [S]
/// [ ]             [ ]     [ ]
///                 [ ]     [ ]
/// ```
///
/// Because all smaller delays would get you caught, the fewest number of
/// picoseconds you would need to delay to get through safely is `10`.
///
/// ```
/// # use advent_solutions::advent2017::day13::part2;
/// # let input = "0: 3
/// # 1: 2
/// # 4: 4
/// # 6: 4
/// # ";
/// assert_eq!(part2(&input), 10);
/// ```
///
/// *What is the fewest number of picoseconds* that you need to delay the
/// packet to pass through the firewall without being caught?
pub fn part2(input: &str) -> usize {
    let firewall = Firewall::new(&input);

    for delay in 0.. {
        let caught = firewall.0.iter()
            .enumerate()
            .any(|(i, layer)| match *layer {
                Some(Layer { range, .. }) => (delay + i) % (range * 2 - 2) == 0,
                None => false,
            });

        if !caught {
            return delay;
        }
    }

    unreachable!();
}

pub fn parse_input(input: &str) -> &str {
    input
}

test_day!("13", 648, 3933124);