From fb41ac47f10163bca06cb545f8aa9f1ab674b323 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Mon, 28 Oct 2024 13:41:26 +0100 Subject: [PATCH] =?UTF-8?q?=C3=8Berste=20poging=20projecteuler=2090?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projecteuler/090/Cargo.lock | 18 +++++++++++ projecteuler/090/Cargo.toml | 1 + projecteuler/090/src/main.rs | 62 ++++++++++++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/projecteuler/090/Cargo.lock b/projecteuler/090/Cargo.lock index 88ffcd8..7b9c150 100644 --- a/projecteuler/090/Cargo.lock +++ b/projecteuler/090/Cargo.lock @@ -2,6 +2,24 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "main" version = "0.1.0" +dependencies = [ + "itertools", +] diff --git a/projecteuler/090/Cargo.toml b/projecteuler/090/Cargo.toml index fca4e93..222e4c3 100644 --- a/projecteuler/090/Cargo.toml +++ b/projecteuler/090/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +itertools = "0.12.1" diff --git a/projecteuler/090/src/main.rs b/projecteuler/090/src/main.rs index 1d837a3..978462c 100644 --- a/projecteuler/090/src/main.rs +++ b/projecteuler/090/src/main.rs @@ -1,10 +1,68 @@ +use itertools::Itertools; use std::time::Instant; +fn generate_cubes() -> Vec> { + (0..=9).combinations(6).collect() +} + +fn is_valid_dice_configuration(d1: &Vec, d2: &Vec) -> bool { + let necessary_matches: Vec<_> = [ + (0, 1), + (0, 4), + (0, 9), + (1, 6), + (2, 5), + (3, 6), + (4, 9), + (6, 4), + (8, 1), + ] + .into_iter() + .collect(); + + for (m1, m2) in necessary_matches { + if !(d1.contains(&m1) && d2.contains(&m2) || d1.contains(&m2) && d2.contains(&m1)) { + if m1 == 6 { + if !(d1.contains(&9) && d2.contains(&m2) || d1.contains(&m2) && d2.contains(&9)) { + return false; + } + } else if m2 == 6 { + if !(d1.contains(&m1) && d2.contains(&9) || d1.contains(&9) && d2.contains(&m2)) { + return false; + } + } else if m2 == 9 { + if !(d1.contains(&m1) && d2.contains(&6) || d1.contains(&6) && d2.contains(&m2)) { + return false; + } + } else { + return false; + } + } + } + + true +} + fn main() { println!("Hello, this is Patrick!"); let now = Instant::now(); - // We can't just loop through all possibilities because that would something in the order of - // (10*9*8*7*6*5)^2, which is a really big scary number + + let dice1 = generate_cubes(); + let dice2 = generate_cubes(); + + assert_eq!(dice1.len(), 210); + + let mut result = 0; + + for d1 in dice1.iter() { + for d2 in dice2.iter() { + if is_valid_dice_configuration(d1, d2) { + result += 1; + } + } + } + + println!("Number of valid configurations of dice is {}", result); println!("Time passed: {:?}", Instant::now() - now); }