Skip to content

Commit 7ae763e

Browse files
committed
Solve part 1 :)
1 parent 990c375 commit 7ae763e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: src/2024/01.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
use eyre::{eyre, OptionExt, Report, Result};
2+
use rayon::prelude::*;
3+
14
use crate::meta::Problem;
25

3-
pub const HISTORIAN_HYSTERIA: Problem = Problem::unsolved();
6+
pub const HISTORIAN_HYSTERIA: Problem = Problem::partially_solved(&|input| -> Result<usize> {
7+
let (lhs, rhs) = input
8+
.lines()
9+
.map(|line| {
10+
let mut iter = line.split_ascii_whitespace();
11+
let a_str = iter.next().ok_or_eyre("empty line")?;
12+
let b_str = iter
13+
.next()
14+
.ok_or_else(|| eyre!("No whitespace on line: \"{line}\""))?;
15+
16+
debug_assert!(iter.next().is_none());
17+
18+
let a = a_str.parse()?;
19+
let b = b_str.parse()?;
20+
21+
Ok::<(usize, usize), Report>((a, b))
22+
})
23+
.try_fold((Vec::new(), Vec::new()), |(mut lhs, mut rhs), res| {
24+
let (a, b) = res?;
25+
26+
lhs.push(a);
27+
rhs.push(b);
28+
29+
Ok::<_, Report>((lhs, rhs))
30+
})
31+
.map(|(mut lhs, mut rhs)| {
32+
lhs.sort_unstable();
33+
rhs.sort_unstable();
34+
35+
(lhs, rhs)
36+
})?;
37+
38+
Ok(lhs
39+
.into_par_iter()
40+
.zip(rhs)
41+
.map(|(a, b)| a.abs_diff(b))
42+
.sum())
43+
});

Diff for: tests/integration.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ util::tests! {
3636
6: ["tzstqsua", "myregdnr"],
3737
7: [115, 231],
3838
8: [115, "EFEYKFRFIJ"],
39-
9: [102239, 10780403063u64]
39+
9: [102239, #[ignore = "slow implementation"] 10780403063u64]
40+
},
41+
42+
2024: {
43+
1: [2000468]
4044
}
4145
}
4246

0 commit comments

Comments
 (0)