Skip to content

Instantly share code, notes, and snippets.

@FreeMasen
Created March 1, 2021 14:48
Show Gist options
  • Save FreeMasen/22d514ff397172f58321d074548a68b5 to your computer and use it in GitHub Desktop.
Save FreeMasen/22d514ff397172f58321d074548a68b5 to your computer and use it in GitHub Desktop.
pub fn is_valid2(code: &str) -> bool {
code.chars().filter(|&c| c != ' ').count() > 1
&& code.chars().filter(|&c| c != ' ').all(|c| c.is_digit(10))
&& code
.chars()
.filter(|&c| c != ' ')
.rev()
.enumerate()
.fold(0, |acc, (i, c)| match (i, c.to_digit(10).unwrap()) {
(i, d) if i % 2 == 0 => acc + d,
(_i, d) if d * 2 > 9 => acc + 2 * d - 9,
(_i, d) => acc + 2 * d,
})
% 10
== 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment