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


// version 1.1.3
 
import java.util.Collections.shuffle
 
enum class Color   { RED, GREEN, PURPLE }
enum class Symbol  { OVAL, SQUIGGLE, DIAMOND }
enum class Number  { ONE, TWO, THREE }
enum class Shading { SOLID, OPEN, STRIPED }
enum class Degree  { BASIC, ADVANCED }
 
class Card(
    val color:   Color,
    val symbol:  Symbol,
    val number:  Number,
    val shading: Shading
) : Comparable<Card> {
 
    private val value = 
        color.ordinal * 27 + symbol.ordinal * 9 + number.ordinal * 3  + shading.ordinal
 
    override fun compareTo(other: Card) = value.compareTo(other.value)
 
    override fun toString() = (
        color.name.padEnd(8)   +
        symbol.name.padEnd(10) +
        number.name.padEnd(7)  + 
        shading.name.padEnd(7)
    ).toLowerCase()
 
    companion object {
        val zero = Card(Color.RED, Symbol.OVAL, Number.ONE, Shading.SOLID)
    }
}
 
fun createDeck() = 
    List<Card>(81) {
        val col = Color.values()  [it / 27]
        val sym = Symbol.values() [it / 9 % 3]
        val num = Number.values() [it / 3 % 3]
        val shd = Shading.values()[it % 3] 
        Card(col, sym, num, shd)
    }
 
fun playGame(degree: Degree) {
    val deck = createDeck()
    val nCards = if (degree == Degree.BASIC) 9 else 12
    val nSets = nCards / 2
    val sets = Array(nSets) { Array(3) { Card.zero } }
    var hand: Array<Card>
    outer@ while (true) {
        shuffle(deck)
        hand = deck.take(nCards).toTypedArray()
        var count = 0  
        for (i in 0 until hand.size - 2) {
            for (j in i + 1 until hand.size - 1) {
                for (k in j + 1 until hand.size) {
                    val trio = arrayOf(hand[i], hand[j], hand[k]) 
                    if (isSet(trio)) {
                        sets[count++] = trio
                        if (count == nSets) break@outer
                    }
                }
            }
        }
    }
    hand.sort()
    println("DEALT $nCards CARDS:\n")
    println(hand.joinToString("\n"))
    println("\nCONTAINING $nSets SETS:\n")
    for (s in sets) {
        s.sort()
        println(s.joinToString("\n"))
        println()
    }  
}
 
fun isSet(trio: Array<Card>): Boolean {
    val r1 = trio.sumBy { it.color.ordinal   } % 3
    val r2 = trio.sumBy { it.symbol.ordinal  } % 3
    val r3 = trio.sumBy { it.number.ordinal  } % 3
    val r4 = trio.sumBy { it.shading.ordinal } % 3
    return (r1 + r2 + r3 + r4) == 0
}
 
fun main(args: Array<String>) {   
    playGame(Degree.BASIC)
    println()
    playGame(Degree.ADVANCED)
}