-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rs
110 lines (97 loc) · 2.87 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
use itertools::Itertools;
use aoc_lib::{answer::Answer, solution::Solution};
pub struct Day13;
impl Solution for Day13 {
fn part_a(&self, input: &[String]) -> Answer {
let parsed = parse(input);
parsed
.patterns
.iter()
.map(|p| {
Pattern::count_before_reflection(&p.transpose(), 0)
+ 100 * Pattern::count_before_reflection(&p.rows, 0)
})
.sum::<usize>()
.into()
}
fn part_b(&self, input: &[String]) -> Answer {
let parsed = parse(input);
parsed
.patterns
.iter()
.map(|p| {
Pattern::count_before_reflection(&p.transpose(), 1)
+ 100 * Pattern::count_before_reflection(&p.rows, 1)
})
.sum::<usize>()
.into()
}
}
struct Parsed {
patterns: Vec<Pattern>,
}
fn parse(input: &[String]) -> Parsed {
let mut patterns = vec![];
let degrouped = input
.iter()
.chunk_by(|line| !line.is_empty())
.into_iter()
.filter(|(key, _)| *key)
.map(|(_, group)| group.collect::<Vec<_>>())
.collect::<Vec<_>>();
for group in degrouped {
let rows = group
.iter()
.map(|l| l.chars().collect::<Vec<char>>())
.collect::<Vec<Vec<_>>>();
patterns.push(Pattern { rows });
}
Parsed { patterns }
}
struct Pattern {
rows: Vec<Vec<char>>,
}
impl Pattern {
fn transpose(&self) -> Vec<Vec<char>> {
let rows = self.rows.clone();
(0..rows[0].len())
.map(|col| (0..rows.len()).map(|row| rows[row][col]).collect())
.collect()
}
fn count_before_reflection(buffer: &[Vec<char>], limit: usize) -> usize {
for mid in 1..=buffer.len() - 1 {
let side = mid.min(buffer.len() - mid);
let start = mid - side;
let mut diff = 0;
for l in start..mid {
let r = mid * 2 - l - 1;
diff += (0..buffer[l].len())
.filter(|&i| buffer[l][i] != buffer[r][i])
.count();
}
if diff == limit {
return mid;
}
}
0
}
}
#[cfg(test)]
mod test {
use aoc_lib::{self, answer::Answer, input, solution::Solution};
use super::Day13;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_13_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day13.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(405), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_13_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day13.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(400), answer);
}
}