Stream: beginners

Topic: Advent of Code 2022


view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Nov 25 2022 at 23:49):

hello, I'm new here but I'm around for a while now, I was looking to find a good and new function programming language that give the possiblity to me to give a shoit with functional languages aftet the bad experience in College.

I found Roc, and I said why not contribute in a functional programming language? :smug_cat: shoudl be fun, but before I would like to use for a bit the language

So I was thinking to start the Advent of Code 2022 with roc and build a reall appp that take the command line input and run the problem by loading the file with the input.

There is some suggestion on what kind of resource start to make my onborading less painfull?

Thanks!

view this post on Zulip Luke Boswell (Nov 26 2022 at 00:34):

Have you been through the new tutorial yet? I found that really helpful. Also looking at the examples, and the docs for the builtins which is basically the std library. :smiling_face:

view this post on Zulip cmart (Dec 01 2022 at 04:53):

another Roc beginner (but Elm veteran) here thinking about AoC 2022

view this post on Zulip Luke Boswell (Dec 01 2022 at 05:55):

Welcome @cmart, if your open to sharing your work I would be keen to borroe any ideas you have. I'm new to Roc too. :grinning:

view this post on Zulip cmart (Dec 01 2022 at 16:32):

happy to share @Luke Boswell, I'll prob get started on each day 2-3 hours before the next day.. midnight EST is 10 PM here but still bedtime

view this post on Zulip JesterXL (Dec 01 2022 at 16:56):

Anyone made progress on the str to int parsing? I keep getting seg faults or compiler crashes, but I feel like I'm super close.

view this post on Zulip JesterXL (Dec 01 2022 at 17:03):

Oh wait, I think I got it. You just ... have to be careful, lol

view this post on Zulip JesterXL (Dec 01 2022 at 17:03):

"careful" === try various ways

view this post on Zulip JesterXL (Dec 01 2022 at 17:10):

BOOYA, right answer on first try, with NO type annotations... go Roc!

view this post on Zulip JesterXL (Dec 01 2022 at 17:11):

(I know, I know... I shouldn't brag about no annotations, but that's just rad, man...)

view this post on Zulip JesterXL (Dec 01 2022 at 17:20):

BOOOOYAA X2, correct answer 1st try on Day 1 Challenge 2. Roc makes this so dang easy. (famous last words from a guy who never makes it past day 7)

view this post on Zulip JesterXL (Dec 01 2022 at 17:20):

Code if y'all interested, and yes, please judge harshly, I'm learning this, coming from Elm:

view this post on Zulip JesterXL (Dec 01 2022 at 17:21):

app "AoC Day 1"
    packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.1.1/zAoiC9xtQPHywYk350_b7ust04BmWLW00sjb9ZPtSQk.tar.br" }
    imports [pf.Stdout]
    provides [main] to pf

# Attempt 1: 70374 -- right answer!


main =
    dbg (inputLarge |> part2)
    Stdout.line "yay"

# part1 = \input ->
#     Str.split input "\n"
#     |> groupCalories
#     |> List.map sumCalorieStrings
#     |> List.max

part2 = \input ->
    Str.split input "\n"
    |> groupCalories
    |> List.map sumCalorieStrings
    |> List.sortDesc
    |> List.sublist { start: 0, len: 3 }
    |> List.sum

sumCalorieStrings = \listOfStr ->
    List.walk listOfStr 0 \state, elem ->
        when Str.toNat elem is
            Ok value ->
                value + state
            Err _ ->
                state

groupCalories = \calories ->
    List.walk calories { elves: [], staging: [] } \state, elem ->
        if elem == "" then
            { state & elves: List.append state.elves state.staging, staging: [] }
        else
            { state & staging: List.append state.staging elem }
    |> \state -> { state & elves: List.append state.elves state.staging, staging: [] }
    |> .elves

# inputSmall : Str
# inputSmall =
#     """6750
#     6538
#     5292
#     4635
#     6855
#     4137
#     3840
#     4691
#     1633
#     6008
#     2447
#     1448
#     4061

#     4261
#     6778
#     1531
#     2914
#     2102
#     4098
#     2451
#     1219
#     6488
#     3941
#     2158

#     9058
#     3441
#     9318
#     1976
#     6115
#     9451
#     10090
#     5850"""

inputLarge : Str
inputLarge =
    """6750
    6538
    5292
    4635
    6855
    4137
    3840
    4691
    1633
    6008
    2447
    1448
    4061

    4261
    6778
    1531
    2914
    2102
    4098
    2451
    1219
    6488
    ..."""

view this post on Zulip Brendan Hansknecht (Dec 01 2022 at 17:28):

Just a note for anyone interested in advent of code. There is a dedicated #Advent of Code stream. Especially for answers, it is probably the better place to post them. And general discuss there is a good idea as well.

view this post on Zulip JesterXL (Dec 04 2022 at 14:51):

7 years of typing \state elem and Roc's all like "Don't you mean \state, elem?"
Yes Roc... just... give me a bit to adjust, geez.

view this post on Zulip Notification Bot (Dec 04 2022 at 17:36):

Vincenzo Palazzo (vincenzopalazzo) has marked this topic as resolved.

view this post on Zulip Alex Perkins (Dec 04 2022 at 21:12):

I think I found a kind of weird crashing behavior in my bad version of part 2 of day 3. Gist here https://gist.github.com/perkee/424743527fe41adb7ea39eb6f4b06cfc in a few words: I use a pattern match to get the head of a list or crash on empty. Some patterns make the program succeed when it ought not to.

view this post on Zulip Notification Bot (Dec 04 2022 at 21:12):

Alex Perkins has marked this topic as unresolved.

view this post on Zulip Ayaz Hafiz (Dec 04 2022 at 22:13):

Thanks for the report! I can reproduce the first bug - with

gimme : List a -> a
gimme = \l ->
    when l is
        [x] -> x
        [_, ..] -> crash "gimme can fail with many?" # somewhat redundant with [x] I realize

if you pass a list with more than one element it will return the value of the first element when it should crash

I can't reproduce the other two though, they either succeed with one-element lists or panic with >=2 element lists. Are you sure you are passing one-element lists in the second two cases?

view this post on Zulip Alex Perkins (Dec 04 2022 at 23:59):

Ayaz Hafiz said:

I can't reproduce the other two though, they either succeed with one-element lists or panic with >=2 element lists. Are you sure you are passing one-element lists in the second two cases?

I'm not actually sure what I'm passing is the problem, sorry. Obviously it's not a list with one element or I'd be getting the right answer for the question (which I got a while back in Javascript). I'm going to try to peel this back a little bit and see if I can't figure out what I do have in that value. I provided the second two examples of gimme because they work as expected, and it seems like the first should throw but does not.

view this post on Zulip Alex Perkins (Dec 05 2022 at 00:01):

Thanks for all of your hard work on this language @Ayaz Hafiz this is a lot of fun!! Also sorry if I'm posting in the wrong spot; I'll try to familiarize myself with Zulip a bit more, just figured I should get the example in here quickly. Also for what it's worth I was able to solve AoC Day 3 in Roc by instead passing three sets in a record rather than in a list.

view this post on Zulip Ayaz Hafiz (Dec 05 2022 at 00:07):

Thank you for reporting the bug, please let is know if you see any others! I'll try to get to addressing it tomorrow.

view this post on Zulip Alex Perkins (Dec 05 2022 at 03:44):

tiny update: it was definitely a list of many Int Unsigned32. Odd. Turns out my implementation of a function intersecting all the sets in a list was bad.


Last updated: Jul 05 2025 at 12:14 UTC