day 7!
This commit is contained in:
parent
15109541b5
commit
6e00934819
|
@ -0,0 +1,99 @@
|
|||
io.input("input")
|
||||
|
||||
function handtype(hand, is_part2)
|
||||
cards = {}
|
||||
repeats = {}
|
||||
for c in hand:gmatch("[%d%a]") do
|
||||
repeats[c] = repeats[c] and repeats[c]+1 or 1
|
||||
table.insert(cards, c)
|
||||
end
|
||||
njokers = repeats["J"]
|
||||
lenrep = 0
|
||||
for k,v in pairs(repeats) do
|
||||
lenrep = lenrep + 1
|
||||
end
|
||||
if lenrep==1 then --five of a kind
|
||||
return 7 -- five of a kind
|
||||
end
|
||||
if lenrep==2 then --four of a kind or full house
|
||||
if is_part2 and njokers then return 7 end --five of a kind
|
||||
if repeats[cards[1]]==4 or repeats[cards[2]]==4 then
|
||||
return 6 --four of a kind
|
||||
else
|
||||
return 5 --full house
|
||||
end
|
||||
end
|
||||
if lenrep==3 then --three of a kind or two pair
|
||||
if repeats[cards[1]]==3 or repeats[cards[2]]==3 or repeats[cards[3]]==3 then
|
||||
if is_part2 and njokers then return 6 end -- four of a kind
|
||||
return 4 -- three of a kind
|
||||
else
|
||||
if is_part2 and njokers then
|
||||
if njokers==1 then return 5 --full house (!)
|
||||
else return 6 --four of a kind
|
||||
end
|
||||
end
|
||||
return 3 -- two pair
|
||||
end
|
||||
end
|
||||
if lenrep==4 then -- one pair
|
||||
if is_part2 and njokers then return 4 end -- three of a kind
|
||||
return 2 -- one pair
|
||||
end
|
||||
if lenrep==5 then -- high card
|
||||
if is_part2 and njokers then return 2 end -- one pair
|
||||
return 1 -- high card
|
||||
end
|
||||
end
|
||||
|
||||
cardvalues={A=13, K=12, Q=11, J=10, T=9, ["9"]=8, ["8"]=7, ["7"]=6, ["6"]=5, ["5"]=4, ["4"]=3, ["3"]=2, ["2"]=1}
|
||||
cardvalues2={A=13, K=12, Q=11, J=0, T=9, ["9"]=8, ["8"]=7, ["7"]=6, ["6"]=5, ["5"]=4, ["4"]=3, ["3"]=2, ["2"]=1}
|
||||
function sorthands(a, b)
|
||||
ta, tb = handtype(a.hand, is_part2), handtype(b.hand, is_part2)
|
||||
if ta < tb then
|
||||
return true
|
||||
elseif ta==tb then
|
||||
for i=1,5 do
|
||||
ca, cb = a.hand:sub(i,i), b.hand:sub(i,i)
|
||||
if is_part2 then
|
||||
va, vb = cardvalues2[ca], cardvalues2[cb]
|
||||
else
|
||||
va, vb = cardvalues[ca], cardvalues[cb]
|
||||
end
|
||||
if va==vb then goto continue
|
||||
else return va<vb end
|
||||
|
||||
::continue::
|
||||
end
|
||||
return false
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
set = {}
|
||||
for line in io.lines() do
|
||||
h, b = line:match("^([%d%a]+) (%d+)$")
|
||||
b = tonumber(b)
|
||||
table.insert(set, {hand=h, bid=b})
|
||||
end
|
||||
|
||||
|
||||
table.sort(set, sorthands)
|
||||
sum1 = 0
|
||||
for i,s in ipairs(set) do
|
||||
hand, bid = s.hand, s.bid
|
||||
sum1 = sum1 + i*bid
|
||||
-- print(i, bid, hand, handtype(hand))
|
||||
end
|
||||
print("part 1", sum1)
|
||||
|
||||
is_part2 = true
|
||||
table.sort(set, sorthands)
|
||||
sum2 = 0
|
||||
for i,s in ipairs(set) do
|
||||
hand, bid = s.hand, s.bid
|
||||
sum2 = sum2 + i*bid
|
||||
-- print(i, bid, hand, handtype(hand, is_part2))
|
||||
end
|
||||
print("part 2", sum2)
|
|
@ -0,0 +1,5 @@
|
|||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
Loading…
Reference in New Issue