ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use itertools::Itertools;

fn challenge_1() {
    /*
        Vilka siffror döljer sig bakom bokstäverna?

                G O D
              × J U L
              -------
                N O N
            T O D O
        + T G O J
        -------------
          T O M T E N

        Varje bokstav motsvarar en unik siffra.
    */

    // Define the letters used in the puzzle
    let letters = ['G', 'O', 'D', 'J', 'U', 'L', 'N', 'T', 'M', 'E'];

    // Generate all possible solutions
    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();

        // Check if the current permutation is a valid solution
        if is_valid_solution(&mapping) {
            // If it's a valid solution, print it
            for (letter, digit) in &mapping {
                println!("{} = {}", letter, digit);
            }
            break;
        }
    }

    fn is_valid_solution(mapping: &[(char, u32)]) -> bool {
        // Check if the mapping is valid
        // 1. The mapping must be 10 characters long
        if mapping.len() != 10 {
            return false;
        }

        // 2. The mapping must contain all letters
        let mut letters = mapping.iter().map(|(letter, _)| letter).collect::<Vec<_>>();
        letters.sort();
        letters.dedup();
        if letters.len() != 10 {
            return false;
        }

        // 3. The mapping must contain all digits
        let mut digits = mapping.iter().map(|(_, digit)| digit).collect::<Vec<_>>();
        digits.sort();
        digits.dedup();

        if digits.len() != 10 {
            return false;
        }

        // 4. The mapping must be a valid solution to the puzzle
        // Create a helper function to convert a letter to its corresponding digit
        let letter_to_digit = |letter: char| -> Option<u32> {
            mapping
                .iter()
                .find(|(l, _)| *l == letter)
                .map(|&(_, digit)| digit)
        };

        // Assign digits to each letter
        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");

        // Form the numbers from the letters
        let god = g * 100 + o * 10 + d;
        let jul = j * 100 + u * 10 + l;
        let product = god * jul;

        // Split the product into its constituent parts (N O N, T O D O, T G O J)
        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;

        // Check the multiplication step
        if non + todo * 10 + tgoj * 100 != product {
            return false;
        }

        // Since TOMTEN is the sum of the products, split it into its digits
        let tomten = t * 100000 + o * 10000 + m * 1000 + t * 100 + e * 10 + n;

        // Check if TOMTEN matches the product
        if tomten != product {
            return false;
        }

        true
    }
}

fn challange_2() {
    /*
        TOÅÅH MZVDOÅHTO RSHNN
        XV EHVH OÅ NHNNOVDSHNN.
        TOÄ UHÅTSHV IL RVPNÄOVBÅG
        DIL MZVDXÄÄOV OKO B MYÅTOVBÅG.
        IL AYSDÄZROÄ XV OÅ NXVD
        RHÅ TY HSSÄBT EODÄXLLH OÅ LYSÄBNSBRHÄBK BÅKOVD.
        TOÄ GWV BÅÄO HÄÄ NW AYSHMÄIÅ VBLLH
        OMÄOVDIL THÄYLOÄ OA XV VOSHÄBKÄ NVBLH.
        ÅY DRH KB XÄH GVZÄ GAIVT HK VBD
        IJU RHÅDRO RVPNÄOVH LOT UAXSN HK OÅ LHÄVBD.
        SWÄ GSZGGOÅ DKHSÅH IL TOÅ EVXÅÅD
        LOTHÅ TY EOVXRÅHV OÅ AIEEBG RIÅGVYOÅD.
        RÅXJR ÅWGVH UWVTH ÅZÄÄOV
        DW XV TY VYDÄHT HÄÄ VXRÅH LOT NVBLBÄBKH VZÄÄOV.
        OÄÄ ÅPÄÄ WV DRH DÅHVÄ MBVHD BÅ LOT GSHL
        IJU TOÅÅH ÄOFÄ XV BÅÄO SXÅGVO OÄÄ RVPNÄIGVHL.
        DO ÅY ÄBSS HÄÄ VI TOÄ B UHLÅ,
        DW HÄÄ AHG RHÅ DRVBKH G KBT TBÄÄ ÅHLÅ.
        AHG ZÅDRHV TBG OÅ GIT AYS
        IJU UINNHD HÄÄ TY ÄPJROV RYVDOÅ KHVBÄ RYS.
    */
}

pub fn run_challanges() {
    challenge_1();
}