Day 4 part 1

This commit is contained in:
Wesley Moore 2019-12-04 20:17:27 +11:00
parent 7d282f886f
commit da510f364c
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE

42
2019/src/bin/day4.rs Normal file
View file

@ -0,0 +1,42 @@
use std::ops::RangeInclusive;
const INPUT: RangeInclusive<u32> = 356261..=846303;
// However, they do remember a few key facts about the password:
// * It is a six-digit number.
// * The value is within the range given in your puzzle input.
// * Two adjacent digits are the same (like 22 in 122345).
// * Going from left to right, the digits never decrease; they only ever increase or stay the same
// (like 111123 or 135679).
fn main() {
let count = INPUT.filter(meets_criteria).count();
println!("Part 1: {}", count);
}
fn meets_criteria(number: &u32) -> bool {
let mut number = *number;
let mut digits = [0; 6];
digits.iter_mut().rev().for_each(|digit| {
*digit = number % 10;
number /= 10;
});
// adjacent digits and increasing digits
digits.windows(2).any(|pair| pair[0] == pair[1])
&& digits.windows(2).all(|pair| pair[1] >= pair[0])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_meets_criteria() {
assert!(meets_criteria(&111123));
assert!(meets_criteria(&135669));
assert!(!meets_criteria(&223450));
assert!(!meets_criteria(&123789));
}
}