summaryrefslogtreecommitdiff
path: root/src/bin/day13.rs
blob: 02045eacdda3611bb8dc3af1d3f30a02f2c15484 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::collections::HashSet;

static DAY: u8 = 13;

fn main() {
    let input = advent::read_lines(DAY);
    println!("{DAY}a: {}", summarize_patterns(&input));
    println!("{DAY}b: {}", summarize_patterns_with_smudge(&input));
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
enum Reflection {
    Vertical(usize),
    Horizontal(usize),
}

impl Reflection {
    fn summary(&self) -> usize {
        match *self {
            Reflection::Vertical(column) => column + 1,
            Reflection::Horizontal(row) => (row + 1) * 100,
        }
    }
}

#[derive(Clone)]
struct Map {
    pattern: Vec<Vec<bool>>,
}

impl Map {
    fn new(input: &[String]) -> Map {
        let mut pattern = Vec::new();
        for line in input {
            let row = line.chars()
                          .map(|c| c == '#')
                          .collect();
            pattern.push(row);
        }

        Map { pattern }
    }

    fn _print_map(&self) {
        for y in 0 .. self.pattern.len() {
            for x in 0 .. self.pattern[0].len() {
                if self.pattern[y][x] {
                    print!("#");
                } else {
                    print!(".");
                }
            }
            println!();
        }
        println!();
    }

    fn is_horizontal_reflection(&self, y: usize) -> bool {
        for pair in (y+1 .. self.pattern.len()).zip((0 ..= y).rev()) {
            if self.pattern[pair.0] != self.pattern[pair.1] {
                return false;
            }
        }
        true
    }

    fn is_vertical_reflection(&self, x: usize) -> bool {
        for pair in (x+1 .. self.pattern[0].len()).zip((0 ..= x).rev()) {
            for y in 0 .. self.pattern.len() {
                if self.pattern[y][pair.0] != self.pattern[y][pair.1] {
                    return false;
                }
            }
        }
        true
    }

    fn find_reflections(&self) -> HashSet<Reflection> {
        let mut reflections = HashSet::new();
        for y in 0 .. self.pattern.len() - 1 {
            if self.is_horizontal_reflection(y) {
                reflections.insert(Reflection::Horizontal(y));
            }
        }
        for x in 0 .. self.pattern[0].len() - 1 {
            if self.is_vertical_reflection(x) {
                reflections.insert(Reflection::Vertical(x));
            }
        }
        reflections
    }

    fn find_reflection_with_smudge(&self) -> Reflection {
        let orig_reflection = self.find_reflections();
        for y in 0 .. self.pattern.len() {
            for x in 0 .. self.pattern[0].len() {
                let mut map = self.clone();
                map.pattern[y][x] = !map.pattern[y][x];
                let reflections = map.find_reflections();
                let new_reflections = reflections.difference(&orig_reflection).collect::<Vec<_>>();
                if !new_reflections.is_empty() {
                    return *new_reflections[0];
                }
           }
        }
        panic!("no reflection found");
    }
}

fn summarize_patterns(input: &[String]) -> usize {
    input.split(|line| line.is_empty())
         .map(Map::new)
         .map(|m| m.find_reflections().iter().next().unwrap().summary())
         .sum()
}

fn summarize_patterns_with_smudge(input: &[String]) -> usize {
    input.split(|line| line.is_empty())
         .map(Map::new)
         .map(|m| m.find_reflection_with_smudge().summary())
         .sum()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let input = [
            "#.##..##.",
            "..#.##.#.",
            "##......#",
            "##......#",
            "..#.##.#.",
            "..##..##.",
            "#.#.##.#.",
            "",
            "#...##..#",
            "#....#..#",
            "..##..###",
            "#####.##.",
            "#####.##.",
            "..##..###",
            "#....#..#",
        ].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
        assert_eq!(summarize_patterns(&input), 405);
        assert_eq!(summarize_patterns_with_smudge(&input), 400);
    }
}