-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_09.rs
155 lines (120 loc) Β· 3.37 KB
/
day_09.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
use common::{solution, Answer};
use itertools::{repeat_n, Itertools};
solution!("Disk Fragmenter", 9);
fn part_a(input: &str) -> Answer {
let mut problem = Blocks::parse(input);
problem.sort();
problem.score().into()
}
fn part_b(input: &str) -> Answer {
let mut problem = Files::parse(input);
problem.sort();
problem.score().into()
}
struct Blocks {
blocks: Vec<Option<u32>>,
}
struct Files {
files: Vec<File>,
}
struct File {
pos: u32,
size: u8,
id: u32,
}
impl Blocks {
fn parse(input: &str) -> Self {
let mut blocks = Vec::new();
let mut id = 0;
for (idx, chr) in input.trim().char_indices() {
let count = chr.to_digit(10).unwrap() as u8;
let is_block = idx % 2 == 0;
let item = is_block.then_some(id);
blocks.extend(repeat_n(item, count as usize));
id += is_block as u32;
}
Self { blocks }
}
fn sort(&mut self) {
loop {
let empty = self.blocks.iter().position(|x| x.is_none()).unwrap();
let last = self.blocks.iter().rposition(|x| x.is_some()).unwrap();
if last <= empty {
break;
}
self.blocks.swap(empty, last);
}
}
fn score(&self) -> u64 {
self.blocks
.iter()
.enumerate()
.map(|(idx, id)| idx as u64 * id.unwrap_or_default() as u64)
.sum()
}
}
impl Files {
fn parse(input: &str) -> Self {
let mut files = Vec::new();
let (mut id, mut pos) = (0, 0);
for (idx, chr) in input.trim().char_indices() {
let size = chr.to_digit(10).unwrap() as u8;
if idx % 2 == 0 {
files.push(File { pos, size, id });
id += 1;
}
pos += size as u32;
}
Self { files }
}
fn sort(&mut self) {
let max_id = self.files.last().unwrap().id;
for id in (0..=max_id).rev() {
let file_idx = self.files.iter().position(|x| x.id == id).unwrap();
let file = &self.files[file_idx];
let mut new_pos = None;
for (a, b) in self.files.iter().tuple_windows() {
let free = (b.pos) - (a.pos + a.size as u32);
let pos = a.pos + a.size as u32;
if pos > file.pos {
break;
}
if free >= file.size as u32 {
new_pos = Some(pos);
break;
}
}
if let Some(new_pos) = new_pos {
self.files[file_idx].pos = new_pos;
}
self.files.sort_by_key(|x| x.pos);
}
}
fn score(&self) -> u64 {
let mut sum = 0;
let mut last = 0;
let mut idx = 0;
for x in &self.files {
idx += x.pos - last;
sum += (x.id as u64 * x.size as u64 * (x.size as u64 + 2 * idx as u64 - 1)) / 2;
idx += x.size as u32;
last = x.pos + x.size as u32;
}
sum
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
2333133121414131402
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 1928.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 2858.into());
}
}