(Go) day 14 part 2 done

This commit is contained in:
kageru 2018-12-15 08:10:55 +01:00
parent 64e8954436
commit 8da4dc79bc
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 50 additions and 41 deletions

49
14/main2.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
)
func completesTargetSequence(newElement byte) bool {
if target[foundDigits] == newElement {
foundDigits++
if foundDigits == len(target) {
return true
}
} else {
foundDigits = 0
}
return false
}
var target = [...]byte{9, 1, 9, 9, 0, 1}
var foundDigits = 0
func main() {
// 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 true {
firstRecipe, secondRecipe := recipes[e1], recipes[e2]
newRecipe := firstRecipe + secondRecipe
if newRecipe >= 10 {
recipes = append(recipes, 1)
newRecipe -= 10
if completesTargetSequence(1) {
break
}
}
recipes = append(recipes, newRecipe)
if completesTargetSequence(newRecipe) {
break
}
e1 = (e1 + int(firstRecipe) + 1) % len(recipes)
e2 = (e2 + int(secondRecipe) + 1) % len(recipes)
}
fmt.Println(len(recipes) - len(target))
}

View File

@ -1,40 +0,0 @@
--- Day 14: Chocolate Charts ---
You finally have a chance to look at all of the produce moving around. Chocolate, cinnamon, mint, chili peppers, nutmeg, vanilla... the Elves must be growing these plants to make hot chocolate! As you realize this, you hear a conversation in the distance. When you go to investigate, you discover two Elves in what appears to be a makeshift underground kitchen/laboratory.
The Elves are trying to come up with the ultimate hot chocolate recipe; they're even maintaining a scoreboard which tracks the quality score (0-9) of each recipe.
Only two recipes are on the board: the first recipe got a score of 3, the second, 7. Each of the two Elves has a current recipe: the first Elf starts with the first recipe, and the second Elf starts with the second recipe.
To create new recipes, the two Elves combine their current recipes. This creates new recipes from the digits of the sum of the current recipes' scores. With the current recipes' scores of 3 and 7, their sum is 10, and so two new recipes would be created: the first with score 1 and the second with score 0. If the current recipes' scores were 2 and 3, the sum, 5, would only create one recipe (with a score of 5) with its single digit.
The new recipes are added to the end of the scoreboard in the order they are created. So, after the first round, the scoreboard is 3, 7, 1, 0.
After all new recipes are added to the scoreboard, each Elf picks a new current recipe. To do this, the Elf steps forward through the scoreboard a number of recipes equal to 1 plus the score of their current recipe. So, after the first round, the first Elf moves forward 1 + 3 = 4 times, while the second Elf moves forward 1 + 7 = 8 times. If they run out of recipes, they loop back around to the beginning. After the first round, both Elves happen to loop around until they land on the same recipe that they had in the beginning; in general, they will move to different recipes.
Drawing the first Elf as parentheses and the second Elf as square brackets, they continue this process:
(3)[7]
(3)[7] 1 0
3 7 1 [0](1) 0
3 7 1 0 [1] 0 (1)
(3) 7 1 0 1 0 [1] 2
3 7 1 0 (1) 0 1 2 [4]
3 7 1 [0] 1 0 (1) 2 4 5
3 7 1 0 [1] 0 1 2 (4) 5 1
3 (7) 1 0 1 0 [1] 2 4 5 1 5
3 7 1 0 1 0 1 2 [4](5) 1 5 8
3 (7) 1 0 1 0 1 2 4 5 1 5 8 [9]
3 7 1 0 1 0 1 [2] 4 (5) 1 5 8 9 1 6
3 7 1 0 1 0 1 2 4 5 [1] 5 8 9 1 (6) 7
3 7 1 0 (1) 0 1 2 4 5 1 5 [8] 9 1 6 7 7
3 7 [1] 0 1 0 (1) 2 4 5 1 5 8 9 1 6 7 7 9
3 7 1 0 [1] 0 1 2 (4) 5 1 5 8 9 1 6 7 7 9 2
The Elves think their skill will improve after making a few recipes (your puzzle input). However, that could take ages; you can speed this up considerably by identifying the scores of the ten recipes after that. For example:
If the Elves think their skill will improve after making 9 recipes, the scores of the ten recipes after the first nine on the scoreboard would be 5158916779 (highlighted in the last line of the diagram).
After 5 recipes, the scores of the next ten would be 0124515891.
After 18 recipes, the scores of the next ten would be 9251071085.
After 2018 recipes, the scores of the next ten would be 5941429882.
What are the scores of the ten recipes immediately after the number of recipes in your puzzle input?
Your puzzle input is 919901.

View File

@ -5,7 +5,7 @@
| Lang. | 1 | 2 | 3 | 4 | 5 |
|:-------|---|---|---|---|---|
| C | x | x | x | x |(x)|
| Go | x | x | | | |
| Go | x | x | x | | |
| Kotlin | x | x | x | | |
| Python | x | | | | |
| Rust | x | x | | | |