From 7845628111e92360915da8077be98465c8735244 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Thu, 5 Dec 2019 23:32:45 -0500 Subject: [PATCH] Completed day 4 --- day4/Cargo.lock | 6 ++++ day4/Cargo.toml | 9 ++++++ day4/src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 day4/Cargo.lock create mode 100644 day4/Cargo.toml create mode 100644 day4/src/main.rs diff --git a/day4/Cargo.lock b/day4/Cargo.lock new file mode 100644 index 0000000..ca33fa2 --- /dev/null +++ b/day4/Cargo.lock @@ -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" + diff --git a/day4/Cargo.toml b/day4/Cargo.toml new file mode 100644 index 0000000..824483a --- /dev/null +++ b/day4/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day4" +version = "0.1.0" +authors = ["Tyler Hallada "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day4/src/main.rs b/day4/src/main.rs new file mode 100644 index 0000000..2569966 --- /dev/null +++ b/day4/src/main.rs @@ -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()); +}