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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! # [Day 22: Sporifica Virus](http://adventofcode.com/2017/day/22)
//!
//! Diagnostics indicate that the local *grid computing cluster* has been
//! contaminated with the *Sporifica Virus*. The grid computing cluster is a
//! seemingly-<span title="The infinite is possible at AdventOfCodeCom.">
//! infinite</span> two-dimensional grid of compute nodes. Each node is either
//! *clean* or *infected* by the virus.

use std::{ fmt, ops };
use std::convert::TryFrom;
use std::collections::VecDeque;

use ::Direction;
use ::Direction::*;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum Node { Clean, Weakened, Infected, Flagged }

use self::Node::*;

impl TryFrom<char> for Node {
    type Error = &'static str;

    fn try_from(c: char) -> Result<Self, Self::Error> {
        match c {
            '.' => Ok(Clean),
            'W' => Ok(Weakened),
            '#' => Ok(Infected),
            'F' => Ok(Flagged),
            _ => Err("Invalid node character"),
        }
    }
}

impl Into<char> for Node {
    fn into(self) -> char {
        match self {
            Clean => '.',
            Weakened => 'W',
            Infected => '#',
            Flagged => 'F',
        }
    }
}

impl<'a> Into<char> for &'a Node {
    fn into(self) -> char {
        match *self {
            Clean => '.',
            Weakened => 'W',
            Infected => '#',
            Flagged => 'F',
        }
    }
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", Into::<char>::into(self))
    }
}

/// Diagnostics have also provided a *map of the node infection status*
/// (your puzzle input). *Clean* nodes are shown as `.`; *infected* nodes
/// are shown as `#`. This map only shows the center of the grid; there are
/// many more nodes beyond those shown, but none of them are currently
/// infected.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Memory {
    memory: VecDeque<VecDeque<Node>>,
    origin: (usize, usize),
}

impl Memory {
    fn parse(input: &str) -> Memory {
        let memory = input.lines()
            .map(|line| line.chars()
                .map(|c| Node::try_from(c).unwrap())
                .collect::<VecDeque<_>>()
            )
            .collect::<VecDeque<_>>();

        let height = memory.len();
        let width = memory[0].len();

        Memory {
            memory,
            origin: (height / 2, width / 2),
        }
    }

    fn width(&self) -> usize {
        self.memory[0].len()
    }

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

impl ops::Index<(isize, isize)> for Memory {
    type Output = Node;
    fn index(&self, (x, y): (isize, isize)) -> &Self::Output {
        let ny = self.origin.0 as isize + y;
        let nx = self.origin.1 as isize + x;

        if ny >= self.height() as isize
            || ny < 0
            || nx >= self.width() as isize
            || nx < 0 {
            return &Clean;
        }

        &self.memory[ny as usize][nx as usize]
    }
}

impl ops::IndexMut<(isize, isize)> for Memory {
    fn index_mut(&mut self, (x, y): (isize, isize)) -> &mut Self::Output {
        let ny = self.origin.0 as isize + y;
        let nx = self.origin.1 as isize + x;

        if ny >= self.height() as isize {
            let grow = ny - self.height() as isize + 1;

            for _ in 0..grow {
                let mut row = VecDeque::new();
                row.resize(self.width(), Clean);
                self.memory.push_back(row);
            }
        } else if ny < 0 {
            let grow = ny.abs();

            for _ in 0..grow {
                let mut row = VecDeque::new();
                row.resize(self.width(), Clean);
                self.memory.push_front(row);
            }

            self.origin.0 += grow as usize;
        }

        let ny = self.origin.0 as isize + y;

        if nx >= self.width() as isize {
            let grow = nx - self.width() as isize + 1;

            self.memory.iter_mut().for_each(|row| for _ in 0..grow {
                row.push_back(Clean)
            });
        } else if nx < 0 {
            let grow = nx.abs();

            self.memory.iter_mut().for_each(|row| for _ in 0..grow {
                row.push_front(Clean)
            });

            self.origin.1 += grow as usize;
        }

        let nx = self.origin.1 as isize + x;

        &mut self.memory[ny as usize][nx as usize]
    }
}

impl fmt::Display for Memory {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.memory.iter()
            .map(|line| {
                line.iter().map(|x| write!(f, "{}", x))
                    .skip_while(|x| x.is_ok())
                    .next()
                    .unwrap_or(Ok(()))
                    .and_then(|_| write!(f, "\n"))
            })
            .skip_while(|x| x.is_ok())
            .next()
            .unwrap_or(Ok(()))
            .and_then(|_| (0..self.width())
                .map(|i| write!(f, "{}", if i == self.origin.1 { "|" } else { " " }))
                .skip_while(|x| x.is_ok())
                .next()
                .unwrap_or(Ok(()))
                .and_then(|_| write!(f, "\n"))
            )
            .and_then(|_| write!(f, "\n"))
    }
}

/// To [prevent overloading] the nodes (which would render them useless to
/// the virus) or detection by system administrators, exactly one *virus
/// carrier* moves through the network, infecting or cleaning nodes as it
/// moves. The virus carrier is always located on a single node in the
/// network (the *current node*) and keeps track of the *direction* it is
/// facing.
///
/// The virus carrier begins in the middle of the map facing *up*.
///
///   [prevent overloading]: https://en.wikipedia.org/wiki/Morris_worm#The_mistake
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Carrier {
    position: (isize, isize),
    facing: Direction,
    infected: usize,
}

impl Carrier {
    fn new() -> Carrier {
        Carrier {
            position: (0, 0),
            facing: Up,
            infected: 0,
        }
    }

    /// To avoid detection, the virus carrie/// r works in bursts; in each burst, it
    /// *wakes up*, does some *work*, and goes back to *sleep*. The following
    /// steps are all executed *in order* one time each burst:
    ///
    /// -   If the *current node* is *infected*, it turns to its *right*.
    ///     Otherwise, it turns to its *left*. (Turning is done in-place; the
    ///     *current node* does not change.)
    /// -   If the *current node* is *clean*, it becomes *infected*. Otherwise,
    ///     it becomes *cleaned*. (This is done *after* the node is considered
    ///     for the purposes of changing direction.)
    /// -   The virus carrier [moves] *forward* one node in the direction it is
    ///     facing.
    ///
    ///   [moves]: https://www.youtube.com/watch?v=2vj37yeQQHg
    fn part1_with_bursts(&mut self, memory: &mut Memory) {
        match memory[self.position] {
            Clean => {
                self.facing = self.facing.ccw();
                memory[self.position] = Infected;
                self.infected += 1;
            },
            Infected => {
                self.facing = self.facing.cw();
                memory[self.position] = Clean;
            },
            _ => panic!("Step 1 should not have weakened or flagged nodes"),
        };

        self.position += self.facing;
    }

    /// As you go to remove the virus from the infected nodes, it *evolves* to
    /// resist your attempt.
    ///
    /// Now, before it infects a clean node, it will *weaken* it to disable your
    /// defenses. If it encounters an infected node, it will instead *flag* the
    /// node to be cleaned in the future. So:
    ///
    /// -   *Clean* nodes become *weakened*.
    /// -   *Weakened* nodes become *infected*.
    /// -   *Infected* nodes become *flagged*.
    /// -   *Flagged* nodes become *clean*.
    ///
    /// Every node is always in exactly one of the above states.
    ///
    /// The virus carrier still functions in a similar way, but now uses the
    /// following logic during its bursts of action:
    ///
    /// -   Decide which way to turn based on the *current node*:
    ///     -   If it is *clean*, it turns *left*.
    ///     -   If it is *weakened*, it does *not* turn, and will continue
    ///         moving in the same direction.
    ///     -   If it is *infected*, it turns *right*.
    ///     -   If it is *flagged*, it *reverses* direction, and will go back
    ///         the way it came.
    /// -   Modify the state of the *current node*, as described above.
    /// -   The virus carrier moves *forward* one node in the direction it is
    ///     facing.
    pub fn part2_with_bursts(&mut self, memory: &mut Memory) {
        match memory[self.position] {
            Clean => {
                self.facing = self.facing.ccw();
                memory[self.position] = Weakened;
            },
            Weakened => {
                memory[self.position] = Infected;
                self.infected += 1;
            },
            Infected => {
                self.facing = self.facing.cw();
                memory[self.position] = Flagged;
            },
            Flagged => {
                self.facing = self.facing.reverse();
                memory[self.position] = Clean;
            },
        };

        self.position += self.facing;
    }
}


/// For example, suppose you are given a map like this:
///
/// ```text
/// ..#
/// #..
/// ...
/// ```
///
/// Then, the middle of the infinite grid looks like this, with the virus
/// carrier's position marked with `[ ]`:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . # . . .
/// . . . #[.]. . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// The virus carrier is on a *clean* node, so it turns *left*, *infects*
/// the node, and moves left:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . # . . .
/// . . .[#]# . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// The virus carrier is on an *infected* node, so it turns *right*,
/// *cleans* the node, and moves up:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . .[.]. # . . .
/// . . . . # . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// Four times in a row, the virus carrier finds a *clean*, *infects* it,
/// turns *left*, and moves forward, ending in the same place and still
/// facing up:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . #[#]. # . . .
/// . . # # # . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// Now on the same node as before, it sees an infection, which causes it to
/// turn *right*, *clean* the node, and move forward:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . # .[.]# . . .
/// . . # # # . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// After the above actions, a total of `7` bursts of activity had taken
/// place. Of them, `5` bursts of activity caused an infection.
///
/// After a total of `70`, the grid looks like this, with the virus carrier
/// facing up:
///
/// ```text
/// . . . . . # # . .
/// . . . . # . . # .
/// . . . # . . . . #
/// . . # . #[.]. . #
/// . . # . # . . # .
/// . . . . . # # . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// By this time, `41` bursts of activity caused an infection (though most
/// of those nodes have since been cleaned).
///
/// ```
/// # use advent_solutions::advent2017::day22::part1_with_bursts;
/// # let input = "..#
/// # #..
/// # ...
/// # ";
/// assert_eq!(part1_with_bursts(input, 70), 41);
/// ```
///
/// After a total of `10000` bursts of activity, `5587` bursts will have
/// caused an infection.
///
/// ```
/// # use advent_solutions::advent2017::day22::part1_with_bursts;
/// # let input = "..#
/// # #..
/// # ...
/// # ";
/// assert_eq!(part1_with_bursts(input, 10000), 5587);
/// ```
///
/// Given your actual map, after `10000` bursts of activity, *how many
/// bursts cause a node to become infected*? (Do not count nodes that begin
/// infected.)
pub fn part1_with_bursts(input: &str, bursts: usize) -> usize {
    let mut memory = Memory::parse(input);
    let mut carrier = Carrier::new();

    for _ in 0..bursts {
        carrier.part1_with_bursts(&mut memory);
    }

    carrier.infected
}

pub fn part1(input: &str) -> usize {
    part1_with_bursts(input, 10000)
}

/// Start with the same map (still using `.` for *clean* and `#` for
/// infected) and still with the virus carrier starting in the middle and
/// facing *up*.
///
/// Using the same initial state as the previous example, and drawing
/// *weakened* as `W` and *flagged* as `F`, the middle of the infinite grid
/// looks like this, with the virus carrier's position again marked with
/// `[ ]`:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . # . . .
/// . . . #[.]. . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// This is the same as before, since no initial nodes are *weakened* or
/// *flagged*. The virus carrier is on a clean node, so it still turns left,
/// instead *weakens* the node, and moves left:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . # . . .
/// . . .[#]W . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// The virus carrier is on an infected node, so it still turns right,
/// instead *flags* the node, and moves up:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . .[.]. # . . .
/// . . . F W . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// This process repeats three more times, ending on the previously-flagged
/// node and facing right:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . W W . # . . .
/// . . W[F]W . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// Finding a flagged node, it reverses direction and *cleans* the node:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . W W . # . . .
/// . .[W]. W . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// The *weakened* node becomes infected, and it continues in the same
/// direction:
///
/// ```text
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . W W . # . . .
/// .[.]# . W . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// . . . . . . . . .
/// ```
///
/// Of the first `100` bursts, `26` will result in *infection*.
/// Unfortunately, another feature of this evolved virus is *speed*; of the
/// first `10000000` bursts, `2511944` will result in *infection*.
///
/// ```
/// # use advent_solutions::advent2017::day22::part2_with_bursts;
/// let input = "..#
/// #..
/// ...
/// ";
///
/// assert_eq!(part2_with_bursts(input, 100), 26);
/// assert_eq!(part2_with_bursts(input, 10000000), 2511944);
/// ```
///
/// Given your actual map, after `10000000` bursts of activity, *how many
/// bursts cause a node to become infected*? (Do not count nodes that begin
/// infected.)
pub fn part2_with_bursts(input: &str, bursts: usize) -> usize {
    let mut memory = Memory::parse(&input);
    let mut carrier = Carrier::new();

    for _ in 0..bursts {
        carrier.part2_with_bursts(&mut memory);
    }

    carrier.infected
}

pub fn part2(input: &str) -> usize {
    part2_with_bursts(input, 10000000)
}

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

test_day!("22", 5259, 2511722);