Added day 7 solutions
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
Part 1: `247815719`
|
||||
|
||||
Part 2: `248747492`
|
||||
|
||||
[--- Day 7: Camel Cards ---
|
||||
--------------------------
|
||||
|
||||
@@ -79,4 +83,40 @@ Now, you can determine the total winnings of this set of hands by adding up the
|
||||
Find the rank of every hand in your set. *What are the total winnings?*
|
||||
|
||||
|
||||
, --- Part Two ---
|
||||
----------------
|
||||
|
||||
To make things a little more interesting, the Elf introduces one additional rule. Now, `J` cards are [jokers](https://en.wikipedia.org/wiki/Joker_(playing_card)) - wildcards that can act like whatever card would make the hand the strongest type possible.
|
||||
|
||||
|
||||
To balance this, *`J` cards are now the weakest* individual cards, weaker even than `2`. The other cards stay in the same order: `A`, `K`, `Q`, `T`, `9`, `8`, `7`, `6`, `5`, `4`, `3`, `2`, `J`.
|
||||
|
||||
|
||||
`J` cards can pretend to be whatever card is best for the purpose of determining hand type; for example, `QJJQ2` is now considered *four of a kind*. However, for the purpose of breaking ties between two hands of the same type, `J` is always treated as `J`, not the card it's pretending to be: `JKKK2` is weaker than `QQQQ2` because `J` is weaker than `Q`.
|
||||
|
||||
|
||||
Now, the above example goes very differently:
|
||||
|
||||
|
||||
|
||||
```
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
||||
|
||||
```
|
||||
|
||||
* `32T3K` is still the only *one pair*; it doesn't contain any jokers, so its strength doesn't increase.
|
||||
* `KK677` is now the only *two pair*, making it the second-weakest hand.
|
||||
* `T55J5`, `KTJJT`, and `QQQJA` are now all *four of a kind*! `T55J5` gets rank 3, `QQQJA` gets rank 4, and `KTJJT` gets rank 5.
|
||||
|
||||
|
||||
With the new joker rule, the total winnings in this example are `*5905*`.
|
||||
|
||||
|
||||
Using the new joker rule, find the rank of every hand in your set. *What are the new total winnings?*
|
||||
|
||||
|
||||
]
|
||||
@@ -0,0 +1,69 @@
|
||||
with open("../input.txt", "r") as fp:
|
||||
input = fp.read().strip().split("\n")
|
||||
|
||||
ranks = [[],[],[],[],[],[],[]]
|
||||
strengths = "AKQJT98765432"[::-1]
|
||||
|
||||
def insertRank(hand, t):
|
||||
pos = -1
|
||||
if not ranks[t]:
|
||||
ranks[t].insert(pos, hand)
|
||||
return
|
||||
|
||||
for p, player in enumerate(ranks[t]):
|
||||
player = player[0]
|
||||
|
||||
for c, card in enumerate(hand[0]):
|
||||
pCard = strengths.find(card)
|
||||
rCard = strengths.find(player[c])
|
||||
|
||||
if pCard < rCard:
|
||||
ranks[t].insert(p, hand)
|
||||
return
|
||||
elif pCard > rCard and p+1 == len(ranks[t]):
|
||||
ranks[t].insert(p+1, hand)
|
||||
return
|
||||
elif pCard > rCard:
|
||||
break
|
||||
|
||||
for player in input:
|
||||
cards = player.split()[0]
|
||||
bet = int(player.split()[-1])
|
||||
|
||||
c = set([cards.count(x) for x in cards])
|
||||
counts = len(''.join(set(cards)))
|
||||
|
||||
if counts == 5:
|
||||
# High Card
|
||||
insertRank((cards,bet), 6)
|
||||
elif counts == 4:
|
||||
# One Pair
|
||||
insertRank((cards,bet), 5)
|
||||
elif counts == 3:
|
||||
# Two Pair: 22334
|
||||
c = set([cards.count(x) for x in cards])
|
||||
if 2 in c:
|
||||
insertRank((cards,bet), 4)
|
||||
else:
|
||||
# Three of a kind: 22234
|
||||
insertRank((cards,bet), 3)
|
||||
elif counts == 2:
|
||||
# Full House: 23332
|
||||
c = set([cards.count(x) for x in cards])
|
||||
if 2 in c:
|
||||
insertRank((cards,bet), 2)
|
||||
else:
|
||||
# Four of a kind: 22223
|
||||
insertRank((cards,bet), 1)
|
||||
elif counts == 1:
|
||||
# Five of a kind
|
||||
insertRank((cards,bet), 0)
|
||||
|
||||
sum = 0
|
||||
i = 1
|
||||
for r in reversed(ranks):
|
||||
for cards, bet in r:
|
||||
sum += (bet*i)
|
||||
i += 1
|
||||
|
||||
print(sum)
|
||||
@@ -0,0 +1,71 @@
|
||||
with open("../input.txt", "r") as fp:
|
||||
input = fp.read().strip().split("\n")
|
||||
|
||||
ranks = [[],[],[],[],[],[],[]]
|
||||
strengths = "AKQT98765432J"[::-1]
|
||||
|
||||
def insertRank(hand, t):
|
||||
pos = -1
|
||||
if not ranks[t]:
|
||||
ranks[t].insert(pos, hand)
|
||||
return
|
||||
|
||||
for p, player in enumerate(ranks[t]):
|
||||
player = player[0]
|
||||
|
||||
for c, card in enumerate(hand[0]):
|
||||
pCard = strengths.find(card)
|
||||
rCard = strengths.find(player[c])
|
||||
|
||||
if pCard < rCard:
|
||||
ranks[t].insert(p, hand)
|
||||
return
|
||||
elif pCard > rCard and p+1 == len(ranks[t]):
|
||||
ranks[t].insert(p+1, hand)
|
||||
return
|
||||
elif pCard > rCard:
|
||||
break
|
||||
|
||||
for player in input:
|
||||
cards = player.split()[0]
|
||||
bet = int(player.split()[-1])
|
||||
|
||||
c = set([cards.count(x) for x in cards])
|
||||
counts = len(''.join(set(cards)))
|
||||
jokers = 0 if (cards.count('J')==-1) else cards.count('J')
|
||||
|
||||
if counts == 5 and jokers == 0:
|
||||
# High Card
|
||||
insertRank((cards,bet), 6)
|
||||
elif counts == 4 and jokers == 0 or (counts == 5 and jokers >= 1):
|
||||
# One Pair
|
||||
insertRank((cards,bet), 5)
|
||||
# ?? VV ??
|
||||
elif counts == 3 and jokers == 0 or (counts == 4 and jokers >= 1):
|
||||
# Two Pair: 22334
|
||||
c = set([cards.count(x) for x in cards])
|
||||
if 2 in c and jokers == 0:
|
||||
insertRank((cards,bet), 4)
|
||||
else:
|
||||
# Three of a kind: 22234
|
||||
insertRank((cards,bet), 3)
|
||||
elif counts == 2 and jokers == 0 or (counts == 3 and jokers >= 1):
|
||||
# Full House: 23332
|
||||
c = set([cards.count(x) for x in cards])
|
||||
if 2 in c and jokers == 0 or (2 in c and jokers == 1):
|
||||
insertRank((cards,bet), 2)
|
||||
else:
|
||||
# Four of a kind: 22223
|
||||
insertRank((cards,bet), 1)
|
||||
elif counts == 1 and (jokers == 0 or jokers == 5) or (counts == 2 and jokers >= 1):
|
||||
# Five of a kind
|
||||
insertRank((cards,bet), 0)
|
||||
|
||||
sum = 0
|
||||
i = 1
|
||||
for r in reversed(ranks):
|
||||
for cards, bet in r:
|
||||
sum += (bet*i)
|
||||
i += 1
|
||||
|
||||
print(sum)
|
||||
Reference in New Issue
Block a user