2023 day 7
This commit is contained in:
parent
64a4dc3cf9
commit
eec219eeda
7 changed files with 1570 additions and 0 deletions
125
2023/07/part1/main.go
Normal file
125
2023/07/part1/main.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bufio"
|
||||
"strings"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
reader := bufio.NewReader(os.Stdin);
|
||||
hands := [][2]string{}
|
||||
for {
|
||||
text, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
strs := strings.Split(strings.Trim(text, "\n"), " ")
|
||||
hands = append(hands, [2]string{strs[0], strs[1]});
|
||||
}
|
||||
slices.SortFunc(hands, func(a, b [2]string) int {
|
||||
return Wins(a[0], b[0])
|
||||
})
|
||||
total := 0
|
||||
for i, hand := range hands {
|
||||
rank := i+1
|
||||
bid, _ := strconv.Atoi(hand[1])
|
||||
println(hand[0], rank, bid)
|
||||
total += rank * bid
|
||||
}
|
||||
println(total)
|
||||
}
|
||||
|
||||
|
||||
func IsFiveOfAKind(hand string) (bool) {
|
||||
return hand[0] == hand[1] && hand[0] == hand[2] && hand[0] == hand[3] && hand[0] == hand[4];
|
||||
}
|
||||
|
||||
func IsFourOfAKind(hand string) (bool) {
|
||||
|
||||
cardMap := map[rune]int{}
|
||||
for _, card := range hand {
|
||||
cardMap[card] ++;
|
||||
if cardMap[card] == 4 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func HasTriple(hand string) (bool) {
|
||||
|
||||
cardMap := map[rune]int{}
|
||||
for _, card := range hand {
|
||||
cardMap[card] ++;
|
||||
}
|
||||
for _,value := range cardMap {
|
||||
if value == 3 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func PairCount(hand string) (int) {
|
||||
|
||||
cardMap := map[rune]int{}
|
||||
for _, card := range hand {
|
||||
cardMap[card] ++;
|
||||
}
|
||||
count := 0;
|
||||
for _,value := range cardMap {
|
||||
if value == 2 {
|
||||
count ++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func IsFullHouse(hand string) (bool) {
|
||||
return PairCount(hand) == 1 && HasTriple(hand)
|
||||
}
|
||||
|
||||
func GetType(hand string) int {
|
||||
pc := 0;
|
||||
if IsFiveOfAKind(hand) {
|
||||
return 6
|
||||
} else if IsFourOfAKind(hand) {
|
||||
return 5
|
||||
} else if IsFullHouse(hand) {
|
||||
return 4
|
||||
} else if HasTriple(hand) {
|
||||
return 3
|
||||
} else if pc = PairCount(hand); pc == 2 {
|
||||
return 2
|
||||
} else if pc == 1 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Wins(left string, right string) (int) {
|
||||
leftType := GetType(left)
|
||||
rightType := GetType(right)
|
||||
if leftType == rightType {
|
||||
return GetValue(left) - GetValue(right)
|
||||
}
|
||||
return leftType - rightType
|
||||
}
|
||||
|
||||
func GetValue(hand string) (int) {
|
||||
cardValues := map[rune]int{'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, '1': 1}
|
||||
value := 0
|
||||
for _, card := range hand {
|
||||
value *= 14
|
||||
value += cardValues[card]-1
|
||||
}
|
||||
return value
|
||||
}
|
116
2023/07/part1/main_test.go
Normal file
116
2023/07/part1/main_test.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsFiveOfAKind(t *testing.T) {
|
||||
if IsFiveOfAKind("AAAAQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if !IsFiveOfAKind("AAAAA") {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFourOfAKind(t *testing.T) {
|
||||
if !IsFourOfAKind("AAAAQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if IsFourOfAKind("AAAQQ") {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFullHouse(t *testing.T) {
|
||||
if !IsFullHouse("AAAQQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if IsFullHouse("AAKQQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if IsFullHouse("AAAAA") {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasTriple(t *testing.T) {
|
||||
if HasTriple("AAKQQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if HasTriple("AAAAQ") {
|
||||
t.Fail()
|
||||
}
|
||||
if !HasTriple("AAAQQ") {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPairCount(t *testing.T) {
|
||||
if PairCount("AAKQQ") != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
if PairCount("AQKQA") != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
if PairCount("12345") != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetType(t *testing.T) {
|
||||
pass := GetType("AAAAA") == 6 &&
|
||||
GetType("AAKAA") == 5 &&
|
||||
GetType("AAKAK") == 4 &&
|
||||
GetType("AAAKQ") == 3 &&
|
||||
GetType("AAKK2") == 2 &&
|
||||
GetType("AAK12") == 1 &&
|
||||
GetType("12345") == 0
|
||||
|
||||
if !pass {
|
||||
t.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
func TestWins(t *testing.T) {
|
||||
if Wins("AAAAA", "AAAA1") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("AAAA1", "AAA11") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("AAA11", "AAA12") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("AAA12", "AA123") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("AA123", "A1234") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("A1234", "1234A") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("AAAAK", "KAAAA") < 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("12212", "22112") > 0 {
|
||||
t.Fail();
|
||||
}
|
||||
if Wins("77788", "77888") > 0 {
|
||||
t.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetValue(t *testing.T) {
|
||||
if GetValue("A") != 13 {
|
||||
t.Fail()
|
||||
}
|
||||
if GetValue("AA") != 13*14 + 13 {
|
||||
t.Fail()
|
||||
}
|
||||
if GetValue("KA") != 12*14 + 13 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue