-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.rs
132 lines (107 loc) · 3.2 KB
/
day13.rs
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
use std::convert::Infallible;
use aoc_runner_derive::{aoc, aoc_generator};
use crate::common::{matrix::rotate_right_m_n, utils::parse_split};
#[derive(Debug)]
pub struct Pattern {
pattern: Vec<Vec<u8>>,
}
impl std::str::FromStr for Pattern {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let pattern: Vec<_> = s.lines().map(|line| line.as_bytes().to_vec()).collect();
Ok(Pattern { pattern })
}
}
impl Pattern {
fn find_mirror<const TARGET: usize>(&self) -> Option<usize> {
let a = Self::find_mirror_horz::<TARGET>(&self.pattern);
if let Some(a) = a {
Some(a * 100)
} else {
Self::find_mirror_vert::<TARGET>(&self.pattern)
}
}
fn count_badness<const TARGET: usize>(left: &[u8], right: &[u8]) -> usize {
left.iter()
.zip(right.iter())
.filter(|(l, r)| l != r)
.try_fold(0, |sum, _| (sum < TARGET).then_some(sum + 1))
.unwrap_or(TARGET + 1)
}
fn check_mirror<const TARGET: usize>(left: &[Vec<u8>], right: &[Vec<u8>]) -> bool {
left.iter()
.rev()
.zip(right.iter())
.map(|(l, r)| Self::count_badness::<TARGET>(l, r))
.try_fold(0, |sum, n| (sum + n <= TARGET).then_some(sum + n))
.unwrap_or(TARGET + 1)
== TARGET
}
fn find_mirror_horz<const TARGET: usize>(p: &[Vec<u8>]) -> Option<usize> {
(1..p.len()).find(|&split| {
let (left, right) = p.split_at(split);
Self::check_mirror::<TARGET>(left, right)
})
}
fn find_mirror_vert<const TARGET: usize>(p: &[Vec<u8>]) -> Option<usize> {
let p = rotate_right_m_n(p);
Self::find_mirror_horz::<TARGET>(&p)
}
}
#[aoc_generator(day13)]
pub fn generator(input: &str) -> Vec<Pattern> {
parse_split(input, "\n\n")
}
#[aoc(day13, part1)]
pub fn part1(inputs: &[Pattern]) -> usize {
inputs.iter().filter_map(Pattern::find_mirror::<0>).sum()
}
#[aoc(day13, part2)]
pub fn part2(inputs: &[Pattern]) -> usize {
inputs.iter().filter_map(Pattern::find_mirror::<1>).sum()
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = r"#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#";
#[test]
pub fn input_test() {
let p = generator(SAMPLE);
// println!("{:?}", p);
assert_eq!(Pattern::find_mirror_vert::<0>(&p[0].pattern), Some(5));
assert_eq!(Pattern::find_mirror_horz::<0>(&p[1].pattern), Some(4));
}
#[test]
pub fn part1_test() {
assert_eq!(part1(&generator(SAMPLE)), 405);
}
#[test]
pub fn part2_test() {
assert_eq!(part2(&generator(SAMPLE)), 400);
}
mod regression {
use super::*;
const INPUT: &str = include_str!("../input/2023/day13.txt");
const ANSWERS: (usize, usize) = (33047, 28806);
#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);
assert_eq!(part1(&output), ANSWERS.0);
assert_eq!(part2(&output), ANSWERS.1);
}
}
}