generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.rs
199 lines (185 loc) · 6.92 KB
/
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
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
use std::{collections::VecDeque, ops::Div};
advent_of_code::solution!(9);
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
enum Block {
File(u32),
Free(u32),
}
pub fn part_one(input: &str) -> Option<u64> {
let mut queue: VecDeque<Block> =
VecDeque::from_iter(input.chars().enumerate().filter_map(|(idx, ch)| {
if let Some(value) = ch.to_digit(10) {
if idx % 2 == 0 {
Some(Block::File(value))
} else {
Some(Block::Free(value))
}
} else {
None
}
}));
let mut curr_lower_file_id: u64 = 0;
let mut curr_upper_file_id: u64 = (queue.len() - 2).div_ceil(2) as u64;
let mut curr_idx = 0;
let mut result: u64 = 0;
while !queue.is_empty() {
let Some(front_candidate) = queue.pop_front() else {
break;
};
match front_candidate {
Block::File(size) => {
for offset in 0..size {
result += curr_lower_file_id * (curr_idx + offset) as u64;
}
curr_idx += size;
curr_lower_file_id += 1;
}
Block::Free(size) => {
let r_size = if let Some(r) = queue.pop_back() {
match r {
Block::File(r_size) => r_size,
Block::Free(_) => {
if let Some(r2) = queue.pop_back() {
match r2 {
Block::File(r2_size) => r2_size,
Block::Free(_) => panic!("Double free lol"),
}
} else {
break;
}
}
}
} else {
break;
};
match size.cmp(&r_size) {
std::cmp::Ordering::Less => {
for offset in 0..size {
result += curr_upper_file_id * (curr_idx + offset) as u64;
}
queue.push_back(Block::File(r_size - size));
curr_idx += size;
}
std::cmp::Ordering::Equal => {
for offset in 0..size {
result += curr_upper_file_id * (curr_idx + offset) as u64;
}
curr_upper_file_id -= 1;
curr_idx += size;
}
std::cmp::Ordering::Greater => {
for offset in 0..r_size {
result += curr_upper_file_id * (curr_idx + offset) as u64;
}
queue.push_front(Block::Free(size - r_size));
curr_upper_file_id -= 1;
curr_idx += r_size;
}
}
}
}
}
Some(result)
}
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
enum Block2 {
File(u32, u32),
Free(u32),
}
pub fn part_two(input: &str) -> Option<u64> {
let mut queue: VecDeque<Block2> =
VecDeque::from_iter(input.chars().enumerate().filter_map(|(idx, ch)| {
if let Some(value) = ch.to_digit(10) {
if idx % 2 == 0 {
Some(Block2::File(value, (idx as u32).div(2)))
} else {
Some(Block2::Free(value))
}
} else {
None
}
}));
let mut curr_idx = 0;
let mut result: u64 = 0;
while !queue.is_empty() {
let Some(front_candidate) = queue.pop_front() else {
break;
};
match front_candidate {
Block2::File(size, id) => {
for offset in 0..size {
result += id as u64 * (curr_idx + offset) as u64;
}
curr_idx += size;
}
Block2::Free(size) => {
// First get next right side candidate that fits
let mut to_readd_to_queue = VecDeque::new();
let mut r_candidate = None;
loop {
if queue.is_empty() {
queue.extend(to_readd_to_queue);
break;
}
if let Some(r) = queue.pop_back() {
match r {
Block2::File(r_size, id) => match size.cmp(&r_size) {
std::cmp::Ordering::Greater | std::cmp::Ordering::Equal => {
r_candidate = Some((r_size, id));
to_readd_to_queue.push_front(Block2::Free(r_size));
queue.extend(to_readd_to_queue);
break;
}
std::cmp::Ordering::Less => {
to_readd_to_queue.push_front(Block2::File(r_size, id));
}
},
Block2::Free(_) => {
to_readd_to_queue.push_front(r);
}
}
} else {
queue.extend(to_readd_to_queue);
break;
};
}
// Now fit it if it can
if let Some(r_can) = r_candidate {
match size.cmp(&r_can.0) {
std::cmp::Ordering::Less => panic!("Should not be possible"),
std::cmp::Ordering::Equal => {
for offset in 0..r_can.0 {
result += r_can.1 as u64 * (curr_idx + offset) as u64;
}
curr_idx += r_can.0;
}
std::cmp::Ordering::Greater => {
for offset in 0..r_can.0 {
result += r_can.1 as u64 * (curr_idx + offset) as u64;
}
curr_idx += r_can.0;
queue.push_front(Block2::Free(size - r_can.0));
}
}
} else {
curr_idx += size;
}
}
}
}
Some(result)
}
#[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(1928));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(2858));
}
}