Working day07, huge mess
This commit is contained in:
parent
dc2cb20d36
commit
8b15f34960
189
day07/cards.go
189
day07/cards.go
@ -1,18 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
// "math/rand"
|
"strings"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
// "regexp"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mapSeeds = map[string]int{
|
var mapSeedsFirst = map[string]int{
|
||||||
"A": 12,
|
"A": 12,
|
||||||
"K": 11,
|
"K": 11,
|
||||||
"Q": 10,
|
"Q": 10,
|
||||||
@ -28,6 +26,22 @@ var mapSeeds = map[string]int{
|
|||||||
"2": 0,
|
"2": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mapSeedsSecond = map[string]int{
|
||||||
|
"A": 12,
|
||||||
|
"K": 11,
|
||||||
|
"Q": 10,
|
||||||
|
"T": 9,
|
||||||
|
"9": 8,
|
||||||
|
"8": 7,
|
||||||
|
"7": 6,
|
||||||
|
"6": 5,
|
||||||
|
"5": 4,
|
||||||
|
"4": 3,
|
||||||
|
"3": 2,
|
||||||
|
"2": 1,
|
||||||
|
"J": 0,
|
||||||
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
ranks []int
|
ranks []int
|
||||||
@ -38,7 +52,7 @@ type Game struct {
|
|||||||
baseThirteen [7][]int
|
baseThirteen [7][]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) ChangeBase(hType, index int, wg *sync.WaitGroup) {
|
func (g *Game) ChangeBase(hType, index int, mapSeeds map[string]int, wg *sync.WaitGroup) {
|
||||||
// Starting from the first char [0], we create the base13 num
|
// Starting from the first char [0], we create the base13 num
|
||||||
chars := len(g.typeOfHand[hType][index])
|
chars := len(g.typeOfHand[hType][index])
|
||||||
baseTN := g.typeOfHand[hType][index]
|
baseTN := g.typeOfHand[hType][index]
|
||||||
@ -46,23 +60,19 @@ func (g *Game) ChangeBase(hType, index int, wg *sync.WaitGroup) {
|
|||||||
for i := 0; i < chars; i++ {
|
for i := 0; i < chars; i++ {
|
||||||
// This should be refactored to be a bit more legible
|
// This should be refactored to be a bit more legible
|
||||||
// It just computes N * 13^i and adds it over
|
// It just computes N * 13^i and adds it over
|
||||||
decNum += int(float64(mapSeeds[string(baseTN[i])]) * math.Pow(13, float64(chars - i))) }
|
decNum += int(float64(mapSeeds[string(baseTN[i])]) * math.Pow(13, float64(chars-i)))
|
||||||
|
}
|
||||||
g.baseThirteen[hType][index] = decNum
|
g.baseThirteen[hType][index] = decNum
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) int {
|
func (g *Game) AnalyzeMap(cards string, index, mapSize, offset int) {
|
||||||
// We create a map and we check the length. Depending on the length, we
|
// The offset defaults to 0 for the regular game
|
||||||
// insert the string in a specific type
|
// it is +1 for a game with Jokers\
|
||||||
m := make(map[string]int)
|
mapSeed := mapSeedsFirst
|
||||||
for i := 0; i < len(cards); i++ {
|
if offset != 0 {
|
||||||
key := string(cards[i])
|
mapSeed = mapSeedsSecond
|
||||||
m[key] = mapSeeds[key]
|
|
||||||
}
|
}
|
||||||
// Now, depending on the number of elements in the map, we can assign
|
|
||||||
// append cards to a specific rank
|
|
||||||
mapSize := len(m)
|
|
||||||
|
|
||||||
switch mapSize {
|
switch mapSize {
|
||||||
// Five of a kind
|
// Five of a kind
|
||||||
case 1:
|
case 1:
|
||||||
@ -72,14 +82,14 @@ func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) int {
|
|||||||
g.mu.Unlock()
|
g.mu.Unlock()
|
||||||
// Four of a kind || Full House
|
// Four of a kind || Full House
|
||||||
case 2:
|
case 2:
|
||||||
i := FullOrFour(cards)
|
i := FullOrFour(cards, offset, mapSeed)
|
||||||
g.mu.Lock()
|
g.mu.Lock()
|
||||||
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
||||||
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
||||||
g.mu.Unlock()
|
g.mu.Unlock()
|
||||||
// Three of a kind || Two pair
|
// Three of a kind || Two pair
|
||||||
case 3:
|
case 3:
|
||||||
i := ThreeOrTwo(cards)
|
i := ThreeOrTwo(cards, offset, mapSeed)
|
||||||
g.mu.Lock()
|
g.mu.Lock()
|
||||||
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
||||||
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
||||||
@ -97,47 +107,111 @@ func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) int {
|
|||||||
g.indexOfHand[0] = append(g.indexOfHand[0], index)
|
g.indexOfHand[0] = append(g.indexOfHand[0], index)
|
||||||
g.mu.Unlock()
|
g.mu.Unlock()
|
||||||
}
|
}
|
||||||
wg.Done()
|
|
||||||
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ThreeOrTwo(cards string) int {
|
func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) {
|
||||||
|
// We create a map and we check the length. Depending on the length, we
|
||||||
|
// insert the string in a specific type
|
||||||
|
m := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
m[key] = mapSeedsFirst[key]
|
||||||
|
}
|
||||||
|
// Now, depending on the number of elements in the map, we can assign
|
||||||
|
// append cards to a specific rank
|
||||||
|
mapSize := len(m)
|
||||||
|
g.AnalyzeMap(cards, index, mapSize, 0)
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) SecondGame(cards string, index int, wg *sync.WaitGroup) {
|
||||||
|
// If I don't have Js, run the standard DetermineType
|
||||||
|
m := make(map[string]int)
|
||||||
|
n := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
// We need to track the number of Js
|
||||||
|
m[key] += 1
|
||||||
|
// This is to track the type of hand, knowing the number of Js
|
||||||
|
n[key] = mapSeedsSecond[key]
|
||||||
|
}
|
||||||
|
mapSize := len(n)
|
||||||
|
switch m["J"] {
|
||||||
|
case 0:
|
||||||
|
// We have a hand without Js
|
||||||
|
wg.Add(1)
|
||||||
|
g.DetermineType(cards, index, wg)
|
||||||
|
case 1:
|
||||||
|
// If there is a J, J can be use is not adding information, therefore -1
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 1)
|
||||||
|
case 2:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 2)
|
||||||
|
case 3:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 3)
|
||||||
|
case 4:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 4)
|
||||||
|
case 5:
|
||||||
|
wg.Add(1)
|
||||||
|
g.DetermineType(cards, index, wg)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ThreeOrTwo(cards string, offset int, mapSeed map[string]int) int {
|
||||||
m := make(map[string]int)
|
m := make(map[string]int)
|
||||||
for i := 0; i < len(cards); i++ {
|
for i := 0; i < len(cards); i++ {
|
||||||
key := string(cards[i])
|
key := string(cards[i])
|
||||||
m[key] += 1
|
m[key] += 1
|
||||||
}
|
}
|
||||||
|
// If we are in the second game, remove J
|
||||||
|
if mapSeed["J"] == 0 {
|
||||||
|
m["J"] = 0
|
||||||
|
}
|
||||||
// m[i] returns 0 if the element is not in the map. I take advantage
|
// m[i] returns 0 if the element is not in the map. I take advantage
|
||||||
// of that
|
// of that
|
||||||
for i := range mapSeeds {
|
tempNum := 0
|
||||||
|
for i := range mapSeed {
|
||||||
|
if m[i] > tempNum {
|
||||||
|
tempNum = m[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
// If an element has 3 values, we have a three of a kind
|
// If an element has 3 values, we have a three of a kind
|
||||||
if m[i] == 3 {
|
if tempNum+offset == 3 {
|
||||||
return 3
|
return 3
|
||||||
/// If an element has 2 values, we have a two pair
|
/// If an element has 2 values, we have a two pair
|
||||||
} else if m[i] == 2 {
|
} else if tempNum+offset == 2 {
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
}
|
PrintAndWait("This has run for ", cards)
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
func FullOrFour(cards string) int {
|
func FullOrFour(cards string, offset int, mapSeed map[string]int) int {
|
||||||
m := make(map[string]int)
|
m := make(map[string]int)
|
||||||
for i := 0; i < len(cards); i++ {
|
for i := 0; i < len(cards); i++ {
|
||||||
key := string(cards[i])
|
key := string(cards[i])
|
||||||
m[key] += 1
|
m[key] += 1
|
||||||
}
|
}
|
||||||
|
// If we are in the second game, remove J
|
||||||
|
if mapSeed["J"] == 0 {
|
||||||
|
m["J"] = 0
|
||||||
|
}
|
||||||
// m[i] returns 0 if the element is not in the map. I take advantage
|
// m[i] returns 0 if the element is not in the map. I take advantage
|
||||||
// of that
|
// of that
|
||||||
for i := range mapSeeds {
|
// Better to save the maximum number
|
||||||
|
tempNum := 0
|
||||||
|
for i := range mapSeed {
|
||||||
|
if m[i] > tempNum {
|
||||||
|
tempNum = m[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
// If an element has four values, we have a Four of a kind
|
// If an element has four values, we have a Four of a kind
|
||||||
if m[i] == 4 {
|
if tempNum+offset == 4 {
|
||||||
return 5
|
return 5
|
||||||
/// If an element has 3 values, we have a Full House
|
/// If an element has 3 values, we have a Full House
|
||||||
} else if m[i] == 3 {
|
} else if tempNum+offset == 3 {
|
||||||
return 4
|
return 4
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +251,6 @@ func quicksort(a, h []int, hType int) []int {
|
|||||||
a[left], a[right] = a[right], a[left]
|
a[left], a[right] = a[right], a[left]
|
||||||
h[left], h[right] = h[right], h[left]
|
h[left], h[right] = h[right], h[left]
|
||||||
|
|
||||||
|
|
||||||
quicksort(a[:left], h[:left], hType)
|
quicksort(a[:left], h[:left], hType)
|
||||||
quicksort(a[left+1:], h[left+1:], hType)
|
quicksort(a[left+1:], h[left+1:], hType)
|
||||||
|
|
||||||
@ -186,12 +259,14 @@ func quicksort(a, h []int, hType int) []int {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
file, err := os.Open("./inputs/day07_input")
|
file, err := os.Open("./inputs/day7_try")
|
||||||
check(err)
|
check(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// Struct for multiple races
|
// Struct for regular game
|
||||||
g := Game{}
|
g := Game{}
|
||||||
|
// Struct for the Joker game
|
||||||
|
jo := Game{}
|
||||||
|
|
||||||
// Variable where we store every line in the file
|
// Variable where we store every line in the file
|
||||||
lines := make([]string, 0)
|
lines := make([]string, 0)
|
||||||
@ -213,6 +288,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
// Rank will be from 1 to len(lines)
|
// Rank will be from 1 to len(lines)
|
||||||
g.ranks = make([]int, len(lines))
|
g.ranks = make([]int, len(lines))
|
||||||
|
jo.ranks = make([]int, len(lines))
|
||||||
// What do we know for sure? 5 identical seeds are the highest ranks,
|
// What do we know for sure? 5 identical seeds are the highest ranks,
|
||||||
// 5 completely different seeds are the lowest ranks.
|
// 5 completely different seeds are the lowest ranks.
|
||||||
// We can iterate for every set of cards, and do different things
|
// We can iterate for every set of cards, and do different things
|
||||||
@ -225,9 +301,10 @@ func main() {
|
|||||||
//
|
//
|
||||||
// With this, I put every typeOfHand in a different array
|
// With this, I put every typeOfHand in a different array
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(len(lines))
|
wg.Add(2 * len(lines))
|
||||||
for i := 0; i < len(lines); i++ {
|
for i := 0; i < len(lines); i++ {
|
||||||
go g.DetermineType(cards[i], i, &wg)
|
g.DetermineType(cards[i], i, &wg)
|
||||||
|
jo.SecondGame(cards[i], i, &wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@ -240,29 +317,26 @@ func main() {
|
|||||||
for i := range g.typeOfHand {
|
for i := range g.typeOfHand {
|
||||||
// As many wait groups as the element we will iterate
|
// As many wait groups as the element we will iterate
|
||||||
wg.Add(len(g.typeOfHand[i]))
|
wg.Add(len(g.typeOfHand[i]))
|
||||||
|
wg.Add(len(jo.typeOfHand[i]))
|
||||||
// We also need to initialize the array g.baseThirteen so we can
|
// We also need to initialize the array g.baseThirteen so we can
|
||||||
// keep the same index
|
// keep the same index
|
||||||
g.baseThirteen[i] = make([]int, len(g.typeOfHand[i]))
|
g.baseThirteen[i] = make([]int, len(g.typeOfHand[i]))
|
||||||
|
jo.baseThirteen[i] = make([]int, len(jo.typeOfHand[i]))
|
||||||
// For every element in a single type of hand
|
// For every element in a single type of hand
|
||||||
for j := range g.typeOfHand[i] {
|
for j := range g.typeOfHand[i] {
|
||||||
go g.ChangeBase(i, j, &wg)
|
g.ChangeBase(i, j, mapSeedsFirst, &wg)
|
||||||
|
}
|
||||||
|
for j := range jo.typeOfHand[i] {
|
||||||
|
jo.ChangeBase(i, j, mapSeedsSecond, &wg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wg.Wait()
|
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
|
// A sort of some kind. Important is to also move the index with the number as
|
||||||
// well.
|
// well.
|
||||||
//
|
|
||||||
for i := range g.baseThirteen {
|
for i := range g.baseThirteen {
|
||||||
quicksort(g.baseThirteen[i], g.indexOfHand[i], i)
|
quicksort(g.baseThirteen[i], g.indexOfHand[i], i)
|
||||||
|
quicksort(jo.baseThirteen[i], jo.indexOfHand[i], i)
|
||||||
}
|
}
|
||||||
|
|
||||||
curRank := 1
|
curRank := 1
|
||||||
@ -270,14 +344,23 @@ func main() {
|
|||||||
// Iter every array
|
// Iter every array
|
||||||
for i := range g.typeOfHand {
|
for i := range g.typeOfHand {
|
||||||
for j := range g.typeOfHand[i] {
|
for j := range g.typeOfHand[i] {
|
||||||
//fmt.Printf("For the array %d, element %d, ", i, j)
|
|
||||||
index := g.indexOfHand[i][j]
|
index := g.indexOfHand[i][j]
|
||||||
//fmt.Printf("I gather index %d ", index)
|
|
||||||
rank += curRank * bet[index]
|
rank += curRank * bet[index]
|
||||||
//fmt.Printf("and finally I multiply %d and %d.\n", curRank, bet[index])
|
|
||||||
//fmt.Scanln()
|
|
||||||
curRank++
|
curRank++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Print(rank)
|
fmt.Printf("Rank: %d\n", rank)
|
||||||
|
|
||||||
|
curRank = 1
|
||||||
|
rank = 0
|
||||||
|
// Iter every array
|
||||||
|
for i := range jo.typeOfHand {
|
||||||
|
for j := range jo.typeOfHand[i] {
|
||||||
|
index := jo.indexOfHand[i][j]
|
||||||
|
rank += curRank * bet[index]
|
||||||
|
curRank++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Rank: %d\n", rank)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user