-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rs
128 lines (111 loc) Β· 3.58 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
use ndarray::{s, Array, Array2};
use super::Solution;
pub struct Day13 {
rock_groups: Vec<Rocks>,
}
impl Solution for Day13 {
fn part1(&mut self) -> String {
self.rock_groups
.iter()
.map(|rocks| rocks.reflection_point())
.sum::<usize>()
.to_string()
}
fn part2(&mut self) -> String {
self.rock_groups
.iter()
.map(|rocks| rocks.smudge())
.sum::<usize>()
.to_string()
}
fn parse(input: String) -> Box<dyn Solution> {
Box::new(Self {
rock_groups: input.split("\n\n").map(Rocks::from_str).collect(),
})
}
}
#[derive(Debug)]
struct Rocks {
map: Array2<bool>,
}
impl Rocks {
fn from_str(str: &str) -> Self {
let cols = str.find('\n').unwrap();
let rows = str.lines().count();
let map = Array::from_iter(
str.lines()
.flat_map(|line| line.bytes().map(|byte| byte == b'#')),
)
.into_shape((rows, cols))
.unwrap();
Self { map }
}
fn reflection_point(&self) -> usize {
let shape = self.map.shape();
// horizontal reflection
for split_idx in 1..shape[0] {
let diff = std::cmp::min(split_idx, shape[0] - split_idx);
let left = self.map.slice(s![split_idx - diff..split_idx;-1, ..]);
let right = self.map.slice(s![split_idx..split_idx + diff, ..]);
if left == right {
return 100 * split_idx;
}
}
// vertical reflection
for split_idx in 1..shape[1] {
let diff = std::cmp::min(split_idx, shape[1] - split_idx);
let left = self.map.slice(s![.., split_idx - diff..split_idx;-1]);
let right = self.map.slice(s![.., split_idx..split_idx + diff]);
if left == right {
return split_idx;
}
}
0
}
fn smudge(&self) -> usize {
let shape = self.map.shape();
// horizontal reflection
for split_idx in 1..shape[0] {
let diff = std::cmp::min(split_idx, shape[0] - split_idx);
let left = self.map.slice(s![split_idx - diff..split_idx;-1, ..]);
let right = self.map.slice(s![split_idx..split_idx + diff, ..]);
let shape = left.shape();
let mut differences = 0;
for row in 0..shape[0] {
for col in 0..shape[1] {
if left[[row, col]] != right[[row, col]] {
differences += 1;
}
if differences > 1 {
break;
}
}
}
if differences == 1 {
return 100 * split_idx;
}
}
// vertical reflection
for split_idx in 1..shape[1] {
let diff = std::cmp::min(split_idx, shape[1] - split_idx);
let left = self.map.slice(s![.., split_idx - diff..split_idx;-1]);
let right = self.map.slice(s![.., split_idx..split_idx + diff]);
let shape = left.shape();
let mut differences = 0;
for row in 0..shape[0] {
for col in 0..shape[1] {
if left[[row, col]] != right[[row, col]] {
differences += 1;
}
if differences > 1 {
break;
}
}
}
if differences == 1 {
return split_idx;
}
}
0
}
}