From 1ca0ee331ae9829dcda73c56c6b2df2fe1b903c0 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 4 Dec 2019 17:02:36 -0500 Subject: [PATCH] Update day 4 with part 1 solution Signed-off-by: Alek Ratzloff --- day04/Day04.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/day04/Day04.rs b/day04/Day04.rs index 76df095..66674ed 100644 --- a/day04/Day04.rs +++ b/day04/Day04.rs @@ -1,11 +1,67 @@ -//use std::io::{self, Read}; - -//type Result = std::result::Result>; - fn main() { part1(246515, 739105); + part2(246515, 739105); +} + +fn digit_count(n: usize) -> u32 { + if n == 0 { 1 } else { (n as f64).log10().floor() as u32 + 1 } +} + +fn digit_n(digit: u32, n: usize) -> usize { + let digit_count = digit_count(n); + println!("{} {} {}", n, digit_count, digit); + let place = 10usize.pow(digit_count - digit); + (n / place) % 10 +} + +fn is_increasing(n: usize) -> bool { + for digit in 0 .. 5 { + let this = digit_n(digit, n); + let next = digit_n(digit + 1, n); + if next < this { + return false; + } + } + true +} + +fn has_double(n: usize) -> bool { + let digit_count = digit_count(n); + for digit in 0 .. digit_count { + let this = digit_n(digit, n); + let next = digit_n(digit + 1, n); + if next == this { + return true; + } + } + false +} + +fn has_strict_double(n: usize) -> bool { + false } fn part1(lower: usize, upper: usize) { - + let mut count = 0; + for n in lower ..= upper { + if is_increasing(n) && has_double(n) { + count += 1; + } + } + println!("Part 1: {}", count); +} + +fn part2(lower: usize, upper: usize) { + let mut count = 0; + + for n in &[112233, 123444, 111122, 144455, 114444] { + println!("{} has_strict_double: {}", n, has_strict_double(*n)); + } + + for n in lower ..= upper { + if is_increasing(n) && has_strict_double(n) { + count += 1; + } + } + println!("Part 2: {}", count); }