Stream: advent of code

Topic: 2023 Day 3


view this post on Zulip John Murray (Dec 03 2023 at 06:26):

I'm not done yet but the array 2d lib by @Elias Mulhall is soo nice

view this post on Zulip Ayaz Hafiz (Dec 03 2023 at 06:50):

My solution

view this post on Zulip Ayaz Hafiz (Dec 03 2023 at 06:50):

I feel like I overcomplicated it, and should totally have used Elias's 2D lib, but... today I really felt like wow, Roc is nice language

view this post on Zulip Ayaz Hafiz (Dec 03 2023 at 06:51):

Things just worked (language server, dbg, expect) and even though I ran into some compiler bugs this is the best experience I've had writing Roc code since I joined the project

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 06:56):

So I decided to solve this in a really strange way. Not sure why...definitely would not recommend this code, but if you want to read a strange solution: https://github.com/bhansconnect/roc-aoc-2023/blob/main/day3.roc

view this post on Zulip Oskar Hahn (Dec 03 2023 at 07:53):

Here is mine: https://github.com/ostcar/aoc2023/blob/main/days/day03.roc

Todays Problems

view this post on Zulip Luke Boswell (Dec 03 2023 at 09:02):

This is my solution for Day 3. That was a little hairy in some parts.

I also feel like Roc is running really great. I haven't ran into any compiler bugs yet. :octopus:

view this post on Zulip Luke Boswell (Dec 03 2023 at 09:03):

I pretty much wrote the whole program using roc check and then roc test occasionally. When I finally wired it all together and it compiled, there were are couple of easy to debug logic bugs ( dbg :hearts:) and then it Just Worked™ which was super fun.

view this post on Zulip Luke Boswell (Dec 03 2023 at 09:09):

speed comparison between dev and --optimize

view this post on Zulip Johan Lindskogen (Dec 03 2023 at 10:07):

Still on P1... I bumped into several compiler bugs.. Then I finally got the right answer for the sample data! But it doesn't work for the input: https://github.com/lindskogen/advent-of-code-2023/blob/main/day03/main.roc

view this post on Zulip joshi (Dec 03 2023 at 12:04):

Huh, things run a lot faster if you put them into main instead of at the top level.. weird.

At least I'm no longer painfully, like 200x compared to Luke, slow (but still very slow)

https://gitlab.com/arkandos/aoc/-/blob/2023/solutions/day3.roc

timings

view this post on Zulip Richard Feldman (Dec 03 2023 at 12:11):

joshi said:

Huh, things run a lot faster if you put them into main instead of at the top level.. weird.

I think you're the first person to notice this in practice! :big_smile:

this is currently the case because today the way we compile top-level values is essentially that we turn them into functions, and then call those functions every time you reference the value. There's a plan to instead evaluate them at compile time (at which point there will be no performance difference to using them this way) but we haven't started on that project yet. :big_smile:

view this post on Zulip joshi (Dec 03 2023 at 12:22):

Well that makes sense that parsing the same file thousands of times might take a second or two :big_smile:

But so far it's great! Roc does almost everything I ever wanted in a programming language, and everyone here seems super friendly and welcoming! :smiling_face:

view this post on Zulip LoipesMas (Dec 03 2023 at 13:55):

I wrote all of the part 1 logic first and then moved onto parsing, which was an interesting change. I know it's not optimal (in terms of performance and dev speed), but it just feels so nice to parse the file to a structure and then just operate on that.
A classic off-by-one error got me stumped for a moment, because it only came up in the real input, but I managed to debug it. I've also spent some time trying to implement Eq for a custom type, but I managed to figure out how to do it by looking at the standard library.
Code: https://github.com/LoipesMas/roc-aoc2023/blob/main/3/main.roc
~30ms for part2, could probably be greatly improved with partitioning (instead of checking every * with every number...)

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 15:15):

Not sure if anyone else does this, but for working on AOC instead of using roc check or roc test, I now just change my main function to:

main =
    dbg (part1 p1Sample)
    Stout.line "done"

Enables me to just run the code and look at whatever intermediate value. Really nice repl like loop. Just see whatever value my current function generates and then add the next pipeline stage to my current function.

While doing things, I temporarily comment out the type signature for part1 so that it can return anything.

I probably should set this up with entr so it autoruns on file change.

view this post on Zulip Jean Niklas L'orange (Dec 03 2023 at 15:29):

Here's my solution for Day 3. Pain points today was Nat underflowing (it said overflow, which confused me for a second), and me using a previous state instead of updating the current one. It's a variant of the shadowing pain, kinda related to the threading issues already mentioned in #ideas > Shadowing & Redeclaration

Implementation trick

view this post on Zulip Elias Mulhall (Dec 03 2023 at 15:50):

Day 3, this one has a performance bug that I'll talk about below
Day 3, with a workaround

Performance Issue Details

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 16:06):

Interesting. I'll have to look at that set issue. We have something similar to anal::flat_hash_set, so it should generally be pretty fast. Though depending on the size wouldn't be surprised if skipping hashing and linearly scanning ends up being faster. But yeah, probably a bug.

view this post on Zulip Jason Hobbs (Dec 03 2023 at 22:03):

Elias Mulhall said:

Day 3, this one has a performance bug that I'll talk about below
Day 3, with a workaround

Performance Issue Details


Poor performance here too at 33s

And here's my code...

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 22:08):

Set.toList definitely is not fast currently, but I wouldn't expect to to totally ruin perf. Though I guess it depends on the size of the lists

https://github.com/roc-lang/roc/blob/a187d141bd881e3515133ee484fb8ffa1ba790e8/crates/compiler/builtins/roc/Dict.roc#L194

view this post on Zulip Asbjørn Olling (Dec 03 2023 at 22:08):

Alrighty, here's my day 3: https://gitlab.com/AsbjornOlling/aoc2023/-/blob/main/03/main.roc

Had to do a bit of a refactor for part2, but managed to reuse almost all of my code- and stuff just worked once roc dev stopped politely informing me that I was wrong about something.

Took some typing, but otherwise pretty painless day.

part2 spoilers

Probably not as performant as it could be, but hey. I'm happy with this.

view this post on Zulip Pearce Keesling (Dec 03 2023 at 22:35):

Really bad performance for my solutions today (takes about 10s to run both parts :grimacing:) https://github.com/keeslinp/AoC2023/blob/main/day3.roc still got both answers first try though which just shows how good the type system + expect workflow works.

I think I'm really starting to feel the pain of not having syntax highlighting setup correctly for my editor (I tried getting the treesitter stuff running but helix doesn't seem to actually display it, idk why). I kept getting a little lost, but thankfully hover from the LSP was super helpful trying to figure out how nested my lists were at any given point. Maybe a roc formatter would help me, I don't really know the best way to format things yet.

Also my solution crashes if I turn on optimize so maybe the performance wouldn't be so bad if it were on

view this post on Zulip Richard Feldman (Dec 03 2023 at 22:38):

roc format will format things for you! :smiley:

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 22:45):

For helix, make sure you also copy the queries over

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 22:45):

So add the grammar, build it, copy the queries so helix knows about them

view this post on Zulip Fabian Schmalzried (Dec 03 2023 at 22:51):

Here is also my solution:

code

Part 1 was pretty quick for me, but I could not reuse it for part 2, so quite some more typing for part 2. Performance is good, but I did run into a bug at one point. I will try to create a minimal example for it before opening a bug on github.

view this post on Zulip Axel (Dec 03 2023 at 23:26):

Late to the party, but here is mine (also just using Array2D, no perf problems :see_no_evil: )
The thing that bogs me down most is that dbg doesn't work during test runs (at leas on my Mac). It just terminates with [1] 85268 trace trap roc test

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 23:31):

should be fixed on main

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 23:32):

maybe even in the very latest nightly

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 23:32):

otherwise, should be in the next nightly

view this post on Zulip Fabian Schmalzried (Dec 03 2023 at 23:49):

Axel said:

The thing that bogs me down most is that dbg doesn't work during test runs (at leas on my Mac).

Yes, it would be nice to have dbg output on tests, is there a reason for why this is missing?

view this post on Zulip Richard Feldman (Dec 03 2023 at 23:52):

it used to work, but we recently changed how dbg works (to fix a bunch of bugs, among other benefits) and haven't updated tests to use that new way yet

view this post on Zulip Richard Feldman (Dec 03 2023 at 23:52):

@Folkert de Vries do you know offhand where we define roc_alloc and such for roc test?

view this post on Zulip Richard Feldman (Dec 03 2023 at 23:52):

I assume if we add a roc_dbg implementation there it'll fix it

view this post on Zulip Brendan Hansknecht (Dec 04 2023 at 00:20):

It should be fixed on main already

view this post on Zulip Jason Hobbs (Dec 04 2023 at 02:32):

Richard Feldman said:

roc format will format things for you! :smiley:

Not having this was such a pet peeve of mine as I've been coding lol

The whole time I've been thinking, we need to get on this for some sanity! Ty to everyone who put it together!

view this post on Zulip Ryan Bates (Dec 05 2023 at 01:02):

Here's my day 3 solution.
https://github.com/ryanb/advent-2023-roc/blob/main/day03/main.roc

view this post on Zulip Nikolaus Wettstein (Dec 06 2023 at 20:27):

Hi roc community! These are my solutions so far: https://github.com/ni-ko-o-kin/advent-of-code/tree/main/2023 I'm having so much fun with roc this year! A huge difference to last year where I ran into a couple of compiler bugs. And it also helped to let my brain process and digest for one year how roc works and how it differs from other languages I already know (like elm or haskell).

view this post on Zulip Jonas Schell (Jan 28 2024 at 15:24):

My solution for day 3: https://github.com/Ocupe/advent-of-code-2023/tree/main/day_03

I look forward to seeing how outers tackle this problem. I struggled with nested lists in pipes. Sometimes I didn't realize that I was still working on the first level where I should have used List.map on the nested list. I guess this is mostly due to my brain adapting to the functional style of doing things and getting used to Roc.

A lot of things took longer than I expected, but I also had some moments of "wow, this is so cool!". Amazing work!

view this post on Zulip Anton (Jan 28 2024 at 15:29):

I look forward to seeing how outers tackle this problem.

If you click on the header of this topic "# Advent of Code > 2023 Day 3" you can see the solutions posted in previous messages :)

view this post on Zulip Jonas Schell (Jan 28 2024 at 15:37):

Thanks @Anton, that is what I am currently doing. I just don't want to read anything related to the problem before I've solved it :)

view this post on Zulip Luke Boswell (Jan 28 2024 at 22:05):

Nice work, one thing I noticed I thought I might mention.

    # we can remove the braces
    |> (\numList -> { value: numList, row: hit.row, cols: range })

    # like this
    |> \numList -> { value: numList, row: hit.row, cols: range }

In general I wonder if roc format could/should do this automatically?

view this post on Zulip Isaac Van Doren (Jan 29 2024 at 19:55):

:100:


Last updated: Jul 06 2025 at 12:14 UTC