#Day 5 part 2

1 messages ยท Page 1 of 1 (latest)

muted orbit
#

I just cant find my issue what i've done wrong with that brute-force approach. It does work for the example input but it does not work for that main input, i just get a wrong result.

func part2() int {
    digits := getDigits()

    var smallestSeed = 999999999999
    var seedRanges = strings.Split(data[0], " ")

    for index := 1; index < len(seedRanges); index += 2 {
        value, err := strconv.Atoi(seedRanges[index])
        value2, err2 := strconv.Atoi(seedRanges[index+1])
        if err != nil && err2 != nil {
            continue
        }
        for rangeIndex := 0; rangeIndex < value2; rangeIndex++ {
            var seed = value + rangeIndex

            for section := 0; section < len(digits); section++ {
                for index := 0; index < len(digits[section]); index += 3 {
                    var source = digits[section][index+1]
                    var destination = digits[section][index]
                    var length = digits[section][index+2]

                    if source <= seed && source+length >= seed {
                        var difference = seed - source

                        seed = destination + difference
                        break
                    }
                }
            }
            if seed < smallestSeed {
                smallestSeed = seed
            }
        }
    }
    return smallestSeed
}
late siloBOT
#

<@&987246964494204979> please have a look, thanks.

granite marsh
#

If you're using the brute force approach it's the same as part 1, but you just iterate over the list of all possible seeds

so if you're getting the right answer for part 1, there must be something missing in the way you're iterating over the seeds? ๐Ÿค”

can you share part 1 to take a look at?

muted orbit
#

Sure thats part 1: ```go
func part1() int {
seeds := getSeeds()
digits := getDigits()

for seedIndex := range seeds {
    for section := 0; section < len(digits); section++ {
        for index := 0; index < len(digits[section]); index += 3 {
            var source = digits[section][index+1]
            var destination = digits[section][index]
            var length = digits[section][index+2]

            if source <= seeds[seedIndex] && source+length >= seeds[seedIndex] {
                var difference = seeds[seedIndex] - source

                seeds[seedIndex] = destination + difference
                break
            }
        }
    }
}

return slices.Min(seeds)

}
func getSeeds() []int {
var seeds []int

for _, num := range strings.Split(data[0], " ") {
    value, err := strconv.Atoi(num)
    if err != nil {
        continue
    }
    seeds = append(seeds, value)
}

return seeds

}```

muted orbit
granite marsh
#

one thing to check is that you seem to be using int

some of the ranges of my seeds started at 3,284,226,943 so you will be overflowing when you start to increment your loops?

muted orbit
#

Yeah i did think about that but then i wonder why did it solved part 1 with same overflowing numbers?

granite marsh
#

yee ignore me - i think it might be a red herring

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems

muted orbit
#

Could you maybe try out my input and say me the lowest value for each range? Could be that there is just some special case in one of the ranges

#

Nah nvm, wont bring me forward

#

Since the ranges still contain way to much data xd

granite marsh
#

well the one thing i did when i was debugging a little was to add up the length of all of my ranges, so I knew how many seeds I should have based on the input

#

then a naive way to check is literally just run your loops and increment a counter and make sure they match ๐Ÿ˜…

muted orbit
#

I mean it does work for the example output, so if i would go the main input line by line until it match it could be thousends of lines i will have to check until i find that special case. I did also checked other solutions and my solution does have pretty much the same logic

granite marsh
#

I got your code from GH just gonna try running it against my input to see the diff ๐Ÿ˜‰

#

I can give you a hint, and it might help you to pinpoint what is wrong?

muted orbit
#

Uh sure, that could maybe help

#

Imagine it will work for you pepekek

granite marsh
#

on my input, you are off by 1 on the full range ๐Ÿ™ˆ

muted orbit
#

BY 1 NootLikeThis

granite marsh
#

I guess that's not really a hint as you can pass it now, but i won't tell you which direction ๐Ÿ˜„

muted orbit
#

You are joking right

#

How the hell can it be off by one

muted orbit
granite marsh
muted orbit
#

IT WAS REALLY just - 1 the right result

#

It had me frustrated for hours, i started with this puzzle today morning at around 10am and now its 5pm pepe_sad

#

But well i also had to learn Go, since i never used that lang before pepekek

#

Well thanks a lot for you help, without you i would prob still sit here until 2am next day @granite marsh peepo_heart

granite marsh