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
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::str::FromStr;

use regex::Regex;

use advent_of_code::main;

main!();

fn solve(input: &str) -> Result<String, Box<Error>> {
    let mut generation: Generation = input.parse()?;
    for _ in 0..19 {
        generation.next();
    }

    let pots = generation.next().unwrap();
    let indices: Vec<_> = pots.iter().filter(|(_, &v)| v).map(|(&k, _)| k).collect();
    let output: isize = indices.iter().sum();

    Ok(output.to_string())
}

#[test]
fn test_solve() {
    let input = r"
initial state: #..#.#..##......###...###

...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
    ";

    assert_eq!(solve(&input).unwrap(), 325.to_string());
}

struct Generation {
    pots: HashMap<isize, bool>,
    notes: HashMap<String, bool>,
}

impl fmt::Display for Generation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let plants: Vec<_> = self
            .pots
            .iter()
            .filter(|(_, &v)| v)
            .map(|(&k, _)| k)
            .collect();
        let min = *plants.iter().min().unwrap();
        let max = *plants.iter().max().unwrap();
        let s: String = (min..=max)
            .map(|x| self.pots.get(&x).unwrap_or_else(|| &false))
            .map(|&x| if x { '#' } else { '.' })
            .collect();
        write!(f, "{}", s)
    }
}

impl Iterator for Generation {
    type Item = HashMap<isize, bool>;

    fn next(&mut self) -> Option<Self::Item> {
        let plants: Vec<_> = self
            .pots
            .iter()
            .filter(|(_, &v)| v)
            .map(|(&k, _)| k)
            .collect();
        let min = *plants.iter().min().unwrap() - 2;
        let max = *plants.iter().max().unwrap() + 2;
        self.pots = (min..=max)
            .map(|i| {
                let key: String = ((i - 2)..=(i + 2))
                    .map(|x| self.pots.get(&x).unwrap_or_else(|| &false))
                    .map(|&x| if x { '#' } else { '.' })
                    .collect();
                let value = *self.notes.get(&key).unwrap_or_else(|| &false);
                (i, value)
            })
            .collect();

        Some(self.pots.clone())
    }
}

#[test]
fn test_generation() {
    let input = r"
initial state: #..#.#..##......###...###

...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
    ";
    let mut generation: Generation = input.parse().unwrap();
    let pots = generation.pots.clone();
    assert!(pots.get(&0).unwrap());
    assert!(pots.get(&3).unwrap());
    assert!(!pots.get(&4).unwrap());

    let pots = generation.next().unwrap();
    assert!(pots.get(&0).unwrap());
    assert!(!pots.get(&3).unwrap());
    assert!(pots.get(&4).unwrap());
}

impl FromStr for Generation {
    type Err = Box<Error>;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut lines = s.trim().lines();

        let initial_state = lines.next().unwrap();
        let pots = Regex::new(r"[.#]+")
            .ok()
            .and_then(|re| re.find(initial_state))
            .unwrap()
            .as_str()
            .chars()
            .enumerate()
            .map(|(i, c)| (i as isize, c == '#'))
            .collect();

        lines.next(); // skip blank line

        let notes = lines
            .map(|line| {
                let mut note = line.split(" => ");
                let key = note.next().unwrap();
                let value = note.next().unwrap() == "#";
                (key.into(), value)
            })
            .collect();

        Ok(Generation { pots, notes })
    }
}