refactor: 2023 Day 7
This commit is contained in:
parent
8c18dcda69
commit
d13d30dd57
1 changed files with 37 additions and 36 deletions
|
|
@ -36,15 +36,16 @@ func parseHand(text: string): Hand =
|
||||||
func parseHands(text: string): seq[Hand] =
|
func parseHands(text: string): seq[Hand] =
|
||||||
text.strip.splitLines.map(parseHand)
|
text.strip.splitLines.map(parseHand)
|
||||||
|
|
||||||
const five = 7
|
type Strength {.pure.} = enum
|
||||||
const four = 6
|
High
|
||||||
const full = 5
|
Pair
|
||||||
const three = 4
|
TwoPair
|
||||||
const twoPair = 3
|
Three
|
||||||
const pair = 2
|
Full
|
||||||
const high = 1
|
Four
|
||||||
|
Five
|
||||||
|
|
||||||
func strength(hand: Hand): int =
|
func strength(hand: Hand): Strength =
|
||||||
let count = newCountTable(hand.cards)
|
let count = newCountTable(hand.cards)
|
||||||
|
|
||||||
var counts: CountTable[int]
|
var counts: CountTable[int]
|
||||||
|
|
@ -52,19 +53,19 @@ func strength(hand: Hand): int =
|
||||||
counts.inc v
|
counts.inc v
|
||||||
|
|
||||||
if counts[5] > 0:
|
if counts[5] > 0:
|
||||||
return five
|
return Five
|
||||||
if counts[4] > 0:
|
if counts[4] > 0:
|
||||||
return four
|
return Four
|
||||||
if counts[3] > 0:
|
if counts[3] > 0:
|
||||||
if counts[2] > 0:
|
if counts[2] > 0:
|
||||||
return full
|
return Full
|
||||||
else:
|
else:
|
||||||
return three
|
return Three
|
||||||
if counts[2] == 2:
|
if counts[2] == 2:
|
||||||
return twoPair
|
return TwoPair
|
||||||
if counts[2] == 1:
|
if counts[2] == 1:
|
||||||
return pair
|
return Pair
|
||||||
return high
|
return High
|
||||||
|
|
||||||
func cmpHand(a: Hand, b: Hand): int =
|
func cmpHand(a: Hand, b: Hand): int =
|
||||||
let strength = cmp(a.strength, b.strength)
|
let strength = cmp(a.strength, b.strength)
|
||||||
|
|
@ -87,7 +88,7 @@ proc part1(): int =
|
||||||
for (rank, hand) in enumerate(hands):
|
for (rank, hand) in enumerate(hands):
|
||||||
result += (rank + 1) * hand.bid
|
result += (rank + 1) * hand.bid
|
||||||
|
|
||||||
func strengthJoker(hand: Hand): int =
|
func strengthJoker(hand: Hand): Strength =
|
||||||
let count = newCountTable(hand.cards)
|
let count = newCountTable(hand.cards)
|
||||||
|
|
||||||
let jokers = count[1]
|
let jokers = count[1]
|
||||||
|
|
@ -99,37 +100,37 @@ func strengthJoker(hand: Hand): int =
|
||||||
regular.inc count
|
regular.inc count
|
||||||
|
|
||||||
if regular[5] > 0:
|
if regular[5] > 0:
|
||||||
return five
|
return Five
|
||||||
if regular[4] > 0:
|
if regular[4] > 0:
|
||||||
if jokers == 1:
|
if jokers == 1:
|
||||||
return five
|
return Five
|
||||||
return four
|
return Four
|
||||||
if regular[3] > 0:
|
if regular[3] > 0:
|
||||||
case jokers:
|
case jokers:
|
||||||
of 2: return five
|
of 2: return Five
|
||||||
of 1: return four
|
of 1: return Four
|
||||||
else: discard
|
else: discard
|
||||||
if regular[2] > 0:
|
if regular[2] > 0:
|
||||||
return full
|
return Full
|
||||||
return three
|
return Three
|
||||||
if regular[2] == 2:
|
if regular[2] == 2:
|
||||||
if jokers == 1:
|
if jokers == 1:
|
||||||
return full
|
return Full
|
||||||
return twoPair
|
return TwoPair
|
||||||
if regular[2] == 1:
|
if regular[2] == 1:
|
||||||
case jokers:
|
case jokers:
|
||||||
of 3: return five
|
of 3: return Five
|
||||||
of 2: return four
|
of 2: return Four
|
||||||
of 1: return three
|
of 1: return Three
|
||||||
else: return pair
|
else: return Pair
|
||||||
|
|
||||||
case jokers:
|
case jokers:
|
||||||
of 5: return five
|
of 5: return Five
|
||||||
of 4: return five
|
of 4: return Five
|
||||||
of 3: return four
|
of 3: return Four
|
||||||
of 2: return three
|
of 2: return Three
|
||||||
of 1: return pair
|
of 1: return Pair
|
||||||
else: return high
|
else: return High
|
||||||
|
|
||||||
func cmpHandJoker(a: Hand, b: Hand): int =
|
func cmpHandJoker(a: Hand, b: Hand): int =
|
||||||
let strength = cmp(a.strengthJoker, b.strengthJoker)
|
let strength = cmp(a.strengthJoker, b.strengthJoker)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue