Golang gives different result everytime using map for AOC 2021 Day 6 problem

45 Views Asked by At

I've been trying to solve Advent of Code 2021 and in day 6, I am trying this solution but the result is different everytime. What seems to be the problem? Is there any memory leakage with map?

The input file can be found here The details of the problem can be read here

For part one it was straight-forward looping over arrays but as the number of days increases, the population grows exponentially and the time complexity grows in similar manner.

with go version go1.19.3 I have tried this:

package main

import (
    "fmt"
    "os"
    "strconv"
    "strings"
)

func getInput() []int {
    var parsedData []int
    rawData, _ := os.ReadFile("input.txt")
    data := strings.Split(string(rawData), ",")
    for _, strNum := range data {
        num, _ := strconv.Atoi(strNum)
        parsedData = append(parsedData, num)
    }
    return parsedData
}

func main() {
    data := getInput()
    var total int64
    // create a map t0 hold the number of fish with the same timer
    fishWithSameTimer := make(map[int]int64)
    for _, timer := range data {
        if _, ok := fishWithSameTimer[timer]; ok {
            fishWithSameTimer[timer] += 1
        } else {
            fishWithSameTimer[timer] = 1
        }
    }
    const days int = 18
    currDay := 1

    for currDay <= days {
        tempFishTimerData := make(map[int]int64)
        for timer, numOfFishes := range fishWithSameTimer {
            if timer == 0 {
                tempFishTimerData[8] = numOfFishes
                tempFishTimerData[6] = numOfFishes
            }else{ 
                tempFishTimerData[timer - 1] += numOfFishes
            }
        } 
        fishWithSameTimer = tempFishTimerData
        fmt.Println("Day:", currDay, fishWithSameTimer)
        currDay++
    }
    fmt.Println(fishWithSameTimer)
    for _, num := range fishWithSameTimer {
        total += num
    }
    fmt.Println(total)
}

Can anyone help?

1

There are 1 best solutions below

1
Suresh R On

I hope this piece of code does the work, please add the input file reading part and print the output slice as comma separated string. You can also validate if the input has all numbers between 0 and 8.

package main

import "fmt"

func refreshTimer(x int) (int, bool) {
    if x == 0 {
        return 6, true
    } else {
        return x - 1, false
    }
}

func spawnFish(i []int) []int {
    var parentIntTimer []int
    var childIntTimer []int
    for _, d := range i {
        y, c := refreshTimer(d)
        parentIntTimer = append(parentIntTimer, y)
        if c {
            childIntTimer = append(childIntTimer, 8)
        }
    }
    return append(parentIntTimer, childIntTimer...)
}

func main() {
    initialFishes := []int{3, 4, 3, 1, 2}
    var spFishes []int
    noOfDays := 18
    for i := 1; i <= noOfDays; i++ {
        spFishes = spawnFish(initialFishes)
        initialFishes = spFishes
    }
    fmt.Println(spFishes)
}

Output: [6 0 6 4 5 6 0 1 1 2 6 0 1 1 1 2 2 3 3 4 6 7 8 8 8 8]