This commit is contained in:
Kienan Stewart 2021-12-06 22:35:42 -05:00
parent b9d6d9ad9c
commit 7ad7138e93
3 changed files with 116 additions and 0 deletions

7
day4/Cargo.lock generated Normal file
View File

@ -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"

8
day4/Cargo.toml Normal file
View File

@ -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]

101
day4/src/main.rs Normal file
View File

@ -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);
}