-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
80 lines (71 loc) · 2.5 KB
/
main.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
use std::collections::VecDeque;
fn main() {
let data = include_bytes!("./input.txt");
let layout = data
.iter()
.enumerate()
.map(|(idx, n)| ((idx % 2) == 0, (idx / 2) as u64, (n - b'0') as u64))
.filter(|&(_, _, x)| x > 0)
.collect::<Vec<_>>();
let mut deq = layout
.iter()
.cloned()
.collect::<VecDeque<(bool, u64, u64)>>();
let mut part1 = 0;
let mut i = 0;
while deq.len() > 0 {
let (is_file, file_id, repeat) = deq.pop_front().unwrap();
if is_file {
part1 += file_id * (i..i + repeat).sum::<u64>();
i += repeat;
} else {
while let Some((tail_is_file, tail_file_id, tail_repeat)) = deq.pop_back() {
if tail_is_file {
let files_moved = if tail_repeat <= repeat {
deq.push_front((false, file_id, repeat - tail_repeat));
tail_repeat
} else {
deq.push_back((true, tail_file_id, tail_repeat - repeat));
repeat
};
part1 += tail_file_id * (i..i + files_moved).sum::<u64>();
i += files_moved;
break;
}
}
}
}
println!("part1 {}", part1);
let (_, mut holes, files) = layout
.iter()
.fold((0, Vec::new(), Vec::new()), |mut acc, &elem| {
let (is_file, file_id, repeat) = elem;
if !is_file {
acc.1.push((acc.0, repeat))
} else {
acc.2.push((acc.0, file_id, repeat))
}
acc.0 += repeat;
acc
});
let mut part2 = 0;
for &(i, file_id, repeat) in files.iter().rev() {
let h = holes
.iter()
.position(|&(idx, hole_size)| idx < i && hole_size >= repeat);
if let Some(hidx) = h {
let (start_idx, hole_size) = holes[hidx];
if hole_size == repeat {
holes.remove(hidx);
part2 += file_id * (start_idx..start_idx + repeat).sum::<u64>();
} else {
let (start_idx, hole_size) = holes[hidx];
holes[hidx] = (start_idx + repeat, hole_size - repeat);
part2 += file_id * (start_idx..start_idx + repeat).sum::<u64>();
}
} else {
part2 += file_id * (i..i + repeat).sum::<u64>();
}
}
println!("part2 {}", part2);
}