head(txt, n = 12L)
[1] "9A35J 469" "75T32 237" "6T8JQ 427" "3366A 814" "K2AK9 982" "J8KTT 9"
[7] "94936 970" "Q8AK9 15" "3QQ32 940" "65555 484" "8K88K 674" "Q67T5 788"
Rank hands of cards and get the total winnings (bid * rank).
head(txt, n = 12L)
[1] "9A35J 469" "75T32 237" "6T8JQ 427" "3366A 814" "K2AK9 982" "J8KTT 9"
[7] "94936 970" "Q8AK9 15" "3QQ32 940" "65555 484" "8K88K 674" "Q67T5 788"
= c(
types "11111" = "1_highcard"
"1112" = "2_onepair"
, "122" = "3_twopair"
, "113" = "4_threeofakind"
, "23" = "5_fullhouse"
, "14" = "6_fourofakind"
, "5" = "7_fiveofakind"
,
)
= c(
values "A" = 14
"K" = 13
, "Q" = 12
, "J" = 11
, "T" = 10
, "9" = 9
, "8" = 8
, "7" = 7
, "6" = 6
, "5" = 5
, "4" = 4
, "3" = 3
, "2" = 2
,
)
= function(x){
handtable table(x) |>
sort() |>
paste(collapse = "")
}
= function(x, typ = types){
handtype
= strsplit(x, split = "")
h = Map(
h
handtablex = h
, |>
) unlist()
typ[match(
htable = names(typ)
,
)|>
] unname()
}
= function(x, val = values){
secondorder
= strsplit(x, split = "")
h = lapply(
h
hFUN = \(i) {
, unname(
val[match(
itable = names(val)
,
)
]
)
}
)
lapply(
hFUN = \(i) {
, as.numeric(
/ 10) %*% c(100000000, 1000000, 10000, 100, 1)
(i
)
}|>
) unlist()
}
= data.table(
data txt = txt
)
:= tstrsplit(txt, split = " ", keep = 1L)]
data[, hand := tstrsplit(txt, split = " ", keep = 2L)]
data[, score := as.integer(score)]
data[, score := handtype(hand)]
data[, type := secondorder(hand)]
data[, sorder
setorder(data, type, sorder)
:= .I]
data[, rank := rank * score]
data[, bid sum(data$bid)
[1] 250120186
head(txt, n = 12L)
[1] "9A35J 469" "75T32 237" "6T8JQ 427" "3366A 814" "K2AK9 982" "J8KTT 9"
[7] "94936 970" "Q8AK9 15" "3QQ32 940" "65555 484" "8K88K 674" "Q67T5 788"