use itertools::Itertools;
fn challenge_1() {
let letters = ['G', 'O', 'D', 'J', 'U', 'L', 'N', 'T', 'M', 'E'];
for perm in (0..10).permutations(letters.len()) {
let mapping: Vec<(char, u32)> = letters
.iter()
.cloned()
.zip(perm.into_iter().map(|i| i as u32))
.collect();
if is_valid_solution(&mapping) {
for (letter, digit) in &mapping {
println!("{} = {}", letter, digit);
}
break;
}
}
fn is_valid_solution(mapping: &[(char, u32)]) -> bool {
if mapping.len() != 10 {
return false;
}
let mut letters = mapping.iter().map(|(letter, _)| letter).collect::<Vec<_>>();
letters.sort();
letters.dedup();
if letters.len() != 10 {
return false;
}
let mut digits = mapping.iter().map(|(_, digit)| digit).collect::<Vec<_>>();
digits.sort();
digits.dedup();
if digits.len() != 10 {
return false;
}
let letter_to_digit = |letter: char| -> Option<u32> {
mapping
.iter()
.find(|(l, _)| *l == letter)
.map(|&(_, digit)| digit)
};
let g = letter_to_digit('G').expect("G is not mapped");
let o = letter_to_digit('O').expect("O is not mapped");
let d = letter_to_digit('D').expect("D is not mapped");
let j = letter_to_digit('J').expect("J is not mapped");
let u = letter_to_digit('U').expect("U is not mapped");
let l = letter_to_digit('L').expect("L is not mapped");
let n = letter_to_digit('N').expect("N is not mapped");
let t = letter_to_digit('T').expect("T is not mapped");
let m = letter_to_digit('M').expect("M is not mapped");
let e = letter_to_digit('E').expect("E is not mapped");
let god = g * 100 + o * 10 + d;
let jul = j * 100 + u * 10 + l;
let product = god * jul;
let non = n * 100 + o * 10 + n;
let todo = t * 1000 + o * 100 + d * 10 + o;
let tgoj = t * 1000 + g * 100 + o * 10 + j;
if non + todo * 10 + tgoj * 100 != product {
return false;
}
let tomten = t * 100000 + o * 10000 + m * 1000 + t * 100 + e * 10 + n;
if tomten != product {
return false;
}
true
}
}
fn challange_2() {
}
pub fn run_challanges() {
challenge_1();
}