-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.rs
177 lines (155 loc) · 5.76 KB
/
day21.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::fs;
pub(crate) fn day21() {
let input = fs::read_to_string("input/2017/day21/input.txt").unwrap();
let rules = parse(input.as_str());
println!("{:?}", enhance(5, &rules));
println!("{:?}", enhance(18, &rules));
}
fn parse(input: &str) -> Vec<(Vec<Vec<char>>, Vec<Vec<char>>)> {
input.lines().map(|line| {
let (pattern_str, result_str) = line.split_once(" => ").unwrap();
let pattern = pattern_str.split("/").map(|row| row.chars().collect()).collect::<Vec<Vec<char>>>();
let result = result_str.split("/").map(|row| row.chars().collect()).collect::<Vec<Vec<char>>>();
(pattern, result)
}).flat_map(|(pattern, result)| {
let mut res = vec!();
let mut p = pattern;
res.push((p.clone(), result.clone()));
res.push((flip(&p), result.clone()));
// Rotate to 90, 180 and 270
for _ in 0..3 {
p = rotate(&p);
res.push((p.clone(), result.clone()));
res.push((flip(&p), result.clone()));
}
res
}).collect()
}
fn rotate(image: &Vec<Vec<char>>) -> Vec<Vec<char>> {
if image.len() == 2 {
let mut result = vec![vec![' '; 2]; 2];
result[0][0] = image[1][0];
result[0][1] = image[0][0];
result[1][0] = image[1][1];
result[1][1] = image[0][1];
result
} else if image.len() == 3 {
let mut result = vec![vec![' '; 3]; 3];
result[0][0] = image[2][0];
result[0][1] = image[1][0];
result[0][2] = image[0][0];
result[1][0] = image[2][1];
result[1][1] = image[1][1];
result[1][2] = image[0][1];
result[2][0] = image[2][2];
result[2][1] = image[1][2];
result[2][2] = image[0][2];
result
} else {
panic!("Unsupported image size {}", image.len())
}
}
fn flip(image: &Vec<Vec<char>>) -> Vec<Vec<char>> {
image.iter().map(|row| {
let mut res = row.clone();
res.reverse();
res
}).collect()
}
fn enhance(iterations: i32, rules: &Vec<(Vec<Vec<char>>, Vec<Vec<char>>)>) -> usize {
let mut image = vec![vec!['.', '#', '.'], vec!['.', '.', '#'], vec!['#', '#', '#']];
for _ in 0..iterations {
let inc: usize = if image.len() % 2 == 0 { 2 } else { 3 };
let mut squares: Vec<Vec<Vec<char>>> = vec!();
for dx in 0..(image.len() / inc) {
for dy in 0..(image.len() / inc) {
let mut square = vec!();
for x in 0..inc {
let mut row = vec!();
for y in 0..inc {
row.push(image[x + inc * dx][y + inc * dy]);
}
square.push(row);
}
squares.push(square);
}
}
let mut new_squares = vec!();
'outer: for square in squares {
for (pattern, result) in rules {
if square == *pattern {
new_squares.push(result.clone());
continue 'outer;
}
}
panic!("No matching pattern");
}
let mut new_image = vec!();
for dx in 0..(image.len() / inc) {
let mut new_rows = vec![vec!(); inc + 1];
for dy in 0..(image.len() / inc) {
let square = &new_squares[(image.len() / inc) * dx + dy];
for x in 0..=inc {
for y in 0..=inc {
new_rows[x].push(square[x][y]);
}
}
}
new_image.append(&mut new_rows);
}
image = new_image;
}
image.iter().map(|row| row.iter().filter(|c| **c == '#').count()).sum()
}
#[cfg(test)]
mod day21_tests {
use std::fs;
use crate::y2017::day21::{enhance, flip, parse, rotate};
#[test]
fn rotate_len2_works() {
let original = vec![vec!['.', '.'], vec!['.', '#']];
let rot_90 = vec![vec!['.', '.'], vec!['#', '.']];
let rot_180 = vec![vec!['#', '.'], vec!['.', '.']];
let rot_270 = vec![vec!['.', '#'], vec!['.', '.']];
assert_eq!(rot_90, rotate(&original));
assert_eq!(rot_180, rotate(&rot_90));
assert_eq!(rot_270, rotate(&rot_180));
assert_eq!(original, rotate(&rot_270));
}
#[test]
fn rotate_len3_works() {
let original = vec![vec!['.', '#', '.'], vec!['.', '.', '#'], vec!['#', '#', '#']];
let rot_90 = vec![vec!['#', '.', '.'], vec!['#', '.', '#'], vec!['#', '#', '.']];
let rot_180 = vec![vec!['#', '#', '#'], vec!['#', '.', '.'], vec!['.', '#', '.']];
let rot_270 = vec![vec!['.', '#', '#'], vec!['#', '.', '#'], vec!['.', '.', '#']];
assert_eq!(rot_90, rotate(&original));
assert_eq!(rot_180, rotate(&rot_90));
assert_eq!(rot_270, rotate(&rot_180));
assert_eq!(original, rotate(&rot_270));
}
#[test]
fn flip_len2_works() {
let original = vec![vec!['.', '.'], vec!['.', '#']];
let flipped = vec![vec!['.', '.'], vec!['#', '.']];
assert_eq!(flipped, flip(&original));
}
#[test]
fn flip_len3_works() {
let original = vec![vec!['.', '.', '#'], vec!['.', '#', '.'], vec!['#', '.', '.']];
let flipped = vec![vec!['#', '.', '.'], vec!['.', '#', '.'], vec!['.', '.', '#']];
assert_eq!(flipped, flip(&original));
}
#[test]
fn test_works() {
let input = fs::read_to_string("input/2017/day21/test.txt").unwrap();
let rules = parse(input.as_str());
assert_eq!(12, enhance(2, &rules));
}
#[test]
fn input_works() {
let input = fs::read_to_string("input/2017/day21/input.txt").unwrap();
let rules = parse(input.as_str());
assert_eq!(167, enhance(5, &rules));
assert_eq!(2425195, enhance(18, &rules));
}
}