From 64e8954436f9a5ad2c68a421d9fd0273c0bebb04 Mon Sep 17 00:00:00 2001 From: kageru Date: Sat, 15 Dec 2018 07:51:34 +0100 Subject: [PATCH] (Go) day 14 part 1 done --- 14/main.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 14/main.go diff --git a/14/main.go b/14/main.go new file mode 100644 index 0000000..0e41825 --- /dev/null +++ b/14/main.go @@ -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:]) +}