Skip to content

Commit

Permalink
Add day7 part one
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 8, 2023
1 parent 574166f commit 5d36562
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 1 deletion.
35 changes: 34 additions & 1 deletion day7/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"sort"
"strconv"
"strings"

Expand All @@ -18,6 +19,8 @@ const (
FullHouse
FourOfAKind
FiveOfAKind

MaxCardByHand = 5
)

type Occurences map[string]int
Expand Down Expand Up @@ -80,6 +83,34 @@ type Hand struct {

type Hands []Hand

func (h Hands) SortDesc() {
sort.Slice(h, func(i, j int) bool {
if h[i].Type < h[j].Type {
return true
}
if h[i].Type == h[j].Type {
for k := 0; k < MaxCardByHand; k++ {
if h[i].Cards[k].Strength == h[j].Cards[k].Strength {
continue
}
if h[i].Cards[k].Strength < h[j].Cards[k].Strength {
return true
}
return false
}
}
return false
})
}

func (h Hands) ComputeTotalWinnings() int {
total := 0
for i, hand := range h {
total += hand.Bid * (i + 1)
}
return total
}

func parseHands(input []string) Hands {
hands := make(Hands, 0)
for _, line := range input {
Expand Down Expand Up @@ -110,5 +141,7 @@ func parseHands(input []string) Hands {

func main() {
input := utils.ReadFileByLine("day7/input.txt")
fmt.Println(parseHands(input))
hands := parseHands(input)
hands.SortDesc()
fmt.Println("Part 1:", hands.ComputeTotalWinnings())
}
138 changes: 138 additions & 0 deletions day7/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,141 @@ func TestParseHands(t *testing.T) {
t.Errorf("Expected %v, got %v", expected, actual)
}
}

func TestSortHands(t *testing.T) {
input := []string{
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483",
}
expected := Hands{
{
Cards: []Card{
{Value: "3", Strength: 2},
{Value: "2", Strength: 1},
{Value: "T", Strength: 9},
{Value: "3", Strength: 2},
{Value: "K", Strength: 12},
},
Bid: 765,
Type: OnePair,
},
{
Cards: []Card{
{Value: "K", Strength: 12},
{Value: "T", Strength: 9},
{Value: "J", Strength: 10},
{Value: "J", Strength: 10},
{Value: "T", Strength: 9},
},
Bid: 220,
Type: TwoPair,
},
{
Cards: []Card{
{Value: "K", Strength: 12},
{Value: "K", Strength: 12},
{Value: "6", Strength: 5},
{Value: "7", Strength: 6},
{Value: "7", Strength: 6},
},
Bid: 28,
Type: TwoPair,
},
{
Cards: []Card{
{Value: "T", Strength: 9},
{Value: "5", Strength: 4},
{Value: "5", Strength: 4},
{Value: "J", Strength: 10},
{Value: "5", Strength: 4},
},
Bid: 684,
Type: ThreeOfAKind,
},
{
Cards: []Card{
{Value: "Q", Strength: 11},
{Value: "Q", Strength: 11},
{Value: "Q", Strength: 11},
{Value: "J", Strength: 10},
{Value: "A", Strength: 13},
},
Bid: 483,
Type: ThreeOfAKind,
},
}
actual := parseHands(input)
actual.SortDesc()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected %v, got %v", expected, actual)
}
}

func TestComputeTotalWinnings(t *testing.T) {
input := Hands{
{
Cards: []Card{
{Value: "3", Strength: 2},
{Value: "2", Strength: 1},
{Value: "T", Strength: 9},
{Value: "3", Strength: 2},
{Value: "K", Strength: 12},
},
Bid: 765,
Type: OnePair,
},
{
Cards: []Card{
{Value: "T", Strength: 9},
{Value: "5", Strength: 4},
{Value: "5", Strength: 4},
{Value: "J", Strength: 10},
{Value: "5", Strength: 4},
},
Bid: 684,
Type: ThreeOfAKind,
},
{
Cards: []Card{
{Value: "K", Strength: 12},
{Value: "K", Strength: 12},
{Value: "6", Strength: 5},
{Value: "7", Strength: 6},
{Value: "7", Strength: 6},
},
Bid: 28,
Type: TwoPair,
},
{
Cards: []Card{
{Value: "K", Strength: 12},
{Value: "T", Strength: 9},
{Value: "J", Strength: 10},
{Value: "J", Strength: 10},
{Value: "T", Strength: 9},
},
Bid: 220,
Type: TwoPair,
},
{
Cards: []Card{
{Value: "Q", Strength: 11},
{Value: "Q", Strength: 11},
{Value: "Q", Strength: 11},
{Value: "J", Strength: 10},
{Value: "A", Strength: 13},
},
Bid: 483,
Type: ThreeOfAKind,
},
}
expected := 6440
input.SortDesc()
actual := input.ComputeTotalWinnings()
if actual != expected {
t.Errorf("Expected %v, got %v", expected, actual)
}
}

0 comments on commit 5d36562

Please sign in to comment.