Skip to content

Instantly share code, notes, and snippets.

@badouralix
Created December 22, 2021 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badouralix/b1a76ba68a4e1c6a0baee959afe7f746 to your computer and use it in GitHub Desktop.
Save badouralix/b1a76ba68a4e1c6a0baee959afe7f746 to your computer and use it in GitHub Desktop.
Broken solution for https://adventofcode.com/2021/day/21, not sure why it does not work
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)
const MaxScore = 21
const MaxSpace = 10
func run(s string) uint64 {
// Your code goes here
player := 0
universes := [2][MaxScore + 1][MaxSpace + 1]uint64{}
// spaces := [2][MaxSpace + 1]uint64{}
for _, line := range strings.Split(s, "\n") {
if len(line) == 29 {
universes[player][0][int(line[28]-'0')] = 1
// spaces[player][int(line[28]-'0')] = 1
} else {
universes[player][0][10] = 1
// spaces[player][10] = 1
}
player = 1 - player
}
for {
// newspaces := [MaxSpace + 1]uint64{}
// for i := 1; i <= MaxSpace; i++ {
// newspaces[(i+3-1)%10+1] += 1 * spaces[player][i]
// newspaces[(i+4-1)%10+1] += 3 * spaces[player][i]
// newspaces[(i+5-1)%10+1] += 6 * spaces[player][i]
// newspaces[(i+6-1)%10+1] += 7 * spaces[player][i]
// newspaces[(i+7-1)%10+1] += 6 * spaces[player][i]
// newspaces[(i+8-1)%10+1] += 3 * spaces[player][i]
// newspaces[(i+9-1)%10+1] += 1 * spaces[player][i]
// }
// spaces[player] = newspaces
// for j := 1; j < MaxScore; j++ {
// for i := 1; i <= MaxSpace; i++ {
// scores[1-player][j][i] *= 27
// }
// }
for score := 0; score < MaxScore; score++ {
fmt.Println(universes[player][score])
spaces := [MaxSpace + 1]uint64{}
for space := 1; space <= MaxSpace; space++ {
spaces[(space+3-1)%10+1] += 1 * universes[player][score][space]
spaces[(space+4-1)%10+1] += 3 * universes[player][score][space]
spaces[(space+5-1)%10+1] += 6 * universes[player][score][space]
spaces[(space+6-1)%10+1] += 7 * universes[player][score][space]
spaces[(space+7-1)%10+1] += 6 * universes[player][score][space]
spaces[(space+8-1)%10+1] += 3 * universes[player][score][space]
spaces[(space+9-1)%10+1] += 1 * universes[player][score][space]
// scores[1-player][j][i] *= 27
}
universes[player][score] = spaces
fmt.Println(universes[player][score])
fmt.Println()
}
for space := 1; space <= MaxSpace; space++ {
for score := MaxScore - 1; score >= MaxScore-space; score-- { // MaxScore > MaxSpace
// fmt.Println(MaxScore, j, i)
universes[player][MaxScore][0] += universes[player][score][space]
universes[player][MaxScore][space] += universes[player][score][space]
}
}
foundTheSupremeGalaticChampion := true
// count := uint64(0)
// scores[player][0][0] = 0
for score := MaxScore - 1; score >= 0; score-- {
universes[player][score][0] = 0
for space := 1; space <= MaxSpace; space++ {
universes[player][score][space] = 0
if score-space < 0 {
continue
}
// fmt.Println(score, space)
// scores[player][j][i] += 1 * scores[player][j-i][(i-3+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 3 * scores[player][j-i][(i-4+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 6 * scores[player][j-i][(i-5+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 7 * scores[player][j-i][(i-6+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 6 * scores[player][j-i][(i-7+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 3 * scores[player][j-i][(i-8+MaxSpace-1)%MaxSpace+1]
// scores[player][j][i] += 1 * scores[player][j-i][(i-9+MaxSpace-1)%MaxSpace+1]
// scores[player][0][0] += scores[player][j-i][i]
universes[player][score][0] += universes[player][score-space][space]
universes[player][score][space] += universes[player][score-space][space]
// count += scores[player][j-i][i]
if universes[player][score][space] != 0 {
foundTheSupremeGalaticChampion = false
}
}
// if j-3 < 0 {
// continue
// }
// scores[player][j] += 1 * scores[player][j-3]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-4 < 0 {
// continue
// }
// scores[player][j] += 3 * scores[player][j-4]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-5 < 0 {
// continue
// }
// scores[player][j] += 6 * scores[player][j-5]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-6 < 0 {
// continue
// }
// scores[player][j] += 7 * scores[player][j-6]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-7 < 0 {
// continue
// }
// scores[player][j] += 6 * scores[player][j-7]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-8 < 0 {
// continue
// }
// scores[player][j] += 3 * scores[player][j-8]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
// if j-9 < 0 {
// continue
// }
// scores[player][j] += 1 * scores[player][j-9]
// foundTheSupremeGalaticChampion = foundTheSupremeGalaticChampion && scores[player][j] == 0
}
fmt.Println(player, universes[player])
fmt.Println(1-player, universes[1-player])
// fmt.Println(spaces)
if foundTheSupremeGalaticChampion {
break
}
player = 1 - player
for score := 0; score < MaxScore; score++ {
for space := 1; space <= MaxSpace; space++ {
universes[player][score][space] *= 27
}
}
}
fmt.Println(universes[0][MaxScore])
fmt.Println(universes[1][MaxScore])
result := uint64(0)
for space := 1; space <= MaxSpace; space++ {
result += universes[player][MaxScore][space]
}
return result
}
func main() {
// Uncomment this line to disable garbage collection
// debug.SetGCPercent(-1)
var input []byte
var err error
if len(os.Args) > 1 {
// Read input from file for local debugging
input, err = ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
// Remove extra newline
input = input[:len(input)-1]
} else {
// Read input from stdin
input, err = ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
}
// Start resolution
start := time.Now()
result := run(string(input))
// Print result
fmt.Printf("_duration:%f\n", time.Since(start).Seconds()*1000)
fmt.Println(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment