Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- String extend [ asHexNumber [^Number readFrom: (ReadStream on: self) radix: 16] ]
- Collection extend [ at: idx inc: val [^self at: idx put: ((self at: idx ifAbsent:[0]) + val)] ]
- "
- | Mainline
- "
- input := stdin lines contents.
- maps := {
- {$A -> $E. $K -> $D. $Q -> $C. $J -> $B. $T -> $A}. " Part 1 "
- {$A -> $E. $K -> $D. $Q -> $C. $J -> $1. $T -> $A}. " Part 2 "
- }.
- (1 to: 2) do: [:part |
- hands := input collect: [:line |
- parts := line subStrings.
- (maps at: part) do: [:map |
- parts first replaceAll: map key with: map value
- ].
- " Count cards. "
- " One initialized to 0 so jokers always have something to increase "
- counts := Dictionary from: {$E -> 0}.
- parts first do: [:chr | counts at: chr inc: 1].
- " Extract wilds from dictionary, jokers removed "
- wilds := counts at: $1 ifAbsent: [0].
- counts removeKey: $1 ifAbsent: [].
- " Sort groups and add wilds to largest "
- groups := counts values sorted reverse.
- groups at: 1 inc: wilds.
- " Make string and fill to 6 characters with 0s "
- groups := (groups collect: #asString) join.
- groups := groups, ($0 * (6 - groups size)).
- {groups asNumber. parts first asHexNumber. parts second asNumber}
- ].
- hands sort: [ :a :b | (a first < b first)
- or: [(a first = b first) and: [a second < b second]] ].
- winnings := 0.
- hands keysAndValuesDo: [:i :hand | winnings := winnings + (i * hand third)].
- ('Part %1: %2' % {part. winnings}) displayNl.
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement