-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
231 lines (207 loc) Β· 6.21 KB
/
mod.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::fmt::Display;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DiskBlock {
Free,
File(usize),
}
#[derive(Debug)]
pub struct FreeRegion {
start: usize,
len: usize,
}
#[derive(Debug)]
pub struct File {
id: usize,
start: usize,
len: usize,
}
#[derive(Debug)]
pub struct Disk {
blocks: Vec<DiskBlock>,
first_free_idx: usize,
last_block: usize,
}
impl Disk {
pub fn new(blocks: Vec<DiskBlock>, first_free_idx: usize, last_block: usize) -> Self {
Disk {
blocks,
first_free_idx,
last_block,
}
}
pub fn defrag_simple(&mut self) {
while self.first_free_idx < self.last_block {
self.blocks.swap(self.first_free_idx, self.last_block);
while let DiskBlock::File(_) = self.blocks[self.first_free_idx] {
self.first_free_idx += 1;
}
while let DiskBlock::Free = self.blocks[self.last_block] {
self.last_block -= 1;
}
}
}
pub fn defrag_files(&mut self) {
let mut files = self.files();
let mut free_regions = self.free_regions();
let last_file_id = files.last().unwrap().id;
for i in (0..last_file_id + 1).rev() {
let file = files.iter_mut().find(|f| f.id == i).unwrap();
let file_start = file.start;
let file_size = file.len;
let region = free_regions
.iter_mut()
.find(|r| r.len >= file_size && r.start < file_start);
if let Some(r) = region {
self.swap_multiple(r.start, file_start, file_size);
file.start = r.start;
r.start = file_start;
free_regions = self.free_regions();
}
}
}
pub fn checksum(&self) -> i64 {
self.blocks
.iter()
.enumerate()
.filter_map(|(i, block)| match block {
DiskBlock::Free => None,
DiskBlock::File(id) => Some((id * i) as i64),
})
.sum::<i64>()
}
fn swap_multiple(&mut self, i: usize, j: usize, count: usize) {
for k in 0..count {
self.blocks.swap(i + k, j + k);
}
}
fn free_regions(&self) -> Vec<FreeRegion> {
let mut regions = Vec::new();
let mut start = 0;
let mut len = 0;
for (i, block) in self.blocks.iter().enumerate() {
match block {
DiskBlock::Free => {
if len == 0 {
start = i;
}
len += 1;
}
DiskBlock::File(_) => {
if len > 0 {
regions.push(FreeRegion { start, len });
len = 0;
}
}
}
}
if len > 0 {
regions.push(FreeRegion { start, len });
}
regions
}
fn files(&self) -> Vec<File> {
let mut files = Vec::new();
let mut start = 0;
let mut len = 0;
let mut id = 0;
for (i, block) in self.blocks.iter().enumerate() {
match block {
DiskBlock::File(file_id) => {
if len == 0 {
start = i;
id = *file_id;
}
if *file_id != id {
files.push(File { id, start, len });
start = i;
id = *file_id;
len = 0;
}
len += 1;
}
DiskBlock::Free => {
if len > 0 {
files.push(File { id, start, len });
len = 0;
}
}
}
}
if len > 0 {
files.push(File { id, start, len });
}
files
}
}
impl Display for Disk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s = String::new();
for block in &self.blocks {
match block {
DiskBlock::Free => s.push('.'),
DiskBlock::File(id) => s.push_str(&id.to_string()),
}
}
writeln!(f, "{}", s)?;
writeln!(f, "First free: {}", self.first_free_idx)?;
writeln!(f, "Last block: {}", self.last_block)
}
}
impl From<String> for Disk {
fn from(s: String) -> Self {
let mut first_free_idx = 0;
let mut last_block = 0;
let mut num_blocks = 0;
let blocks: Vec<DiskBlock> = s
.chars()
.enumerate()
.flat_map(|(i, c)| {
if !c.is_ascii_digit() {
return vec![];
}
let digit = c.to_digit(10).unwrap() as usize;
if i % 2 != 0 {
if first_free_idx == 0 {
first_free_idx = num_blocks;
}
num_blocks += digit;
return vec![DiskBlock::Free; digit];
}
let file_id = i / 2;
num_blocks += digit;
last_block = num_blocks - 1;
vec![DiskBlock::File(file_id); digit]
})
.collect();
Disk::new(blocks, first_free_idx, last_block)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::parse_input as util_parse;
#[test]
fn test_example_part1() {
let mut disk = util_parse::<Disk>("day09", "example.txt");
disk.defrag_simple();
assert_eq!(disk.checksum(), 1928);
}
#[test]
fn test_part1() {
let mut disk = util_parse::<Disk>("day09", "puzzle.txt");
disk.defrag_simple();
assert_eq!(disk.checksum(), 6216544403458);
}
#[test]
fn test_example_part2() {
let mut disk = util_parse::<Disk>("day09", "example.txt");
disk.defrag_files();
assert_eq!(disk.checksum(), 2858);
}
#[test]
fn test_part2() {
let mut disk = util_parse::<Disk>("day09", "puzzle.txt");
disk.defrag_files();
assert_eq!(disk.checksum(), 6237075041489);
}
}