From 7ad7138e93f14bae45398c17e7c7e00d99fe20c6 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Mon, 6 Dec 2021 22:35:42 -0500 Subject: [PATCH] Day 4 --- day4/Cargo.lock | 7 ++++ day4/Cargo.toml | 8 ++++ day4/src/main.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 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..e92c02c --- /dev/null +++ b/day4/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day4" +version = "0.1.0" diff --git a/day4/Cargo.toml b/day4/Cargo.toml new file mode 100644 index 0000000..8842130 --- /dev/null +++ b/day4/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day4" +version = "0.1.0" +edition = "2021" + +# 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..b05e60e --- /dev/null +++ b/day4/src/main.rs @@ -0,0 +1,101 @@ +fn main() { + let min = [1, 9, 3, 6, 5, 1]; + let max = [6, 4, 9, 7, 2, 9]; + + let mut matches = 0; + let mut i = min.clone(); + loop { + // Check for a adjacents + if i[0] == i[1] || + i[1] == i[2] || + i[2] == i[3] || + i[3] == i[4] || + i[4] == i[5] + { + // Check for incrementing + if i[0] <= i[1] && + i[1] <= i[2] && + i[2] <= i[3] && + i[3] <= i[4] && + i[4] <= i[5] + { + //println!("{:?} matches", i); + matches += 1; + } + } + let mut x = 5; + loop { + i[x] += 1; + if i[x] >= 10 { + i[x] -= 10; + x -= 1; + continue + } + break; + } + if i[0] >= max[0] && + i[1] >= max[1] && + i[2] >= max[2] && + i[3] >= max[3] && + i[4] >= max[4] && + i[5] >= max[5] { + break; + } + } + println!("[Part 1] There are {} potential matches", matches); + + i = min.clone(); + matches = 0; + loop { + // Check for incrementing + if i[0] <= i[1] && + i[1] <= i[2] && + i[2] <= i[3] && + i[3] <= i[4] && + i[4] <= i[5] + { + // Check for groups + let mut idx = 0; + let mut has_two_chain = false; + let mut chain_size = 1; + while idx < 5 { + if i[idx] == i[idx+1] { + chain_size += 1; + idx += 1; + } + else { + if chain_size == 2 { + has_two_chain = true; + } + chain_size = 1; + idx += 1; + } + } + if chain_size == 2 { + has_two_chain = true; + } + if has_two_chain { + matches += 1; + } + } + let mut x = 5; + loop { + i[x] += 1; + if i[x] >= 10 { + i[x] -= 10; + x -= 1; + continue + } + break; + } + if i[0] >= max[0] && + i[1] >= max[1] && + i[2] >= max[2] && + i[3] >= max[3] && + i[4] >= max[4] && + i[5] >= max[5] { + break; + } + } + println!("[Part 2] There are {} potential matches", matches); +}