day 07 part 2 finished. Works on example, but not on input

This commit is contained in:
2024-12-20 18:31:25 +01:00
parent db1bf32fee
commit df6fc0ca59
2 changed files with 39 additions and 33 deletions

View File

@ -3,7 +3,3 @@ T55J5 684
KK677 28 KK677 28
KTJJT 220 KTJJT 220
QQQJA 483 QQQJA 483
33332 100
2AAAA 100
77888 100
77788 100

View File

@ -14,13 +14,14 @@ import fs from 'node:fs'
*/ */
function init() { function init() {
const data = fs.readFileSync('example.txt', { encoding: 'utf-8' }, data => data) const data = fs.readFileSync('input.txt', { encoding: 'utf-8' }, data => data)
const games = data.split('\n') const games = data.split('\n')
const poker = new Poker() const poker = new Poker()
for (let game of games) { for (let game of games) {
poker.hand = game.split(' ')[0].split('') poker.hand = game.split(' ')[0].split('')
poker.bid = parseInt(game.split(' ')[1]) poker.bid = parseInt(game.split(' ')[1])
poker.jokerrule = true
poker.play() poker.play()
} }
poker.score() poker.score()
@ -32,20 +33,22 @@ class Poker {
this.ranking = [] this.ranking = []
this.bid = 0 this.bid = 0
this.results = 0 this.results = 0
this.jokerrule = true
this.order = [ this.order = [
{ name: 'A', value: 13 }, { name: 'A', value: 14 },
{ name: 'K', value: 12 }, { name: 'K', value: 13 },
{ name: 'Q', value: 11 }, { name: 'Q', value: 12 },
{ name: 'J', value: 10 }, { name: 'J', value: this.jokerrule ? 1 : 11 },
{ name: 'T', value: 9 }, { name: 'T', value: 10 },
{ name: '9', value: 8 }, { name: '8', value: 7 }, { name: '7', value: 6 }, { name: '6', value: 5 }, { name: '5', value: 4 }, { name: '4', value: 3 }, { name: '9', value: 9 }, { name: '8', value: 8 }, { name: '7', value: 7 }, { name: '6', value: 6 }, { name: '5', value: 5 }, { name: '4', value: 4 },
{ name: '3', value: 2 }, { name: '2', value: 1 } { name: '3', value: 3 }, { name: '2', value: 2 }
] ]
} }
getOccurences() { getOccurences() {
let j = this.jokerrule ? this.hasJokers() : 0
return this.hand.reduce((acc, curr) => { return this.hand.reduce((acc, curr) => {
return acc[`${curr}`] ? ++acc[curr] : acc[`${curr}`] = 1, acc return acc[`${curr}`] ? ++acc[curr] : acc[`${curr}`] = j + 1, acc
}, new Object()) }, new Object())
} }
@ -67,6 +70,14 @@ class Poker {
const result = this.getOccurences() const result = this.getOccurences()
return Object.values(result).indexOf(number) > -1 return Object.values(result).indexOf(number) > -1
} }
hasJokers() {
const result = this.hand.reduce((acc, curr) => {
return acc[`${curr}`] ? ++acc[curr] : acc[`${curr}`] = 1, acc
}, new Object())
return result.hasOwnProperty('J') ? result.J : 0
}
testResults() { testResults() {
return { return {
'FiveOfAKind': this.hasMultiple(5), 'FiveOfAKind': this.hasMultiple(5),
@ -74,7 +85,8 @@ class Poker {
'FullHouse': this.isFullHouse(), 'FullHouse': this.isFullHouse(),
'ThreeOfAKind': this.hasMultiple(3), 'ThreeOfAKind': this.hasMultiple(3),
'TwoPair': this.isTwoPair(), 'TwoPair': this.isTwoPair(),
'OnePair': this.hasMultiple(2) 'OnePair': this.hasMultiple(2),
'HasJokers': this.hasJokers()
} }
} }
rank() { rank() {
@ -82,26 +94,26 @@ class Poker {
const results = this.testResults() const results = this.testResults()
let rank = 0 let rank = 0
switch (true) { switch (true) {
case results.FiveOfAKind: case results.FiveOfAKind || results.HasJokers === 5:
rank = 1_000_000_000_000 rank = 7_000_000_000_000
return rank break
case results.FourOfAKind: case results.FourOfAKind:
rank = 900_000_000_000 rank = 6_000_000_000_000
return rank break
case results.FullHouse: case results.FullHouse:
rank = 800_000_000_000 rank = 5_000_000_000_000
return rank break
case results.ThreeOfAKind: case results.ThreeOfAKind:
rank = 700_000_000_000 rank = 4_000_000_000_000
return rank break
case results.TwoPair: case results.TwoPair:
rank = 600_000_000_000 rank = 3_000_000_000_000
return rank break
case results.OnePair: case results.OnePair:
rank = 500_000_000_000 rank = 2_000_000_000_000
return rank break
default: default:
rank = 400_000_000_000 rank = 1_000_000_000_000
break break
} }
return rank return rank
@ -111,11 +123,9 @@ class Poker {
const sorted_list = this.ranking.sort((a, b) => a.rank - b.rank) const sorted_list = this.ranking.sort((a, b) => a.rank - b.rank)
for (let i = 0; i < sorted_list.length; i++) { for (let i = 0; i < sorted_list.length; i++) {
this.results += (i + 1) * sorted_list[i].bid this.results += (i + 1) * sorted_list[i].bid
console.log(sorted_list[i].cards, `${i + 1} * ${sorted_list[i].bid} = ${(i + 1) * sorted_list[i].bid}`) console.log(sorted_list[i])
} }
console.log(sorted_list.length)
console.log(this.results) console.log(this.results)
} }
play() { play() {
let rank = this.rank() let rank = this.rank()
@ -134,7 +144,7 @@ class Poker {
let stringValues = cardValues.map(value => String(value).padStart(2, 0)) let stringValues = cardValues.map(value => String(value).padStart(2, 0))
rank += parseInt(stringValues.join("")) rank += parseInt(stringValues.join(""))
this.ranking.push({ rank: rank, bid: this.bid, cards: this.hand.join("") }) this.ranking.push({ rank: rank, bid: this.bid, cards: this.hand.join(""), jokers: this.hasJokers() })
} }
rankPosition(position, iterator) { rankPosition(position, iterator) {