Update day 4 with part 1 solution

Signed-off-by: Alek Ratzloff <alekr@jsausa.com>
This commit is contained in:
Alek Ratzloff
2019-12-04 17:02:36 -05:00
parent 629afd166d
commit 1ca0ee331a

View File

@@ -1,11 +1,67 @@
//use std::io::{self, Read};
//type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn main() { fn main() {
part1(246515, 739105); 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) { 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);
} }