Skip to content

Commit

Permalink
Avoid using unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiac committed Dec 15, 2023
1 parent 3a26ddc commit 4fe11f8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions aoc-solver/src/y2023/day15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Solution for Day15 {
let sum = input
.trim()
.split(',')
.fold(initial_boxes, |mut boxes, step| {
.try_fold(initial_boxes, |mut boxes, step| {
if let Some(label) = step.strip_suffix('-') {
let index = hash(label);

Expand All @@ -39,7 +39,9 @@ impl Solution for Day15 {
}
} else if let Some((label, focal_length)) = step.split_once('=') {
let index = hash(label);
let focal_length = focal_length.parse().unwrap();
let focal_length = focal_length
.parse()
.map_err(|err| AocError::parse(focal_length, err))?;

if let Some(lens) = boxes[index].iter().position(|(l, _)| *l == label) {
boxes[index][lens] = (label, focal_length)
Expand All @@ -48,8 +50,8 @@ impl Solution for Day15 {
}
}

boxes
})
Ok(boxes)
})?
.iter()
.enumerate()
.map(|(box_number, lens_box)| {
Expand Down

0 comments on commit 4fe11f8

Please sign in to comment.