Skip to content

Commit aa9eb1b

Browse files
committed
Day 1
1 parent e1f6a8c commit aa9eb1b

File tree

3 files changed

+1038
-5
lines changed

3 files changed

+1038
-5
lines changed

src/day_01.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1-
pub fn part_1() -> usize {
2-
0
1+
use itertools::Itertools;
2+
use num::abs;
3+
4+
use crate::input::day_01::INPUT;
5+
6+
pub fn part_1() -> i64 {
7+
list(un).zip(list(deux)).map(|(a, b)| abs(a - b)).sum()
8+
}
9+
10+
pub fn part_2() -> i64 {
11+
let counts = list(deux).counts();
12+
list(un)
13+
.map(|n| n * (counts.get(&n).copied().unwrap_or(0) as i64))
14+
.sum()
15+
}
16+
17+
fn list<F>(f: F) -> impl Iterator<Item = i64>
18+
where
19+
F: Fn(&(i64, i64)) -> i64,
20+
{
21+
INPUT.iter().map(f).sorted_unstable()
22+
}
23+
24+
fn un(value: &(i64, i64)) -> i64 {
25+
value.0
26+
}
27+
28+
fn deux(value: &(i64, i64)) -> i64 {
29+
value.1
330
}

0 commit comments

Comments
 (0)