-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
46 lines (43 loc) · 1.52 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
use ahash::AHashMap;
pub fn main() {
let data = include_bytes!("../input.txt");
let mid = data.windows(2).position(|b| b == b"\n\n").unwrap();
let mut orders: AHashMap<_, Vec<_>> =
data[0..mid]
.split(|&b| b == b'\n')
.fold(AHashMap::new(), |mut orders, range| {
let mut split = range.split(|&b| b == b'|');
orders
.entry(atoi::atoi::<usize>(split.next().unwrap()).unwrap())
.or_default()
.push(atoi::atoi::<usize>(split.next().unwrap()).unwrap());
orders
});
orders.values_mut().for_each(|pages| pages.sort_unstable());
println!(
"{}",
data[mid + 2..]
.split(|&b| b == b'\n')
.map(|pages| {
pages
.split(|&b| b == b',')
.map(|page| atoi::atoi::<usize>(page).unwrap())
.collect::<Vec<_>>()
})
.filter(|pages| {
for (i, page) in pages.iter().enumerate() {
if let Some(orders) = orders.get(page) {
if pages[0..i]
.iter()
.any(|&page| orders.binary_search(&page).is_ok())
{
return false;
}
}
}
true
})
.map(|pages| pages[pages.len() / 2])
.sum::<usize>(),
);
}