Skip to content

Commit b189618

Browse files
committed
make code cleaner
1 parent c0db099 commit b189618

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

2024/day5/src/main.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ fn part1(input: &str) -> usize {
77
let mut map = HashMap::<usize, Vec<usize>>::new();
88
split.0.lines().for_each(|l| {
99
let s = l.split_once('|').unwrap();
10+
let value = s.0.parse::<usize>().unwrap();
1011
map.entry(s.1.parse::<usize>().unwrap())
1112
.or_default()
12-
.push(s.0.parse::<usize>().unwrap());
13+
.push(value);
14+
map.entry(value).or_default();
1315
});
1416
map
1517
};
@@ -21,7 +23,7 @@ fn part1(input: &str) -> usize {
2123
updates
2224
.filter_map(|update| {
2325
update
24-
.is_sorted_by(|a, b| ordering.get(b).map(|v| v.contains(a)).unwrap_or_default())
26+
.is_sorted_by(|a, b| ordering[b].contains(a))
2527
.then_some(update[update.len() / 2])
2628
})
2729
.sum()
@@ -32,9 +34,11 @@ fn part2(input: &str) -> usize {
3234
let mut map = HashMap::<usize, Vec<usize>>::new();
3335
split.0.lines().for_each(|l| {
3436
let s = l.split_once('|').unwrap();
37+
let value = s.0.parse::<usize>().unwrap();
3538
map.entry(s.1.parse::<usize>().unwrap())
3639
.or_default()
37-
.push(s.0.parse::<usize>().unwrap());
40+
.push(value);
41+
map.entry(value).or_default();
3842
});
3943
map
4044
};
@@ -45,16 +49,8 @@ fn part2(input: &str) -> usize {
4549
});
4650
updates
4751
.filter_map(|mut update| {
48-
if !update
49-
.is_sorted_by(|a, b| ordering.get(b).map(|v| v.contains(a)).unwrap_or_default())
50-
{
51-
update.sort_by(|a, b| {
52-
ordering
53-
.get(b)
54-
.map(|v| v.contains(a))
55-
.unwrap_or_default()
56-
.cmp(&true)
57-
});
52+
if !update.is_sorted_by(|a, b| ordering[b].contains(a)) {
53+
update.sort_by(|a, b| ordering[b].contains(a).cmp(&true));
5854
return Some(update[update.len() / 2]);
5955
}
6056
None

0 commit comments

Comments
 (0)