Skip to content

Commit

Permalink
day 7 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Dec 7, 2023
1 parent 3385e41 commit f7aacb8
Show file tree
Hide file tree
Showing 2 changed files with 435 additions and 28 deletions.
220 changes: 208 additions & 12 deletions 07/07.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@ import (
)

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

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

parsedInputV2 := ConvertRawInputToInputV2(input)
for i := range parsedInputV2.Hands {
parsedInputV2.Hands[i].ComputeAndAssignHandType()
parsedInputV2.Hands[i].JokerMode()
}
parsedInputV2.SortHands()
part2Score := parsedInputV2.ComputeTotalPoints()
fmt.Printf("Part 2: %d\n", part2Score)
}

type Hand struct {
type HandV1 struct {
Cards string
Bid int
}

type Input struct {
Hands []Hand
type InputV1 struct {
Hands []HandV1
}

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

for _, line := range rawInput {
splitLine := strings.Split(line, " ")
Expand All @@ -37,7 +46,7 @@ func ConvertRawInputToInput(rawInput []string) Input {
panic(err)
}

hand := Hand{
hand := HandV1{
Cards: cards,
Bid: bid,
}
Expand All @@ -48,7 +57,7 @@ func ConvertRawInputToInput(rawInput []string) Input {
return input
}

func (input Input) SortHands() {
func (input InputV1) SortHands() {
sort.Slice(input.Hands, func(i, j int) bool {
occurrencesI := input.Hands[i].Occurrences()
occurrencesJ := input.Hands[j].Occurrences()
Expand Down Expand Up @@ -106,7 +115,7 @@ func (input Input) SortHands() {
})
}

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

for _, card := range hand.Cards {
Expand All @@ -129,13 +138,200 @@ func (hand Hand) Occurrences() []int {
return occurrences
}

func (input Input) ComputeTotalPoints() int {
func (input InputV1) 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
}

/*
==============================
Part 2
==============================
*/

type HandType int

const (
HighCard HandType = iota
OnePair
TwoPair
ThreeOfAKind
FullHouse
FourOfAKind
FiveOfAKind
)

type HandV2 struct {
Cards string
Bid int
HandType HandType
}

type InputV2 struct {
Hands []HandV2
}

func ConvertRawInputToInputV2(rawInput []string) InputV2 {
input := InputV2{}

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

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

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

return input
}

func (hand HandV2) ComputeOcurrences() []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 (hand *HandV2) ComputeAndAssignHandType() {
occurrences := hand.ComputeOcurrences()

if occurrences[5-1] == 1 {
hand.HandType = FiveOfAKind
return
}

if occurrences[4-1] == 1 {
hand.HandType = FourOfAKind
return
}

if occurrences[3-1] == 1 && occurrences[2-1] == 1 {
hand.HandType = FullHouse
return
}

if occurrences[3-1] == 1 {
hand.HandType = ThreeOfAKind
return
}

if occurrences[2-1] == 2 {
hand.HandType = TwoPair
return
}

if occurrences[2-1] == 1 {
hand.HandType = OnePair
return
}

hand.HandType = HighCard
}

func (hand *HandV2) JokerMode() {
JCount := strings.Count(hand.Cards, "J")

if JCount == 0 {
return
}

for i := 0; i < JCount; i++ {
switch hand.HandType {
case FiveOfAKind:
return
case FourOfAKind:
hand.HandType = FiveOfAKind
if strings.Count(hand.Cards, "J") == 4 {
return
}
case FullHouse:
hand.HandType = FourOfAKind
case ThreeOfAKind:
hand.HandType = FourOfAKind
if strings.Count(hand.Cards, "J") == 3 {
return
}
case TwoPair:
hand.HandType = FullHouse
case OnePair:
hand.HandType = ThreeOfAKind
if strings.Count(hand.Cards, "J") == 2 {
return
}
case HighCard:
hand.HandType = OnePair
}
}
}

func (input InputV2) SortHands() {
sort.Slice(input.Hands, func(i, j int) bool {
if input.Hands[i].HandType != input.Hands[j].HandType {
return input.Hands[i].HandType < input.Hands[j].HandType
}

strengths := map[string]int{
"A": 13,
"K": 12,
"Q": 11,
"T": 10,
"9": 9,
"8": 8,
"7": 7,
"6": 6,
"5": 5,
"4": 4,
"3": 3,
"2": 2,
"J": 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 (input InputV2) ComputeTotalPoints() int {
input.SortHands()

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

0 comments on commit f7aacb8

Please sign in to comment.