MKXM72ZLPXJP4MI4RQ3JBPOL32PUWZFB7R2ABPQVEYXGCPWS4UXQC
package main
import (
"io"
"log"
"os"
"strconv"
"strings"
)
const (
AGES = 9
)
func main() {
if err := myMain(); err != nil {
log.Println(err)
}
}
func myMain() error {
// parse input.txt
lanterns, err := parseInput("input.txt")
if err != nil {
return err
}
log.Println(solveFirst(lanterns, 80))
log.Println(solveFirst(lanterns, 256))
return nil
}
func solveFirst(lf []int, days int) int {
age := lanternage(lf)
for i := 0; i < days; i++ {
new := age[0]
age[0] = age[1]
age[1] = age[2]
age[2] = age[3]
age[3] = age[4]
age[4] = age[5]
age[5] = age[6]
age[6] = age[7] + new
age[7] = age[8]
age[8] = new
}
return sum(age)
}
func sum(in [AGES]int) int {
var sum int
for _, item := range in {
sum += item
}
return sum
}
func lanternage(lf []int) [AGES]int {
var ret [AGES]int
for _, fish := range lf {
ret[fish] += 1
}
return ret
}
func parseInput(fileName string) ([]int, error) {
var ret []int
fd, err := os.Open(fileName)
if err != nil {
return ret, err
}
defer fd.Close()
input, err := io.ReadAll(fd)
if err != nil {
return nil, err
}
instr := strings.Split(strings.TrimSuffix(string(input), "\n"), ",")
for _, elem := range instr {
tmp, err := strconv.Atoi(elem)
if err != nil {
return nil, err
}
ret = append(ret, tmp)
}
return ret, nil
}
4,1,4,1,3,3,1,4,3,3,2,1,1,3,5,1,3,5,2,5,1,5,5,1,3,2,5,3,1,3,4,2,3,2,3,3,2,1,5,4,1,1,1,2,1,4,4,4,2,1,2,1,5,1,5,1,2,1,4,4,5,3,3,4,1,4,4,2,1,4,4,3,5,2,5,4,1,5,1,1,1,4,5,3,4,3,4,2,2,2,2,4,5,3,5,2,4,2,3,4,1,4,4,1,4,5,3,4,2,2,2,4,3,3,3,3,4,2,1,2,5,5,3,2,3,5,5,5,4,4,5,5,4,3,4,1,5,1,3,4,4,1,3,1,3,1,1,2,4,5,3,1,2,4,3,3,5,4,4,5,4,1,3,1,1,4,4,4,4,3,4,3,1,4,5,1,2,4,3,5,1,1,2,1,1,5,4,2,1,5,4,5,2,4,4,1,5,2,2,5,3,3,2,3,1,5,5,5,4,3,1,1,5,1,4,5,2,1,3,1,2,4,4,1,1,2,5,3,1,5,2,4,5,1,2,3,1,2,2,1,2,2,1,4,1,3,4,2,1,1,5,4,1,5,4,4,3,1,3,3,1,1,3,3,4,2,3,4,2,3,1,4,1,5,3,1,1,5,3,2,3,5,1,3,1,1,3,5,1,5,1,1,3,1,1,1,1,3,3,1
3,4,3,1,2
module main
go 1.17