generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.rs
132 lines (120 loc) · 4.09 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
use std::ops::Range;
use itertools::Itertools;
advent_of_code::solution!(9);
#[derive(Debug)]
struct DiskMap<T> {
data: Vec<T>
}
impl DiskMap<Option<usize>> {
fn parse(input: &str) -> DiskMap<Option<usize>> {
let data = (&input.chars().chunks(2)).into_iter()
.enumerate()
.flat_map(|(id_number, mut chunk)| {
let Some(Some(file_size)) = chunk.next().map(|char| char.to_digit(10)) else { return Vec::new() };
let mut file = vec![Some(id_number); file_size as usize];
if let Some(Some(free_space)) = chunk.next().map(|char| char.to_digit(10)) {
file.append(&mut vec![None; free_space as usize]);
file
} else {
file
}
})
.collect();
DiskMap {
data
}
}
fn checksum(&self) -> u64 {
let len = self.data.len();
let mut sum = 0;
let (mut head, mut tail) = (1, len);
while head < tail {
let mut value = None;
if let Some(val) = self.data[head] {
value = Some(val);
} else {
while head < tail {
tail -= 1;
if let Some(val) = self.data[tail] {
value = Some(val);
break;
};
}
}
let Some(value) = value else { break };
sum += head * value;
head += 1;
}
sum as u64
}
}
#[derive(Default, Clone, Debug)]
struct Interval {
id: u64,
interval: Range<u32>,
}
impl DiskMap<Interval> {
fn parse_intervals(input: &str) -> DiskMap<Interval> {
let mut last_index = 0;
let data = (&input.chars().chunks(2)).into_iter()
.enumerate()
.map(|(id_number, mut chunk)| {
let Some(Some(file_size)) = chunk.next().map(|char| char.to_digit(10)) else { return Interval::default() };
let file = Interval {
id: id_number as u64,
interval: last_index..(last_index + file_size)
};
last_index = file.interval.end;
if let Some(Some(free_space)) = chunk.next().map(|char| char.to_digit(10)) {
last_index += free_space;
}
file
})
.collect_vec();
DiskMap {
data
}
}
fn checksum(&self) -> u64 {
let mut data = self.data.clone();
for index in (1..data.len()).rev() {
let interval = &self.data[index];
let actual_index = data.iter().rposition(|x| x.id == interval.id).unwrap();
let interval_size = interval.interval.end - interval.interval.start;
let Some((new_index, start_index, _)) = (0..actual_index)
.map(|index| (index, &data[index].interval, &data[index + 1].interval))
.map(|(index, x, y)| (index, x.end, y.start - x.end))
.find(|(_, _, size)| size >= &(interval_size)) else { continue };
let interval = Interval {
interval: start_index..(start_index + interval_size),
..*interval
};
data.remove(actual_index);
data.insert(new_index + 1, interval);
}
data.iter()
.map(|Interval { id, interval }| interval.clone().map(|x| id * (x as u64)).sum::<u64>())
.sum()
}
}
pub fn part_one(input: &str) -> Option<u64> {
Some(DiskMap::parse(input).checksum())
}
pub fn part_two(input: &str) -> Option<u64> {
let disk_map = DiskMap::parse_intervals(input);
Some(disk_map.checksum())
}
#[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));
}
}