summaryrefslogtreecommitdiffstats
path: root/src/2021/13/mod.rs
blob: 0be74d1ad5bb3aa89585c8eb66081c63173de71f (plain) (blame)
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
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::comparison_chain)]

use crate::util::grid::*;

#[derive(Default)]
struct Paper {
    grid: Grid<bool>,
}

impl Paper {
    fn set(&mut self, row: Row, col: Col) {
        self.grid.grow(Row(row.0 + 1), Col(col.0 + 1));
        self.grid[row][col] = true;
    }

    fn fold(&mut self, horizontal: bool, coord: usize) {
        let mut clone = Self::default();
        if horizontal {
            clone.grid.grow(
                self.grid.rows(),
                Col(coord.max(self.grid.cols().0 - coord - 1)),
            );
            for ((Row(row), Col(col)), cell) in self.grid.indexed_cells() {
                if *cell {
                    if coord > self.grid.cols().0 - coord - 1 {
                        if col < coord {
                            clone.set(Row(row), Col(col));
                        } else if col > coord {
                            clone.set(Row(row), Col(coord * 2 - col));
                        }
                    } else {
                        if col < coord {
                            clone.set(
                                Row(row),
                                Col(self.grid.cols().0 - coord * 2 - 1 + col),
                            );
                        } else if col > coord {
                            clone.set(
                                Row(row),
                                Col(self.grid.cols().0 - col - 1),
                            );
                        }
                    }
                }
            }
        } else {
            clone.grid.grow(
                Row(coord.max(self.grid.rows().0 - coord - 1)),
                self.grid.cols(),
            );
            for ((Row(row), Col(col)), cell) in self.grid.indexed_cells() {
                if *cell {
                    if coord > self.grid.rows().0 - coord - 1 {
                        if row < coord {
                            clone.set(Row(row), Col(col));
                        } else if row > coord {
                            clone.set(Row(coord * 2 - row), Col(col));
                        }
                    } else {
                        if row < coord {
                            clone.set(
                                Row(self.grid.rows().0 - coord * 2 - 1 + row),
                                Col(col),
                            );
                        } else if row > coord {
                            clone.set(
                                Row(self.grid.rows().0 - row - 1),
                                Col(col),
                            );
                        }
                    }
                }
            }
        }
        *self = clone;
    }

    fn total(&self) -> i64 {
        self.grid.cells().map(|b| if *b { 1 } else { 0 }).sum()
    }
}

impl std::fmt::Display for Paper {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "{}",
            self.grid.display_packed(|b| if *b { '#' } else { '.' })
        )
    }
}

pub fn part1() -> anyhow::Result<i64> {
    let mut paper = Paper::default();
    let mut folds = vec![];
    for line in data_lines!()? {
        if line.is_empty() {
            continue;
        }
        if let Some(fold) = line.strip_prefix("fold along ") {
            let mut fold = fold.split('=');
            let horizontal = fold.next().unwrap() == "x";
            let coord: usize = fold.next().unwrap().parse()?;
            folds.push((horizontal, coord));
        } else {
            let mut coords = line.split(',');
            let x: usize = coords.next().unwrap().parse()?;
            let y: usize = coords.next().unwrap().parse()?;
            paper.set(Row(y), Col(x));
        }
    }

    paper.fold(folds[0].0, folds[0].1);
    Ok(paper.total())
}

pub fn part2() -> anyhow::Result<i64> {
    let mut paper = Paper::default();
    let mut folds = vec![];
    for line in data_lines!()? {
        if line.is_empty() {
            continue;
        }
        if let Some(fold) = line.strip_prefix("fold along ") {
            let mut fold = fold.split('=');
            let horizontal = fold.next().unwrap() == "x";
            let coord: usize = fold.next().unwrap().parse()?;
            folds.push((horizontal, coord));
        } else {
            let mut coords = line.split(',');
            let x: usize = coords.next().unwrap().parse()?;
            let y: usize = coords.next().unwrap().parse()?;
            paper.set(Row(y), Col(x));
        }
    }

    for fold in folds {
        paper.fold(fold.0, fold.1);
    }

    println!("{}", paper);
    Ok(paper.total())
}

#[test]
fn test() {
    assert_eq!(part1().unwrap(), 678);
    assert_eq!(part2().unwrap(), 95);
}