(Go) day 14 part 1 done

This commit is contained in:
kageru 2018-12-15 07:51:34 +01:00
parent 9c78e7db28
commit 64e8954436
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

29
14/main.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"fmt"
)
func main() {
const turns = 919901
// passing turns as the length here doesn’t work because it initializes 1 million bytes as 0 instead of allocating space so I can append later.
// RIP performance, but I consider this way more readable than the alternativ.
recipes := make([]byte, 0)
recipes = append(recipes, 3)
recipes = append(recipes, 7)
e1, e2 := 0, 1
for len(recipes) < turns + 10 {
firstRecipe, secondRecipe := recipes[e1], recipes[e2]
newRecipe := firstRecipe + secondRecipe
if newRecipe >= 10 {
recipes = append(recipes, 1)
newRecipe -= 10
}
recipes = append(recipes, newRecipe)
e1 = (e1 + int(firstRecipe) + 1) % len(recipes)
e2 = (e2 + int(secondRecipe) + 1) % len(recipes)
}
fmt.Println(recipes[turns:])
}