Ëerste poging projecteuler 90

This commit is contained in:
2024-10-28 13:41:26 +01:00
parent a7d13bd73c
commit fb41ac47f1
3 changed files with 79 additions and 2 deletions

View File

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

View File

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

View File

@@ -1,10 +1,68 @@
use itertools::Itertools;
use std::time::Instant;
fn generate_cubes() -> Vec<Vec<u32>> {
(0..=9).combinations(6).collect()
}
fn is_valid_dice_configuration(d1: &Vec<u32>, d2: &Vec<u32>) -> 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);
}