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
}