@@ 1,21 1,22 @@
+local T, J, Q, K, A = string.byte('TJQKA', 1, 5)
local value = {
- ['2']=2, ['3']=3, ['4']=4, ['5']=5, ['6']=6, ['7']=7, ['8']=8, ['9']=9,
- T=10, J=11, Q=12, K=13, A=14,
+ [0x32]=2, [0x33]=3, [0x34]=4, [0x35]=5, [0x36]=6, [0x37]=7, [0x38]=8, [0x39]=9,
+ [T]=10, [J]=11, [Q]=12, [K]=13, [A]=14,
}
function calcscore(hand, joker)
- local score, seen, uniq, most = 0, {}, 0, 0
- for card in hand:gmatch('.') do
+ local score, count, uniq, most = 0, {}, 0, 0
+ for i = 1, #hand do
+ local card = string.byte(hand, i)
+ local num = (count[card] or 0) + 1
score = score << 4
- if joker and card == 'J' then
- joker = joker + 1
- else
- local num = (seen[card] or 0) + 1
- seen[card] = num
- score = score | value[card]
+ if card ~= J or not joker then
if num > most then most = num end
if num == 1 then uniq = uniq + 1 end
+ score = score | value[card]
end
+ count[card] = num
end
+ joker = joker and count[J]
if joker then
most = most + joker
if uniq == 0 then uniq = 1 end
@@ 27,7 28,7 @@ for line in io.lines() do
for hand, bid in line:gmatch('(%w+) (%d+)') do
bid = tonumber(bid)
table.insert(hands1, calcscore(hand) << 10 | bid)
- table.insert(hands2, calcscore(hand, 0) << 10 | bid)
+ table.insert(hands2, calcscore(hand, true) << 10 | bid)
end
end
table.sort(hands1)