summaryrefslogtreecommitdiff
path: root/2021/src/bin/day_11.rs
blob: 2a4cd89b258eacfc9a82afabc47a7ae04ba6b9f1 (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
use nom::{
    character::complete::{line_ending, one_of},
    combinator::map_res,
    multi::{many1, separated_list1},
    IResult,
};
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = fs::read_to_string("inputs/day_11.txt")?;
    let squid_grid = parse_squid_grid(&input).unwrap().1;

    {
        let mut squid_grid = squid_grid.clone();
        let mut flashes_on_100 = 0;
        for _ in 0..100 {
            flashes_on_100 += squid_grid.flash();
        }
        dbg!(flashes_on_100);
    }

    {
        let mut squid_grid = squid_grid.clone();
        let sync_time = std::iter::repeat_with(|| squid_grid.flash())
            .position(|flashes| flashes == 100)
            .map(|x| x + 1);
        dbg!(sync_time);
    }

    Ok(())
}

#[derive(Debug, Clone)]
struct Squid {
    energy: u8,
    has_flashed: bool,
}

impl Squid {
    fn should_flash(&self) -> bool {
        self.energy > 9 && !self.has_flashed
    }

    fn reset(&mut self) {
        self.energy = 0;
        self.has_flashed = false;
    }
}

#[derive(Debug, Clone)]
struct SquidGrid {
    squids: [[Squid; 10]; 10],
}

impl SquidGrid {
    fn flash(&mut self) -> usize {
        for y in 0..10 {
            for x in 0..10 {
                self.squids[y][x].energy += 1;
            }
        }

        let mut any_new_flashes = true;
        while any_new_flashes {
            any_new_flashes = false;
            for y in 0..10 {
                for x in 0..10 {
                    if self.squids[y][x].should_flash() {
                        any_new_flashes = true;
                        self.squids[y][x].has_flashed = true;
                        if y > 0 && x > 0 {
                            self.squids[y - 1][x - 1].energy += 1;
                        }
                        if y > 0 {
                            self.squids[y - 1][x].energy += 1;
                        }
                        if y > 0 && x < 9 {
                            self.squids[y - 1][x + 1].energy += 1;
                        }
                        if x > 0 {
                            self.squids[y][x - 1].energy += 1;
                        }
                        if x < 9 {
                            self.squids[y][x + 1].energy += 1;
                        }
                        if y < 9 && x > 0 {
                            self.squids[y + 1][x - 1].energy += 1;
                        }
                        if y < 9 {
                            self.squids[y + 1][x].energy += 1;
                        }
                        if y < 9 && x < 9 {
                            self.squids[y + 1][x + 1].energy += 1;
                        }
                    }
                }
            }
        }

        let mut flashes = 0;
        for y in 0..10 {
            for x in 0..10 {
                if self.squids[y][x].has_flashed {
                    self.squids[y][x].reset();
                    flashes += 1;
                }
            }
        }
        flashes
    }
}

fn parse_squid_grid(input: &str) -> IResult<&str, SquidGrid> {
    map_res(separated_list1(line_ending, parse_squid_row), |squids| {
        squids.try_into().map(|squids| SquidGrid { squids })
    })(input)
}

fn parse_squid_row(input: &str) -> IResult<&str, [Squid; 10]> {
    map_res(many1(parse_squid), |squids| squids.try_into())(input)
}

fn parse_squid(input: &str) -> IResult<&str, Squid> {
    map_res(one_of("0123456789"), |digit| {
        digit.to_string().parse().map(|energy| Squid {
            energy,
            has_flashed: false,
        })
    })(input)
}