From 62d8af2d1b5bc04d8e383c174f9417b26c581956 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Fri, 8 Dec 2023 00:33:30 +0100 Subject: [PATCH] Non-working prototype --- day07/cards.go | 114 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 7 deletions(-) diff --git a/day07/cards.go b/day07/cards.go index dbc062c..022dd64 100644 --- a/day07/cards.go +++ b/day07/cards.go @@ -4,7 +4,8 @@ import ( "fmt" "strings" "bufio" -// "math" + "math" +// "math/rand" "os" "strconv" // "regexp" @@ -33,6 +34,21 @@ type Game struct { // 0: High card, 1: One pair, 2: Two pair, 3: Three of a kind // 4: Full house, 5: Four of a kind, 6: Five of a kind typeOfHand [7][]string + indexOfHand [7][]int + baseThirteen [7][]int +} + +func (g *Game) ChangeBase(hType, index int, wg *sync.WaitGroup) { + // Starting from the first char [0], we create the base13 num + chars := len(g.typeOfHand[hType][index]) + baseTN := g.typeOfHand[hType][index] + decNum := 0 + for i := 0; i < chars; i++ { + // This should be refactored to be a bit more legible + // It just computes N * 13^i and adds it over + decNum += int(float64(mapSeeds[string(baseTN[i])]) * math.Pow(13, float64(chars - i))) } + g.baseThirteen[hType][index] = decNum + wg.Done() } func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) int { @@ -52,29 +68,33 @@ func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) int { case 1: g.mu.Lock() g.typeOfHand[6] = append(g.typeOfHand[6], cards) + g.indexOfHand[6] = append(g.indexOfHand[6], index) g.mu.Unlock() // Four of a kind || Full House case 2: - //call func i := FullOrFour(cards) g.mu.Lock() g.typeOfHand[i] = append(g.typeOfHand[i], cards) + g.indexOfHand[i] = append(g.indexOfHand[i], index) g.mu.Unlock() // Three of a kind || Two pair case 3: i := ThreeOrTwo(cards) g.mu.Lock() g.typeOfHand[i] = append(g.typeOfHand[i], cards) + g.indexOfHand[i] = append(g.indexOfHand[i], index) g.mu.Unlock() // One pair case 4: g.mu.Lock() g.typeOfHand[1] = append(g.typeOfHand[1], cards) + g.indexOfHand[1] = append(g.indexOfHand[1], index) g.mu.Unlock() // High card case 5: g.mu.Lock() g.typeOfHand[0] = append(g.typeOfHand[0], cards) + g.indexOfHand[0] = append(g.indexOfHand[0], index) g.mu.Unlock() } wg.Done() @@ -113,7 +133,7 @@ func FullOrFour(cards string) int { // If an element has four values, we have a Four of a kind if m[i] == 4 { return 5 - /// If an element has 3 values, we have a four of a kind + /// If an element has 3 values, we have a Full House } else if m[i] == 3 { return 4 } @@ -132,6 +152,44 @@ func PrintAndWait(x ...any) { fmt.Scanln() } +// https://www.golangprograms.com/golang-program-for-implementation-of-quick-sort.html +// I need to learn how this shing works +func quicksort(a []int, g *Game, hType int) []int { + if len(a) < 2 { + return a + } + + left, right := 0, len(a)-1 + + pivot := 0 + + a[pivot], a[right] = a[right], a[pivot] + g.mu.Lock() + g.indexOfHand[hType][pivot], g.indexOfHand[hType][right] = g.indexOfHand[hType][right], g.indexOfHand[hType][pivot] + g.mu.Unlock() + + for i, _ := range a { + if a[i] < a[right] { + a[left], a[i] = a[i], a[left] + g.mu.Lock() + g.indexOfHand[hType][left], g.indexOfHand[hType][i] = g.indexOfHand[hType][i], g.indexOfHand[hType][left] + g.mu.Unlock() + left++ + } + } + + a[left], a[right] = a[right], a[left] + g.mu.Lock() + g.indexOfHand[hType][left], g.indexOfHand[hType][right] = g.indexOfHand[hType][right], g.indexOfHand[hType][left] + g.mu.Unlock() + + + quicksort(a[:left], g, hType) + quicksort(a[left+1:], g, hType) + + return a +} + func main() { file, err := os.Open("./inputs/day07_input") @@ -140,7 +198,6 @@ func main() { // Struct for multiple races g := Game{} - m := make(map[string]int) // Variable where we store every line in the file lines := make([]string, 0) @@ -172,7 +229,7 @@ func main() { // Three identical: 3oK or 22 // Four identical: 12 // - // Will try to call a different function for every line + // With this, I put every typeOfHand in a different array var wg sync.WaitGroup wg.Add(len(lines)) for i := 0; i < len(lines); i++ { @@ -180,6 +237,49 @@ func main() { } wg.Wait() - PrintAndWait(g.typeOfHand) - _ = m + // Now that g.typeOfHand has every type of hand separated, the rank + // is determined by the first card(s). I can convert every number + // to a base 13 representation and sort. + // In g.indexOfHand I have the index of the corresponding type. + + // For every type of hand + for i := range g.typeOfHand { + // As many wait groups as the element we will iterate + wg.Add(len(g.typeOfHand[i])) + // We also need to initialize the array g.baseThirteen so we can + // keep the same index + g.baseThirteen[i] = make([]int, len(g.typeOfHand[i])) + // For every element in a single type of hand + for j := range g.typeOfHand[i] { + go g.ChangeBase(i, j, &wg) + } + } + wg.Wait() + + + PrintAndWait(g.typeOfHand[0]) + PrintAndWait(g.typeOfHand[1]) + PrintAndWait(g.typeOfHand[2]) + PrintAndWait(g.typeOfHand[3]) + PrintAndWait(g.typeOfHand[4]) + PrintAndWait(g.typeOfHand[5]) + PrintAndWait(g.typeOfHand[6]) + // A sort of some kind. Important is to also move the index with the number as + // well. + // + for i := range g.baseThirteen { + quicksort(g.baseThirteen[i], &g, i) + } + + curRank := 1 + rank := 0 + // Iter every array + for i := range g.typeOfHand { + for j := range g.typeOfHand[i] { + index := g.indexOfHand[i][j] + rank += curRank * bet[index] + curRank++ + } + } + fmt.Print(rank) }