Skip to content

Commit 4bebe6e

Browse files
completed day 7
1 parent 5a020c2 commit 4bebe6e

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

day7.lua

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
CardValue = {
2+
["A"]=13,
3+
["K"]=12,
4+
["Q"]=11,
5+
["T"]=9,
6+
["9"]=8,
7+
["8"]=7,
8+
["7"]=6,
9+
["6"]=5,
10+
["5"]=4,
11+
["4"]=3,
12+
["3"]=2,
13+
["2"]=1,
14+
["J"]=0,
15+
}
16+
17+
HandTypeValue = {
18+
["five of a kind"] = 6,
19+
["four of a kind"] = 5,
20+
["full house"] = 4,
21+
["three of a kind"] = 3,
22+
["two pair"] = 2,
23+
["one pair"] = 1,
24+
["high card"] = 0,
25+
}
26+
27+
function GetHandType(hand)
28+
local card_counts = {}
29+
for k, _ in pairs(CardValue) do
30+
card_counts[k] = 0
31+
end
32+
33+
for i=1,#hand do
34+
local card = string.sub(hand, i, i)
35+
card_counts[card] = card_counts[card] + 1
36+
end
37+
38+
local max_counts = {0, 0}
39+
for card, count in pairs(card_counts) do
40+
if card ~= "J" then
41+
if count >= max_counts[1] then
42+
max_counts[2] = max_counts[1]
43+
max_counts[1] = count
44+
elseif count > max_counts[2] then
45+
max_counts[2] = count
46+
end
47+
end
48+
end
49+
50+
if card_counts["J"] == 5 or card_counts["J"] == 4 then
51+
return "five of a kind"
52+
end
53+
54+
if max_counts[1] == 5 then
55+
return "five of a kind"
56+
elseif max_counts[1] == 4 then
57+
if card_counts["J"] == 1 then
58+
return "five of a kind"
59+
end
60+
return "four of a kind"
61+
elseif max_counts[1] == 3 then
62+
if card_counts["J"] == 2 then
63+
return "five of a kind"
64+
elseif card_counts["J"] == 1 then
65+
return "four of a kind"
66+
elseif max_counts[2] == 2 then
67+
return "full house"
68+
else
69+
return "three of a kind"
70+
end
71+
elseif max_counts[1] == 2 then
72+
if card_counts["J"] == 3 then
73+
return "five of a kind"
74+
elseif card_counts["J"] == 2 then
75+
return "four of a kind"
76+
elseif card_counts["J"] == 1 then
77+
if max_counts[2] == 2 then
78+
return "full house"
79+
else
80+
return "three of a kind"
81+
end
82+
elseif max_counts[2] == 2 then
83+
return "two pair"
84+
else
85+
return "one pair"
86+
end
87+
end
88+
if card_counts["J"] == 3 then
89+
return "four of a kind"
90+
elseif card_counts["J"] == 2 then
91+
return "three of a kind"
92+
elseif card_counts["J"] == 1 then
93+
return "one pair"
94+
end
95+
return "high card"
96+
end
97+
98+
function EmbedHands(hands)
99+
local embedded_hands = {}
100+
for i, hand in pairs(hands) do
101+
local hand_type_value = HandTypeValue[GetHandType(hand.cards)]
102+
local card_values = {}
103+
for value_i=1,5 do
104+
card_values[value_i] = CardValue[string.sub(hand.cards, value_i, value_i)]
105+
end
106+
local hand_embedding =
107+
(hand_type_value << 20) |
108+
(card_values[1] << 16) |
109+
(card_values[2] << 12) |
110+
(card_values[3] << 8) |
111+
(card_values[4] << 4) |
112+
card_values[5]
113+
embedded_hands[i] = {cards=hand.cards, value=hand_embedding, bid=hand.bid}
114+
end
115+
return embedded_hands
116+
end
117+
118+
function ParseHands()
119+
local hands = {}
120+
local line = io.read("*l")
121+
while line ~= nil do
122+
local cards = string.match(line, "(.+) ")
123+
local bid = tonumber(string.match(line, " (%d+)"))
124+
hands[#hands+1] = {cards=cards, bid=bid}
125+
line = io.read("*l")
126+
end
127+
return hands
128+
end
129+
130+
function CompareHands(a, b)
131+
return a.value < b.value
132+
end
133+
134+
io.input("day7.in")
135+
local hands = ParseHands()
136+
local embedded_hands = EmbedHands(hands)
137+
table.sort(embedded_hands, CompareHands)
138+
local total = 0
139+
local subtotal = 0
140+
for i, hand in pairs(embedded_hands) do
141+
if string.match(hand.cards, "J") then
142+
print(i, hand.cards, hand.bid, hand.value, GetHandType(hand.cards))
143+
end
144+
total = total + hand.bid * i
145+
end
146+
147+
-- 249748283
148+
-- 248457642 (too high)
149+
-- 248581739 (too high)
150+
-- 248457642 (too high)
151+
-- 248029057
152+
print(subtotal)
153+
print(total)

0 commit comments

Comments
 (0)