This is even better, but still not fast enough.
This commit is contained in:
parent
5d4757d4cb
commit
bab5f879b0
|
@ -12,13 +12,14 @@ const NumGenerations = 50000000000
|
||||||
func main() {
|
func main() {
|
||||||
data := util.ReadInput()
|
data := util.ReadInput()
|
||||||
plants, rules := day12.FastParseInput(data)
|
plants, rules := day12.FastParseInput(data)
|
||||||
rootIndex := 0
|
|
||||||
|
|
||||||
|
runner := day12.FastGenerationRunner()
|
||||||
|
rootIndex := 0
|
||||||
for i := 0; i < NumGenerations; i++ {
|
for i := 0; i < NumGenerations; i++ {
|
||||||
plants, rootIndex = day12.FastRunGeneration(plants, rules, rootIndex)
|
plants, rootIndex = runner(plants, rules, rootIndex)
|
||||||
|
|
||||||
// debug
|
// debug
|
||||||
if i%1000000 == 0 {
|
if i%1000000000 == 0 {
|
||||||
fmt.Println("DEBUG: Generation: ", i)
|
fmt.Println("DEBUG: Generation: ", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,39 @@
|
||||||
|
// Unused solution: still not fast enough
|
||||||
package day12
|
package day12
|
||||||
|
|
||||||
func FastRunGeneration(plants []byte, rules map[string]byte, rootIndex int) ([]byte, int) {
|
func FastGenerationRunner() func([]byte, map[string]byte, int) ([]byte, int) {
|
||||||
newGen := make([]byte, len(plants), len(plants)+4)
|
cache := make(map[string]string)
|
||||||
|
|
||||||
for i := 0; i < len(plants); i++ {
|
return func(current []byte, rules map[string]byte, rootIndex int) ([]byte, int) {
|
||||||
newGen[i] = rules[getKey(plants, i)]
|
// check the cache
|
||||||
|
if cache[string(current)] != "" {
|
||||||
|
return []byte(cache[string(current)]), rootIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
newGen := make([]byte, len(current), len(current)+4)
|
||||||
|
|
||||||
|
for i := 0; i < len(current); i++ {
|
||||||
|
newGen[i] = rules[getKey(current, i)]
|
||||||
|
}
|
||||||
|
|
||||||
|
result1 := rules[getKey(current, -2)]
|
||||||
|
result2 := rules[getKey(current, -1)]
|
||||||
|
if result1 == '#' || result2 == '#' {
|
||||||
|
newGen = append([]byte{result1, result2}, newGen...)
|
||||||
|
rootIndex += 2
|
||||||
|
}
|
||||||
|
|
||||||
|
result1 = rules[getKey(current, len(current))]
|
||||||
|
result2 = rules[getKey(current, len(current)+1)]
|
||||||
|
if result1 == '#' || result2 == '#' {
|
||||||
|
newGen = append(newGen, result1, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cache result
|
||||||
|
cache[string(current)] = string(newGen)
|
||||||
|
|
||||||
|
return newGen, rootIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
result1 := rules[getKey(plants, -2)]
|
|
||||||
result2 := rules[getKey(plants, -1)]
|
|
||||||
if result1 == '#' || result2 == '#' {
|
|
||||||
newGen = append([]byte{result1, result2}, newGen...)
|
|
||||||
rootIndex += 2
|
|
||||||
}
|
|
||||||
|
|
||||||
result1 = rules[getKey(plants, len(plants))]
|
|
||||||
result2 = rules[getKey(plants, len(plants)+1)]
|
|
||||||
if result1 == '#' || result2 == '#' {
|
|
||||||
newGen = append(newGen, result1, result2)
|
|
||||||
}
|
|
||||||
|
|
||||||
return newGen, rootIndex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getKey(plants []byte, i int) string {
|
func getKey(plants []byte, i int) string {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// This works fine for 12.1, but it's way too slow for 12.2
|
||||||
package day12
|
package day12
|
||||||
|
|
||||||
type Rule struct {
|
type Rule struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user