This commit is contained in:
Karl Spickermann 2021-12-06 13:43:37 +01:00
parent 77fcd2c1f8
commit d582965b1c
3 changed files with 63 additions and 0 deletions

61
src/day6/day6.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"AOC2021/src/helper"
"fmt"
"os"
"strings"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
inputNumbers, _ := helper.MapToNumber(strings.Split(input[0], ","))
fishArray := [9]int{0, 0, 0, 0, 0, 0, 0, 0, 0}
for _, inputNumber := range inputNumbers {
fishArray[inputNumber] += 1
}
fmt.Println(fishArray)
simulateNDays(fishArray, 80)
simulateNDays(fishArray, 256)
}
func simulateNDays(fishArray [9]int, n int) {
i := 0
for i < n {
daysPassed := 0
fishArray, daysPassed = step(fishArray)
i += daysPassed
}
fmt.Println(getSum(fishArray))
}
func step(input [9]int) ([9]int, int) {
newArray := [9]int{0, 0, 0, 0, 0, 0, 0, 0, 0}
lowestTimerIndex := findIndexOfFirstNonzero(input)
amountCreatingFishes ,a1 := input[lowestTimerIndex] , input[lowestTimerIndex+1:]
copy(newArray[:], a1[:8-lowestTimerIndex])
newArray[8] += amountCreatingFishes
newArray[6] += amountCreatingFishes
return newArray, lowestTimerIndex + 1
}
func getSum(input [9]int) int{
sum := 0
for _,number := range input {
sum += number
}
return sum
}
func findIndexOfFirstNonzero(input [9]int) int {
for i, number := range input {
if number != 0 {
return i
}
}
return -1
}

1
src/day6/day6Input.txt Normal file
View File

@ -0,0 +1 @@
1,3,1,5,5,1,1,1,5,1,1,1,3,1,1,4,3,1,1,2,2,4,2,1,3,3,2,4,4,4,1,3,1,1,4,3,1,5,5,1,1,3,4,2,1,5,3,4,5,5,2,5,5,1,5,5,2,1,5,1,1,2,1,1,1,4,4,1,3,3,1,5,4,4,3,4,3,3,1,1,3,4,1,5,5,2,5,2,2,4,1,2,5,2,1,2,5,4,1,1,1,1,1,4,1,1,3,1,5,2,5,1,3,1,5,3,3,2,2,1,5,1,1,1,2,1,1,2,1,1,2,1,5,3,5,2,5,2,2,2,1,1,1,5,5,2,2,1,1,3,4,1,1,3,1,3,5,1,4,1,4,1,3,1,4,1,1,1,1,2,1,4,5,4,5,5,2,1,3,1,4,2,5,1,1,3,5,2,1,2,2,5,1,2,2,4,5,2,1,1,1,1,2,2,3,1,5,5,5,3,2,4,2,4,1,5,3,1,4,4,2,4,2,2,4,4,4,4,1,3,4,3,2,1,3,5,3,1,5,5,4,1,5,1,2,4,2,5,4,1,3,3,1,4,1,3,3,3,1,3,1,1,1,1,4,1,2,3,1,3,3,5,2,3,1,1,1,5,5,4,1,2,3,1,3,1,1,4,1,3,2,2,1,1,1,3,4,3,1,3

1
src/day6/day6Test.txt Normal file
View File

@ -0,0 +1 @@
3,4,3,1,2