diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2019-12-05 00:29:40 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2019-12-05 00:29:40 +0200 |
commit | fb535480020886dfbf41f682f1b9f7b284249912 (patch) | |
tree | 4a8f93f678fa5525fc4bb8f444310fb478a6b528 /src | |
parent | 3d1a9640016ee8c065ab49d0060d6cd0c3325aad (diff) |
Day 4 - some quick iteration
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/day_4.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/bin/day_4.rs b/src/bin/day_4.rs new file mode 100644 index 0000000..3cb8d38 --- /dev/null +++ b/src/bin/day_4.rs @@ -0,0 +1,59 @@ +use std::io; +use std::io::prelude::*; + +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(name = "Day 4: Secure Container")] +/// Calculates how many possible lock values there are +/// +/// See https://adventofcode.com/2019/day/4 for details. +struct Opt { + /// Repeated digits must be exactly 2 long + #[structopt(long = "larger-range-rule")] + larger_range_rule: bool, + min: u32, + max: u32, +} + +fn main() { + let stdin = io::stdin(); + let opt = Opt::from_args(); + + println!( + "{}", + valid_combinations(opt.min, opt.max, opt.larger_range_rule) + ) +} + +fn valid_combinations(min: u32, max: u32, larger_range_rule: bool) -> u32 { + (min..max) + .filter(|x| { + two_adjacent_identical_digits(*x, larger_range_rule) && digits_never_decrease(*x) + }) + .count() as u32 +} + +fn two_adjacent_identical_digits(x: u32, larger_range_rule: bool) -> bool { + if larger_range_rule { + (0..5).any(|d| { + digit(x, d) == digit(x, d + 1) + && digit(x, d) != digit(x, d + 2) + && digit(x, d) != digit(x, d - 1) + }) + } else { + (0..5).any(|d| digit(x, d) == digit(x, d + 1)) + } +} + +fn digits_never_decrease(x: u32) -> bool { + (0..5).all(|d| digit(x, d) >= digit(x, d + 1)) +} + +fn digit(x: u32, digit: i32) -> u32 { + if digit < 0 { + 0 + } else { + (x / 10u32.pow(digit as u32)) % 10 + } +} |