generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.rs
162 lines (135 loc) · 3.98 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
use num::Integer;
advent_of_code::solution!();
pub fn part_one(input: &str) -> Option<u64> {
let input = if input.len().is_even() {
// If the last item is a free space, ignore it
&input[..input.len() - 1]
} else {
input
};
let mut data = input.bytes().map(|n| {
debug_assert!(n.is_ascii_digit());
n - b'0'
});
// The id of the next file from the start
let mut left_file_id = 0;
// The id of the next file from the end
let mut right_file_id = input.len() / 2;
let mut block_position = 0;
let mut checksum = 0;
// The amount of data to transfer from the end
let mut pending_file_to_transfer = 0;
'outer: loop {
let Some(file_size) = data.next() else {
break;
};
for _ in 0..file_size {
checksum += block_position * left_file_id;
block_position += 1;
}
left_file_id += 1;
let Some(mut space) = data.next() else {
break;
};
while space > 0 {
if pending_file_to_transfer == 0 {
let Some(file_size_at_end) = data.next_back() else {
break 'outer;
};
// consume space at end (we don't need it)
data.next_back();
pending_file_to_transfer = file_size_at_end;
}
if pending_file_to_transfer <= space {
for _ in 0..pending_file_to_transfer {
checksum += block_position * right_file_id;
block_position += 1;
}
space -= pending_file_to_transfer;
pending_file_to_transfer = 0;
right_file_id -= 1;
} else {
for _ in 0..space {
checksum += block_position * right_file_id;
block_position += 1;
}
pending_file_to_transfer -= space;
space = 0;
}
}
}
for _ in 0..pending_file_to_transfer {
checksum += block_position * right_file_id;
block_position += 1;
}
Some(checksum.try_into().unwrap())
}
pub fn part_two(input: &str) -> Option<u64> {
let input = if input.len().is_even() {
// If the last item is a free space, ignore it
&input[..input.len() - 1]
} else {
input
};
struct File {
pos: usize,
id: usize,
size: usize,
}
struct Space {
pos: usize,
size: usize,
}
let half_len = input.len() / 2;
let mut files = Vec::with_capacity(half_len + 1);
let mut spaces = Vec::with_capacity(half_len);
let data = input
.bytes()
.map(|n| {
debug_assert!(n.is_ascii_digit());
(n - b'0') as usize
})
.enumerate();
let mut pos = 0;
for (i, size) in data {
if i.is_even() {
files.push(File {
pos,
id: i / 2,
size,
});
} else {
spaces.push(Space { pos, size });
}
pos += size;
}
for file in files.iter_mut().rev() {
if let Some(space) = spaces
.iter_mut()
.find(|space| space.pos < file.pos && space.size >= file.size)
{
file.pos = space.pos;
space.size -= file.size;
space.pos += file.size;
}
}
let checksum: usize = files
.into_iter()
.map(|File { pos, id, size }| (pos..pos + size).sum::<usize>() * id)
.sum();
Some(checksum.try_into().unwrap())
}
#[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));
}
}