Set Puzzles are created with a deck of cards from the Set Game™. The object of the puzzle is to find sets of 3 cards in a rectangle of cards that have been dealt face up. 


There are 81 cards in a deck.
Each card contains a unique variation of the following four features: color, symbol, number and shading.

 there are three colors:
    red, green, purple

 there are three symbols:
    oval, squiggle, diamond

 there is a number of symbols on the card:
    one, two, three

 there are three shadings:
    solid, open, striped

Three cards form a set if each feature is either the same on each card, or is different on each card. For instance: all 3 cards are red, all 3 cards have a different symbol, all 3 cards have a different number of symbols, all 3 cards are striped.

There are two degrees of difficulty: basic and advanced. The basic mode deals 9 cards, that contain exactly 4 sets; the advanced mode deals 12 cards that contain exactly 6 sets.

When creating sets you may use the same card more than once.




Task
Write code that deals the cards (9 or 12, depending on selected mode) from a shuffled deck in which the total number of sets that could be found is 4 (or 6, respectively); and print the contents of the cards and the sets.

For instance:



DEALT 9 CARDS:

green, one, oval, striped
green, one, diamond, open
green, one, diamond, striped
green, one, diamond, solid
purple, one, diamond, open
purple, two, squiggle, open
purple, three, oval, open
red, three, oval, open
red, three, diamond, solid

CONTAINING 4 SETS:

green, one, oval, striped
purple, two, squiggle, open
red, three, diamond, solid


green, one, diamond, open
green, one, diamond, striped
green, one, diamond, solid


green, one, diamond, open
purple, two, squiggle, open
red, three, oval, open


purple, one, diamond, open
purple, two, squiggle, open
purple, three, oval, open


 
use itertools::Itertools;
use rand::Rng;
 
const DECK_SIZE: usize = 81;
const NUM_ATTRIBUTES: usize = 4;
const ATTRIBUTES: [&[&str]; NUM_ATTRIBUTES] = [
    &["red", "green", "purple"],
    &["one", "two", "three"],
    &["oval", "squiggle", "diamond"],
    &["solid", "open", "striped"],
];
 
fn get_random_card_indexes(num_of_cards: usize) -> Vec<usize> {
    let mut selected_cards: Vec<usize> = Vec::with_capacity(num_of_cards);
    let mut rng = rand::thread_rng();
    loop {
        let idx = rng.gen_range(0..DECK_SIZE);
        if !selected_cards.contains(&idx) {
            selected_cards.push(idx);
        }
        if selected_cards.len() == num_of_cards {
            break;
        }
    }
 
    selected_cards
}
 
fn run_game(num_of_cards: usize, minimum_number_of_sets: usize) {
    println!(
        "\nGAME: # of cards: {} # of sets: {}",
        num_of_cards, minimum_number_of_sets
    );
 
    // generate the deck with 81 unique cards
    let deck = (0..NUM_ATTRIBUTES)
        .map(|_| (0..=2_usize))
        .multi_cartesian_product()
        .collect::<Vec<_>>();
 
    // closure to return true if the three attributes are the same, or each of them is different
    let valid_attribute =
        |a: usize, b: usize, c: usize| -> bool { a == b && b == c || (a != b && b != c && a != c) };
 
    // closure to test all attributes, each of them should be true to have a valid set
    let valid_set = |t: &Vec<&Vec<usize>>| -> bool {
        for attr in 0..NUM_ATTRIBUTES {
            if !valid_attribute(t[0][attr], t[1][attr], t[2][attr]) {
                return false;
            }
        }
        true
    };
 
    loop {
        // select the required # of cards from the deck randomly
        let selected_cards = get_random_card_indexes(num_of_cards)
            .iter()
            .map(|idx| deck[*idx].clone())
            .collect::<Vec<_>>();
 
        // generate all combinations, and filter/keep only which are valid sets
        let valid_sets = selected_cards
            .iter()
            .combinations(3)
            .filter(|triplet| valid_set(triplet))
            .collect::<Vec<_>>();
 
        // if the # of the sets is matching the requirement, print it and finish
        if valid_sets.len() == minimum_number_of_sets {
            print!("SELECTED CARDS:");
            for card in &selected_cards {
                print!("\ncard: ");
                for attr in 0..NUM_ATTRIBUTES {
                    print!("{}, ", ATTRIBUTES[attr][card[attr]]);
                }
            }
 
            print!("\nSets:");
            for triplet in &valid_sets {
                print!("\nSet: ");
                for card in triplet {
                    for attr in 0..NUM_ATTRIBUTES {
                        print!("{}, ", ATTRIBUTES[attr][card[attr]]);
                    }
                    print!(" | ");
                }
            }
 
            break;
        }
 
        //otherwise generate again
    }
}
fn main() {
    run_game(9, 4);
    run_game(12, 6);
}
 
 