I could just link my solution for this Advent of Code day. But I wouldn't want 
to spoil part 2 for you, so here is the snippet:
    
    
    type
      Card = enum
        C2 = (2, "2"), C3 = "3", C4 = "4", C5 = "5", C6 = "6", C7 = "7", C8 = 
"8", C9 = "9",
        T, J, Q, K, A
      Hand = array[5, Card]
    
    
    Run

Enum values have a string and ordinal value associated to them. Here I am 
assigning string "2" and value 2 to enum C2. `C2 = (2, "2")`

Notice how I only assign value to first item, because every item after it will 
have value 1 higher than previous. E3 = 3, E4 = 4, etc. etc.

Also notice that I didn't change string values of T, J, K, A because by default 
string value of item is his name T = "T", J = "J", etc. etc.

> But it's going to require a lot of boilerplate to convert

Not really. Later in the code I parse input for hand string with a for loop and 
parseEnum():
    
    
    func parseHand(hand: string): Hand =
      for i, c in hand:
        result[i] = parseEnum[Card]($c)
    
    
    Run

And making a set of cards from array[5, Card] is just as easy as with strings:
    
    
    import std/setutils
    let setOfCards = [T, C8, K, Q, C5].toSet()
    
    
    Run

> Also in an hypothetical where I have more than 5 cards, for example to 
> represent a 52 cards deck...

Then you could just change `array[5, Card]` to `array[52, Card]`.

If you need more context you can read full solution 
[here](https://codeberg.org/Archargelod/aoc23-nim/src/branch/master/day_07/solution.nim)
 .

Reply via email to