Skip to content

Commit

Permalink
day 7 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Dec 7, 2023
1 parent be6cd85 commit 3385e41
Show file tree
Hide file tree
Showing 3 changed files with 1,267 additions and 0 deletions.
145 changes: 145 additions & 0 deletions 07/07.go
@@ -0,0 +1,145 @@
package main

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

"github.com/angristan/advent-of-code-2023/utils"
)

func main() {
input := utils.ParseInput("/Users/stanislaslange/advent-of-code-2023/07/input.txt")

parsedInput := ConvertRawInputToInput(input)
part1Score := parsedInput.ComputeTotalPoints()
fmt.Printf("Part 1: %d\n", part1Score)
}

type Hand struct {
Cards string
Bid int
}

type Input struct {
Hands []Hand
}

func ConvertRawInputToInput(rawInput []string) Input {
input := Input{}

for _, line := range rawInput {
splitLine := strings.Split(line, " ")
cards := splitLine[0]
bid, err := strconv.Atoi(splitLine[1])
if err != nil {
panic(err)
}

hand := Hand{
Cards: cards,
Bid: bid,
}

input.Hands = append(input.Hands, hand)
}

return input
}

func (input Input) SortHands() {
sort.Slice(input.Hands, func(i, j int) bool {
occurrencesI := input.Hands[i].Occurrences()
occurrencesJ := input.Hands[j].Occurrences()

maxOccurrenceCountI := 0
for i, occurrence := range occurrencesI {
if occurrence > 0 {
maxOccurrenceCountI = i + 1
}
}

maxOccurrenceCountJ := 0
for i, occurrence := range occurrencesJ {
if occurrence > 0 {
maxOccurrenceCountJ = i + 1
}
}

if maxOccurrenceCountI != maxOccurrenceCountJ {
return maxOccurrenceCountI < maxOccurrenceCountJ
}

if maxOccurrenceCountI == 3 || maxOccurrenceCountI == 2 {
if occurrencesI[2-1] != occurrencesJ[2-1] {
return occurrencesI[2-1] < occurrencesJ[2-1]
}
}

strengths := map[string]int{
"A": 13,
"K": 12,
"Q": 11,
"J": 10,
"T": 9,
"9": 8,
"8": 7,
"7": 6,
"6": 5,
"5": 4,
"4": 3,
"3": 2,
"2": 1,
}

for cardIndex := 0; cardIndex < 5; cardIndex++ {
cardJ := string(input.Hands[j].Cards[cardIndex])
cardI := string(input.Hands[i].Cards[cardIndex])

if strengths[cardI] != strengths[cardJ] {
return strengths[cardI] < strengths[cardJ]
}
}

return true
})
}

func (hand Hand) Occurrences() []int {
occurrences := make([]int, 5)

for _, card := range hand.Cards {
count := strings.Count(hand.Cards, string(card))

if count == 0 {
continue
}
indexCount := count - 1

if ok := occurrences[indexCount]; ok == 0 {
occurrences[indexCount] = 1
} else {
occurrences[indexCount]++
}

hand.Cards = strings.ReplaceAll(hand.Cards, string(card), "")
}

return occurrences
}

func (input Input) ComputeTotalPoints() int {
input.SortHands()

for _, hand := range input.Hands {
fmt.Printf("%s %d\n", hand.Cards, hand.Bid)
}

totalPoints := 0
for i, hand := range input.Hands {
totalPoints += hand.Bid * (i + 1)
}

return totalPoints
}
122 changes: 122 additions & 0 deletions 07/07_test.go
@@ -0,0 +1,122 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConvertRawInputToInput(t *testing.T) {
input := []string{
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483",
}

expected := Input{
Hands: []Hand{
{Cards: "32T3K", Bid: 765},
{Cards: "T55J5", Bid: 684},
{Cards: "KK677", Bid: 28},
{Cards: "KTJJT", Bid: 220},
{Cards: "QQQJA", Bid: 483},
},
}

assert.Equal(t, expected, ConvertRawInputToInput(input))
}

func TestOccurences(t *testing.T) {
input := Input{
Hands: []Hand{
{Cards: "T55J5", Bid: 684},
{Cards: "32T3K", Bid: 765},
{Cards: "QQQJA", Bid: 483},
{Cards: "KK677", Bid: 28},
{Cards: "KTJJT", Bid: 220},
{Cards: "T3T3J", Bid: 17},
{Cards: "Q2KJJ", Bid: 13},
},
}

expected := [][]int{
{2, 0, 1, 0, 0},
{3, 1, 0, 0, 0},
{2, 0, 1, 0, 0},
{1, 2, 0, 0, 0},
{1, 2, 0, 0, 0},
{1, 2, 0, 0, 0},
{3, 1, 0, 0, 0},
}

for i, hand := range input.Hands {
assert.Equal(t, expected[i], hand.Occurrences())
}
}

func TestSortHands(t *testing.T) {
type test struct {
input Input
expected Input
}

tests := []test{
{
input: Input{
Hands: []Hand{
{Cards: "T55J5", Bid: 684},
{Cards: "32T3K", Bid: 765},
{Cards: "QQQJA", Bid: 483},
{Cards: "KK677", Bid: 28},
{Cards: "KTJJT", Bid: 220},
},
},
expected: Input{
Hands: []Hand{
{Cards: "32T3K", Bid: 765},
{Cards: "KTJJT", Bid: 220},
{Cards: "KK677", Bid: 28},
{Cards: "T55J5", Bid: 684},
{Cards: "QQQJA", Bid: 483},
},
},
},
{
input: Input{
Hands: []Hand{
{Cards: "T3T3J", Bid: 17},
{Cards: "Q2KJJ", Bid: 13},
},
},
expected: Input{
Hands: []Hand{
{Cards: "Q2KJJ", Bid: 13},
{Cards: "T3T3J", Bid: 17},
},
},
},
}

for _, tc := range tests {
tc.input.SortHands()
assert.Equal(t, tc.expected, tc.input)
}
}

func TestComputeTotalPoints(t *testing.T) {
input := Input{
Hands: []Hand{
{Cards: "32T3K", Bid: 765},
{Cards: "KTJJT", Bid: 220},
{Cards: "KK677", Bid: 28},
{Cards: "T55J5", Bid: 684},
{Cards: "QQQJA", Bid: 483},
},
}

expected := 6440

assert.Equal(t, expected, input.ComputeTotalPoints())
}

0 comments on commit 3385e41

Please sign in to comment.