Skip to content

Commit

Permalink
feat(02/2024): use cmp instead of if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 2, 2024
1 parent 0504f42 commit 6a03c74
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/solutions/year2024/day02.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::solutions::year2024::day02::State::{Decreasing, Equals, Increasing};
use crate::solutions::Solution;
use std::cmp::Ordering;

pub struct Day02;

Expand All @@ -10,7 +10,7 @@ impl Solution for Day02 {
input
.iter()
.filter(|report| {
let mut state: Option<State> = None;
let mut state: Option<Ordering> = None;

for i in 0..report.len() - 1 {
let first = report.get(i).unwrap();
Expand All @@ -20,18 +20,9 @@ impl Solution for Day02 {
return false;
}

let current_state = if first < second {
Increasing
} else if first > second {
Decreasing
} else if first == second {
Equals
} else {
unreachable!()
};

let current_state = first.cmp(second);
if let Some(inner_state) = state {
if inner_state == Equals {
if inner_state == Ordering::Equal {
return false;
}
if inner_state != current_state {
Expand Down Expand Up @@ -66,13 +57,6 @@ impl Day02 {
}
}

#[derive(Copy, Clone, PartialEq)]
enum State {
Increasing,
Decreasing,
Equals,
}

#[cfg(test)]
mod tests {
use crate::solutions::year2024::day02::Day02;
Expand Down

0 comments on commit 6a03c74

Please sign in to comment.