Ëerste poging projecteuler 90
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user