summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_22.rs
blob: 917ed63d5758b4275f1ae665f4aa5647044013aa (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
extern crate advent_of_code_2017;
use advent_of_code_2017::*;

use std::collections::HashSet;

fn main() {
    let args = AdventArgs::init();
    let input_width = args.input[0].len();
    let input_height = args.input.len();

    let mut position = Point {
        x: (input_width / 2) as i32,
        y: (input_height / 2) as i32,
    };
    let mut direction = Direction::Up;

    let mut weakened: HashSet<Point> = HashSet::new();
    let mut infected: HashSet<Point> = HashSet::new();
    let mut flagged: HashSet<Point> = HashSet::new();
    
    for (y, line) in args.input.iter().enumerate() {
        for (x, c) in line.chars().enumerate() {
            if c == '#' {
                infected.insert(Point {
                    x: x as i32,
                    y: y as i32
                });
            }
        }
    }

    let mut infections_caused = 0;

    let bursts = if args.part == 1 {
        10_000
    } else {
        10_000_000
    };
    
    for _ in 0..bursts {
        if args.part == 1 {
            if infected.contains(&position) {
                direction = direction.rotate_right();
                infected.remove(&position);
            } else {
                direction = direction.rotate_left();
                infected.insert(position);
                infections_caused += 1;
            }
        }
        else {
            if weakened.contains(&position) {
                infected.insert(position);
                weakened.remove(&position);
                infections_caused += 1;
            } else if infected.contains(&position) {
                direction = direction.rotate_right();
                flagged.insert(position);
                infected.remove(&position);
            } else if flagged.contains(&position) {
                direction = direction.rotate_right().rotate_right();
                flagged.remove(&position);
            } else {
                direction = direction.rotate_left();
                weakened.insert(position);
            }
        }
        position = position.shift(&direction);
    }

    println!("Infections caused {}", infections_caused);
    
}