Stream: advent of code

Topic: Day 12


view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 00:43):

It took me way too long to realize that the problem is not in my code but Dict is broken :smiling_face_with_tear:

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 02:17):

Any more details on how it is broken or a minimal repro if you have/can make one?

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 04:09):

Min-repro’ing this is going to be really hard. My code works for the “example input”, and on the bigger input it crashes after many many iterations with an unwrap of KeyNotFound on a dict that IIUC has that key in there. https://github.com/shritesh/advent/blob/4995ae8d8bdc4d0ea11edce9555ccccc27707324/2022/12.roc#L36

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 04:12):

Is see. Yeah, may be quite hard to debug. Does dbg printing Dict.keys show the key?

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 04:12):

That will end up printing the underlying list. Would let us know if it is simply a bug in the hash lookup or if it is something else.

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 04:19):

In the latest Apple Silicon nightly, running roc dev 12.roc simply exits without doing anything if I put a dbg before the unwrap

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 04:20):

ok. sad

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 04:57):

Interesting. I can't seem to repro. It still fails, but it is hitting a integer addition overflowed! in the helper function Looks to be happening directly in the newHops List.walk lambda. (note, I am running from source, not from a nightly)

Oh, actually. it is specifically hitting it on alt = currentHop + 1. If I change that to alt = Num.addWrap currentHop 1, the code you linked will pass for me and print out a result.

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 14:20):

nice. roc dev was taking too long for the full input so I was using optimize. Is integer overflow an undefined behavior in optimized roc programs?

view this post on Zulip Folkert de Vries (Dec 13 2022 at 14:23):

no we specifically do want it to panic in release builds

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 14:33):

gotcha. I can repro different behaviors in the file linked above where roc run crashes with overflow while roc run --optimize essentially shows undefined behavior.

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 15:46):

Just a note, roc dev --optimize works. So you can have both

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 16:07):

Huh. Only roc run --optimize manifests the "undefined behavior". roc dev, roc dev --optimize and roc run all result in the integer overflow.

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 16:14):

That's quite intriguing. Definitely something to investigate

view this post on Zulip Richard Feldman (Dec 13 2022 at 16:46):

seems likely to be unrelated

view this post on Zulip Richard Feldman (Dec 13 2022 at 16:47):

like there's an --optimize bug unrelated to overflow that's causing things to break in a way that means the overflow doesn't happen

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 16:51):

But only in run --optimize and not dev --optimize?

view this post on Zulip Brendan Hansknecht (Dec 13 2022 at 16:52):

Yeah, i see. I guess it means there is a compiler bug. Then there is also the bug with the example shared

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 18:58):

Ok so this is definitely a compiler bug: I fixed the integer overflow error in my code. roc run, roc dev and roc dev --optimize all return the correct result. roc run --optimize crashes at that exact same point as before. https://github.com/shritesh/advent/blob/d021f18788fd46b12b485df7932fc5c8315c4e55/2022/12.roc

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 19:02):

moar: roc build --optimize is broken as well

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 19:04):

even moar: the same holds true for --opt-size instead of --optimize for all the commands.

view this post on Zulip Shritesh Bhattarai (Dec 13 2022 at 19:27):

Opened up an issue: https://github.com/roc-lang/roc/issues/4756

view this post on Zulip Shritesh Bhattarai (Dec 14 2022 at 15:53):

Anyway, here's my final solution for Day 12. I'm extremely pleased with the result. Implementing Dijkstra's in Roc forced me to truly internalize the algorithm while my previous understanding was a data structure centric one: "use a minimum priority queue, mark nodes as visited, etc.". Roc is turning into my favorite programming language by the day.


Last updated: Jul 06 2025 at 12:14 UTC