Skip to content

Commit

Permalink
day 7 refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Dec 7, 2023
1 parent f7aacb8 commit 834f23e
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 326 deletions.
200 changes: 34 additions & 166 deletions 07/07.go
Expand Up @@ -13,148 +13,23 @@ func main() {
input := utils.ParseInput("input.txt")

parsedInput := ConvertRawInputToInput(input)
for i := range parsedInput.Hands {
parsedInput.Hands[i].ComputeAndAssignHandType()
}
parsedInput.SortHands(StrengthsPart1)
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()
parsedInputPart2 := ConvertRawInputToInput(input)
for i := range parsedInputPart2.Hands {
parsedInput.Hands[i].ComputeAndAssignHandType()
parsedInput.Hands[i].JokerMode()
}
parsedInputV2.SortHands()
part2Score := parsedInputV2.ComputeTotalPoints()
parsedInputPart2.SortHands(StrengthsPart2)
part2Score := parsedInputPart2.ComputeTotalPoints()
fmt.Printf("Part 2: %d\n", part2Score)
}

type HandV1 struct {
Cards string
Bid int
}

type InputV1 struct {
Hands []HandV1
}

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

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

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

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

return input
}

func (input InputV1) 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 HandV1) 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 InputV1) ComputeTotalPoints() int {
input.SortHands()

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

return totalPoints
}

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

type HandType int

const (
Expand All @@ -167,18 +42,28 @@ const (
FiveOfAKind
)

type HandV2 struct {
type Hand struct {
Cards string
Bid int
HandType HandType
}

type InputV2 struct {
Hands []HandV2
type Input struct {
Hands []Hand
}

func ConvertRawInputToInputV2(rawInput []string) InputV2 {
input := InputV2{}
var (
StrengthsPart1 = 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,
}

StrengthsPart2 = 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,
}
)

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

for _, line := range rawInput {
splitLine := strings.Split(line, " ")
Expand All @@ -188,7 +73,7 @@ func ConvertRawInputToInputV2(rawInput []string) InputV2 {
panic(err)
}

hand := HandV2{
hand := Hand{
Cards: cards,
Bid: bid,
}
Expand All @@ -199,7 +84,7 @@ func ConvertRawInputToInputV2(rawInput []string) InputV2 {
return input
}

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

for _, card := range hand.Cards {
Expand All @@ -222,7 +107,7 @@ func (hand HandV2) ComputeOcurrences() []int {
return occurrences
}

func (hand *HandV2) ComputeAndAssignHandType() {
func (hand *Hand) ComputeAndAssignHandType() {
occurrences := hand.ComputeOcurrences()

if occurrences[5-1] == 1 {
Expand Down Expand Up @@ -258,7 +143,7 @@ func (hand *HandV2) ComputeAndAssignHandType() {
hand.HandType = HighCard
}

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

if JCount == 0 {
Expand Down Expand Up @@ -294,44 +179,27 @@ func (hand *HandV2) JokerMode() {
}
}

func (input InputV2) SortHands() {
func (input Input) SortHands(strengthsMap map[string]int) {
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,
}

// If both hands have the same type, compare the cards by strength
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]
if strengthsMap[cardI] != strengthsMap[cardJ] {
return strengthsMap[cardI] < strengthsMap[cardJ]
}
}

return true
})
}

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

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

0 comments on commit 834f23e

Please sign in to comment.