1

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?

Kanuor
  • 55
  • 5
  • 4
    In Go maps are unordered by design. See https://go.dev/blog/maps#iteration-order – mkopriva Feb 19 '23 at 16:41
  • 1
    obviously there's no way for a hashmap to be ordered unless you store the indices separately – phuclv Feb 19 '23 at 17:12
  • I was well aware of this unordered design. Turns out I missed a "+" before resetting the timer to 6. But thanks to these comments I realized the bug. – Kanuor Feb 20 '23 at 07:04

1 Answers1

0

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]

Suresh R
  • 1
  • 2
  • Hi, thanks for helping but since the population grows exponentially using slice to store each fish timer would take a long time for the program to finish. That's why I used map but I think it's the unordered design of hashmap is causing the error. – Kanuor Feb 20 '23 at 06:47