Stream: advent of code

Topic: 2024 Day 1


view this post on Zulip Luke Boswell (Dec 01 2024 at 05:26):

Here's my solution :smiley:

https://github.com/lukewilliamboswell/aoc/blob/2024/2024/01.roc

TIMING:
READING INPUT:  <1ms
SOLVING PART 1: <1ms
SOLVING PART 2: 1ms

view this post on Zulip Oskar Hahn (Dec 01 2024 at 05:30):

And here is mine: https://github.com/ostcar/aoc2024/blob/main/day01.roc

view this post on Zulip Luke Boswell (Dec 01 2024 at 05:31):

I like your diffing impl

view this post on Zulip Isaac Van Doren (Dec 01 2024 at 06:01):

https://github.com/isaacvando/aoc/blob/main/2024/1.roc :evergreen_tree:

view this post on Zulip Robin Camarasa (Dec 01 2024 at 06:12):

Probably not the most elegant but it works :smile: https://github.com/RobinCamarasa/Advent-of-code/blob/master/2024/day01/RobinCamarasa/main.roc

view this post on Zulip Eli Dowling (Dec 01 2024 at 10:28):

Got it done too, after a few hours of submitting PRs and debugging things :joy:
https://github.com/faldor20/aoc-template/blob/purity-inference/examples/2024/01.roc

view this post on Zulip drew (Dec 01 2024 at 15:26):

am i allowed to post non-roc code :smile:

view this post on Zulip Hardy (Dec 01 2024 at 16:43):

Here my solution: https://github.com/Hardy7cc/aoc/blob/main/2024/01.roc
But watch out I don't understand why the part2 "Ok String" return is crashing. For AoC it is working good enough with returning an "Err", after all I got expected results.

view this post on Zulip Eli Dowling (Dec 01 2024 at 16:45):

drew said:

am i allowed to post non-roc code :)

So long as it's not in a language that makes me want to weep with boredom :sweat_smile:

view this post on Zulip jan kili (Dec 01 2024 at 18:15):

Should we post AoC-inspired bugs / compiler improvement ideas here or in #bugs ?

view this post on Zulip Ryan Barth (Dec 01 2024 at 18:35):

My late entry: https://github.com/r-bar/advent24

view this post on Zulip Brendan Hansknecht (Dec 01 2024 at 18:36):

Probably in #bugs. Though please be careful not to leak AOC solutions directly in #bugs. Make sure they are behind a link and directly labelled as AOC. Or even better, make a repro that doesn't spoil AOC.

view this post on Zulip jan kili (Dec 01 2024 at 18:39):

my GOSH doing day 1 is reminding me how much I love coding in Roc

view this post on Zulip Ghislain (Dec 01 2024 at 18:39):

Here's mine https://github.com/ghigt/advent-of-code/blob/main/2024/roc/01.roc

Feedback on the feeling on writing Roc code:

(These are my small frustrations, thank you for all the work, it's a much better experience than the previous years!)

view this post on Zulip Brendan Hansknecht (Dec 01 2024 at 18:53):

Any extra detail on the dbg comment? Anything that would make it more friendly?

view this post on Zulip Ghislain (Dec 01 2024 at 20:18):

I used it inside a pipeline and it was a bit hard to parse the output because it contained all the pipeline as variable name before =.

✗ roc test 01.roc
[01.roc:53] map.0
    |> List.map \n ->
        map.1
        |> List.keepIf \nn -> nn == n
        |> List.len
        |> Num.toI32
        |> Num.mul n = [9, 4, 0, 0, 9, 9]
0 failed and 2 passed in 529 ms.

It may be only me and my use case but I would have prefered only the line of file for a more concise and simpler output (even more when it's inside a loop). I may be wrong and don't see all the use cases it allows though.

view this post on Zulip Johannes (Dec 01 2024 at 20:54):

It took me hours to get it running :sweat_smile:. Sharing the code would be even more embarrassing :big_smile: , but hey, it was super delightful using Roc, I learned a lot and can’t wait for day 02. :heart_kiss:

view this post on Zulip drew (Dec 01 2024 at 23:44):

here's day 1 in gleam :stuck_out_tongue: but i'll probably do tomorrow in roc

view this post on Zulip drew (Dec 01 2024 at 23:44):

https://github.com/drewolson/aoc-gleam/blob/main/src/aoc/year2024/day01.gleam

view this post on Zulip drew (Dec 02 2024 at 00:16):

here's my day 1 roc https://github.com/drewolson/aoc-roc/blob/main/Aoc/Year2024/Day01.roc

view this post on Zulip Eli Dowling (Dec 02 2024 at 00:25):

Ghislain said:

Hey, I agree that's a pain, but I think I finally fixed all the language server crashes in this pr:
https://github.com/roc-lang/roc/pull/7288
Hopefully it'll be merged very soon and you will be sorted :)

view this post on Zulip drew (Dec 02 2024 at 00:38):

yep that’ll be awesome if it merges

view this post on Zulip Eli Dowling (Dec 02 2024 at 03:32):

@johannes @drew I his is merged! Hopefully you can update and day 2 will be way better!

view this post on Zulip Luke Boswell (Dec 02 2024 at 03:33):

Thank you Eli for fixing that

view this post on Zulip drew (Dec 02 2024 at 03:37):

awesome! hopefully a new release is cut soon!

view this post on Zulip drew (Dec 02 2024 at 03:37):

looking forward to using it

view this post on Zulip jan kili (Dec 02 2024 at 07:03):

:partying_face:
https://gitlab.com/JanCVanB/aoc-2024/-/blob/main/day-01/part-1/main.roc
https://gitlab.com/JanCVanB/aoc-2024/-/blob/main/day-01/part-2/main.roc

view this post on Zulip jan kili (Dec 02 2024 at 07:28):

This is my first time using expect. :loudspeaker: Roc has better TDD flow than I've ever felt in other languages. I haven't used other newer or FP-centric languages, though.

view this post on Zulip jan kili (Dec 02 2024 at 07:30):

The only things I tripped over were some awful error messages when getting my first main set up, like these:
Screenshot from 2024-12-01 11-14-07.png
Screenshot from 2024-12-01 11-17-07.png

view this post on Zulip Luke Boswell (Dec 02 2024 at 07:53):

What's wrong with that fiest message? Looks ok to me

view this post on Zulip jan kili (Dec 02 2024 at 07:57):

"Something is off with the body of this suffixed statement" instead of something like "Please assign the return value of this valid mid-function expression to a variable"
(dismissed by adding foo = in front of that line or adding |> foo on the next line)
posted in https://roc.zulipchat.com/#narrow/channel/463736-bugs/topic/Lame.20error.20message.20-.20.22Something.20is.20off.22/near/485566360

view this post on Zulip Eli Dowling (Dec 02 2024 at 08:11):

(deleted)

view this post on Zulip Anton (Dec 02 2024 at 11:16):

awesome! hopefully a new release is cut soon!

done :)

view this post on Zulip drew (Dec 02 2024 at 13:09):

with the new release, i no longer have LSP errors on my files, but the LSP seems to now do nothing on these files. no hovers, no autocompletion, etc

view this post on Zulip Anton (Dec 02 2024 at 13:10):

Can you see if there any useful messages in the logs?

view this post on Zulip drew (Dec 02 2024 at 13:42):

looking

view this post on Zulip drew (Dec 02 2024 at 13:43):

i see a lot of this:

view this post on Zulip drew (Dec 02 2024 at 13:43):

[ERROR][2024-12-02 07:42:55] .../lean/stderr.lua:80 "rpc" "/Users/drew/.local/bin/roc_language_server"  "stderr"  "thread 'tokio-runt       ime-worker' panicked at crates/language_server/src/analysis.rs:301:71:\ncalled `Option::unwrap()` on a `None` value\nnote: run with `       RUST_BACKTRACE=1` environment variable to display a backtrace\n"

view this post on Zulip Anton (Dec 02 2024 at 13:56):

analysis.rs:301 is also involved here, can you add your code to that issue? That way we can make sure a future fix works for both cases.

view this post on Zulip drew (Dec 02 2024 at 14:22):

done

view this post on Zulip Eli Dowling (Dec 02 2024 at 15:01):

So if the compiler is crashing there will be no errors until whatever is crashing the compiler is fixed. But that also looks like a different issue which I can definitely fix

view this post on Zulip Eli Dowling (Dec 02 2024 at 15:01):

As in, you make a code change to no longer make your code crash the compiler

view this post on Zulip drew (Dec 02 2024 at 15:01):

i'm in neovim, :LspInfo shows the LSP still running

view this post on Zulip drew (Dec 02 2024 at 15:02):

so it seems attached to the buffer

view this post on Zulip drew (Dec 02 2024 at 15:02):

but i'm getting no interactive behavior

view this post on Zulip drew (Dec 02 2024 at 15:02):

interactivity does indeed work in main.roc

view this post on Zulip drew (Dec 02 2024 at 15:03):

anecdotally, it seems to work on singly-nested modules but not doubly-nested

view this post on Zulip drew (Dec 02 2024 at 15:04):

scratch that, i don't know why it sometimes works

view this post on Zulip drew (Dec 02 2024 at 15:04):

things still work in the Aoc/Runner.roc file for me

view this post on Zulip drew (Dec 02 2024 at 15:04):

but no other nested files

view this post on Zulip drew (Dec 02 2024 at 15:04):

ah, wait, it also works on the Aoc/Runner/*.roc files. i am at a loss as to why

view this post on Zulip Eli Dowling (Dec 02 2024 at 15:29):

@drew if you send me a link to your repo I'll try to reproduce and fix this tomorrow:)

view this post on Zulip drew (Dec 02 2024 at 15:30):

https://github.com/drewolson/aoc-roc

view this post on Zulip Eli Dowling (Dec 02 2024 at 15:47):

okayyy, This is much harder than an unwrap. I think you'll find if you run roc check against the files that are causing the lsp to not work, they will also crash. I'd be extremely surprised if this is new as of my latest changes to the language server

view this post on Zulip Eli Dowling (Dec 02 2024 at 16:28):

Using the debug build I was able to get some more info. It's related to not have dependencies resolved by the time we expect them to be. The weird thing is that the dependencies are form List.roc which has no dependencies.

view this post on Zulip Anton (Dec 03 2024 at 17:57):

I let claude do all the work, it is getting impressively good at Roc :mind_blown:

Literally the only things I had to fix:

claude-solution.roc
prompt.txt

For the prompt I just gave it three examples from AoC last year and a list of commonly used Roc functions.

view this post on Zulip Ryan Barth (Dec 03 2024 at 19:16):

Anton said:

I let claude do all the work, it is getting impressively good at Roc :m

The way you pass in all the function signatures in the context reminds me of that Mirror language that was doing the rounds on hackernews a couple weeks ago. Pretty cool it got so close.

view this post on Zulip Al S-M (Dec 03 2024 at 21:09):

https://github.com/alsm/aoc-roc/blob/master/2024/01.roc
Literally the first thing I've written in roc so if anyone has any time to look at it and give any hints on things I could improve or should do differently I'd appreciate it, but either way happy just to make it work :D

view this post on Zulip Anton (Dec 04 2024 at 10:13):

... reminds me of that Mirror language ...

Interesting!

view this post on Zulip Norbert Hajagos (Dec 11 2024 at 12:34):

Al S-M said:

https://github.com/alsm/aoc-roc/blob/master/2024/01.roc
Literally the first thing I've written in roc so if anyone has any time to look at it and give any hints on things I could improve or should do differently I'd appreciate it, but either way happy just to make it work :D

Hi! I'll gladly do a quick review for you:
IIRC as a convenience, you can use |> List.map .first instead of |> List.map (\x -> x.first). Kinda like how you can replace List.map2 left right (\a, b -> Num.absDiff a b) with List.map2 left right Num.absDiff

To my taste, this line is getting quiet long, breaking it up would make it more readable
Ok (Num.toStr (List.map nums (\x -> x.first * Result.withDefault (Dict.get similarity x.first) 0) |> List.sum))

If you've just began learning Roc, you shouldn't really worry about this, but and interesting thing about consider:

        nums
        |> List.map (\x -> x.second)
        |> Set.fromList
        |> Set.toList
        |> List.map (\x -> (x, List.countIf nums (\y -> y.second == x)))

this snippet transforms the nums list a bunch of times. Now, I don't know what happens to the list under the hood, when you turn it into a set and back, so let's ignore that part. It's likely makes my advice on optimization irrelevant in this case, but in general, if you transform a list a bunch, it's a good idea to find a solution where you don't have to reuse the original list later on. If you can do that, the reference count of the list will stay 1, so it can be mutated in place (we call it opportunistic mutation), instead of coppied.
If you've used rust or a language with move semantics, that is the style the roc optimizer likes! But ofc, it can be a lot of hassle.
Congrats on your first Roc code! I didn't even know Num.absDiff was a thing :big_smile:

view this post on Zulip Norbert Hajagos (Dec 11 2024 at 12:41):

Separately:

Here is my solution: https://github.com/HajagosNorbert/advent-of-code-2024/blob/a4b36172966dd519e107d3a395cb5cc6efdc41d5/Day1.roc

Posting cuz part2 solution can be interesting. Using 1 Dict to store the left and right occurences in a tuple, then walking the dict to sum up the key*value.0*value.1.


Last updated: Jul 06 2025 at 12:14 UTC