generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.rs
140 lines (125 loc) · 3.54 KB
/
13.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
133
134
135
136
137
138
139
140
advent_of_code::solution!(13);
pub fn part_one(input: &str) -> Option<usize> {
let mut pattern: Vec<Vec<char>> = vec![];
let mut tot = 0;
for line in input.lines() {
if line.is_empty() {
// process pattern
tot += process_pattern(&pattern);
pattern.clear();
} else {
pattern.push(line.chars().collect());
}
}
// process last pattern
tot += process_pattern(&pattern);
Some(tot)
}
fn process_pattern(pattern: &Vec<Vec<char>>) -> usize {
let mut ans: usize = 0;
for i in 1..pattern.len() {
// expand
let mut top = (i - 1) as isize;
let mut bottom = i;
while top >= 0 && bottom < pattern.len() && pattern[top as usize] == pattern[bottom] {
top -= 1;
bottom += 1;
}
if top == -1 || bottom == pattern.len() {
ans += 100* i;
}
}
for i in 1..pattern[0].len() {
// expand
let mut left = (i - 1) as isize;
let mut right = i;
while left >= 0
&& right < pattern[0].len()
&& pattern.iter().all(|row| row[left as usize] == row[right])
{
left -= 1;
right += 1;
}
if left == -1 || right == pattern[0].len() {
ans += i;
}
}
ans
}
fn process_pattern2(pattern: &Vec<Vec<char>>) -> usize {
let mut ans: usize = 0;
for i in 1..pattern.len() {
// expand
let mut top = (i - 1) as isize;
let mut bottom = i;
let mut diff_count = 0;
while top >= 0 && bottom < pattern.len() {
if pattern[top as usize] != pattern[bottom] {
diff_count += pattern[top as usize]
.iter()
.zip(pattern[bottom].iter())
.filter(|(&c1, &c2)| c1 != c2)
.count();
}
if diff_count > 1 {
break;
}
top -= 1;
bottom += 1;
}
if (top == -1 || bottom == pattern.len()) && diff_count == 1 {
ans += 100 * i;
}
}
for i in 1..pattern[0].len() {
// expand
let mut left = (i - 1) as isize;
let mut right = i;
let mut diff_count = 0;
while left >= 0 && right < pattern[0].len() {
diff_count += pattern
.iter()
.map(|row| (row[left as usize] != row[right]) as usize)
.sum::<usize>();
if diff_count > 1 {
break;
}
left -= 1;
right += 1;
}
if (left == -1 || right == pattern[0].len()) && diff_count == 1 {
ans += i;
}
}
ans
}
pub fn part_two(input: &str) -> Option<usize> {
let mut pattern: Vec<Vec<char>> = vec![];
let mut tot = 0;
for line in input.lines() {
if line.is_empty() {
// process pattern
tot += process_pattern2(&pattern);
pattern.clear();
} else {
pattern.push(line.chars().collect());
}
}
// process last pattern
tot += process_pattern2(&pattern);
Some(tot)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(405));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(400));
}
}