Completed day 4
This commit is contained in:
parent
3b06ca1c64
commit
7845628111
6
day4/Cargo.lock
generated
Normal file
6
day4/Cargo.lock
generated
Normal file
@ -0,0 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
|
9
day4/Cargo.toml
Normal file
9
day4/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
72
day4/src/main.rs
Normal file
72
day4/src/main.rs
Normal file
@ -0,0 +1,72 @@
|
||||
const INPUT_MIN: u32 = 245318;
|
||||
const INPUT_MAX: u32 = 765747;
|
||||
|
||||
fn solve_part1() -> u32 {
|
||||
let mut counter = 0;
|
||||
for num in INPUT_MIN..=INPUT_MAX {
|
||||
let num_string = num.to_string();
|
||||
let mut previous = None;
|
||||
let mut has_double = false;
|
||||
let mut decreasing = false;
|
||||
for c in num_string.chars() {
|
||||
match previous {
|
||||
None => previous = Some(c),
|
||||
Some(p) => {
|
||||
if p == c {
|
||||
has_double = true;
|
||||
}
|
||||
if p.to_digit(10) > c.to_digit(10) {
|
||||
decreasing = true;
|
||||
break;
|
||||
}
|
||||
previous = Some(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if has_double && !decreasing {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
counter
|
||||
}
|
||||
|
||||
fn solve_part2() -> u32 {
|
||||
// too lazy to DRY it up
|
||||
let mut counter = 0;
|
||||
for num in INPUT_MIN..=INPUT_MAX {
|
||||
let num_string = num.to_string();
|
||||
let mut previous = None;
|
||||
let mut has_double = false;
|
||||
let mut matching_group_count = 1;
|
||||
let mut decreasing = false;
|
||||
for c in num_string.chars() {
|
||||
match previous {
|
||||
None => previous = Some(c),
|
||||
Some(p) => {
|
||||
if p == c {
|
||||
matching_group_count += 1;
|
||||
} else {
|
||||
if matching_group_count == 2 {
|
||||
has_double = true;
|
||||
}
|
||||
matching_group_count = 1;
|
||||
}
|
||||
if p.to_digit(10) > c.to_digit(10) {
|
||||
decreasing = true;
|
||||
break;
|
||||
}
|
||||
previous = Some(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (matching_group_count == 2 || has_double) && !decreasing {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
counter
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Part 1: {}", solve_part1());
|
||||
println!("Part 2: {}", solve_part2());
|
||||
}
|
Loading…
Reference in New Issue
Block a user