-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20-rust.rs
189 lines (156 loc) · 4.7 KB
/
day20-rust.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
178
179
180
181
182
183
184
185
186
187
188
189
use std::ops::{Index, IndexMut};
use std::str::FromStr;
use std::{fmt, fs};
#[derive(Clone)]
struct Image {
rows: usize,
cols: usize,
data: Vec<bool>,
default: bool,
}
impl Image {
fn new(rows: usize, cols: usize, default: bool) -> Image {
let data = vec![false; rows * cols];
Image {
rows,
cols,
data,
default,
}
}
fn lit_count(&self) -> usize {
self.data.iter().filter(|a| **a == true).count()
}
}
// 2-d index for Image
impl Index<(i32, i32)> for Image {
type Output = bool;
fn index(&self, index: (i32, i32)) -> &Self::Output {
if index.0 < 0 || index.1 < 0 {
return &self.default;
}
if (index.0 as usize >= self.rows || index.1 as usize >= self.cols) {
return &self.default;
}
&self.data[index.0 as usize * self.cols + index.1 as usize]
}
}
// 2-d index for Image (mutable)
impl IndexMut<(i32, i32)> for Image {
fn index_mut(&mut self, index: (i32, i32)) -> &mut Self::Output {
if index.0 < 0 || index.1 < 0 {
panic!()
}
if (index.0 as usize >= self.rows || index.1 as usize >= self.cols) {
panic!()
}
&mut self.data[index.0 as usize * self.cols + index.1 as usize]
}
}
// "Parse" functionality for Image
impl FromStr for Image {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let lines: Vec<&str> = s.split("\n").filter(|row| !row.trim().is_empty()).collect();
let result = Image {
cols: lines[0].len(),
rows: lines.len(),
data: lines
.iter()
.flat_map(|line| line.chars().map(|c| c == '#'))
.collect(),
default: false,
};
if result.data.len() == result.rows * result.cols {
Ok(result)
} else {
Err(())
}
}
}
// "ToString" functionality for Image
impl fmt::Display for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for r in 0..self.rows {
for c in 0..self.cols {
if self[(r as i32, c as i32)] {
write!(f, "#")?
} else {
write!(f, ".")?
}
}
writeln!(f, "")?
}
Ok(())
}
}
struct ImageEnhancer {
algorithm: Vec<bool>,
}
impl ImageEnhancer {
fn enhance(&self, image: &Image, times: usize) -> Image {
let mut image = image.clone();
for _ in 0..times {
image = self.enhance_iteration(image);
}
image
}
fn enhance_iteration(&self, image: Image) -> Image {
let s = 1 as i32;
let default = if image.default {
*self.algorithm.last().unwrap()
} else {
*self.algorithm.first().unwrap()
};
let mut result = Image::new(
image.rows + s as usize * 2,
image.cols + s as usize * 2,
default,
);
for r in -s..(image.rows as i32 + s) {
for c in -s..(image.rows as i32 + s) {
let mut index = 0;
for r1 in -1..=1 {
for c1 in -1..=1 {
index <<= 1;
if image[(r + r1, c + c1)] {
index += 1;
}
}
}
assert!(index < 512);
result[(r + s, c + s)] = self.algorithm[index];
}
}
result
}
}
// "Parse" functionality for Image
impl FromStr for ImageEnhancer {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = ImageEnhancer {
algorithm: s.trim().chars().map(|c| c == '#').collect(),
};
Ok(result)
}
}
fn main() {
let input = fs::read_to_string("input-day-20.txt").unwrap();
let mut iter = input.split("\n");
let a: Vec<&str> = iter.by_ref().take_while(|a| !a.is_empty()).collect();
let b: Vec<&str> = iter.collect();
let algorithm = a.join("");
let image_data = b.join("\n");
let image: Image = image_data.parse().unwrap();
let enhancer: ImageEnhancer = algorithm.parse().unwrap();
println!("{:?}", algorithm);
println!("{}", image);
println!("{}", enhancer.enhance(&image, 2).lit_count());
println!("{}", enhancer.enhance(&image, 50).lit_count());
// println!("{}", enhancer.enhance(&image, 0));
// println!("{}", enhancer.enhance(&image, 1));
// println!("{}", enhancer.enhance(&image, 2));
// println!("{}", enhancer.enhance(&image, 3));
// println!("{}", enhancer.enhance(&image, 4));
}