AoC 2022 in ZigSelf, day 1

Keywords: #zigself #advent-of-code

Hi there. I had mentioned that I would be solving AoC puzzles this year with ZigSelf. I plan to commit to it as much as possible, but no promises. :^)

The first day of AoC has always been quite easy, and today is no exception, so this was rather simple.

You can see the source code here, or in the below section.

Source code listing
'../../../objects/everything.self' _RunScript.

(|
    parent* = std traits singleton.

    "Put input.txt in your cwd"
    input.

    eachSum: block = (
        input splitBy: '\n\n'; each: [| :group. sum |
            sum: 0.
            [| :break |
                group splitBy: '\n'; each: [| :item |
                    item = '' ifTrue: break.
                    sum: sum + item asInteger.
                ].
            ] break.

            block value: sum.
        ].
    ).

    part1 = (| maxCalories. |
        maxCalories: 0.

        eachSum: [| :sum |
            sum > maxCalories ifTrue: [ maxCalories: sum ].
        ].

        maxCalories.
    ).

    part2 = (| calorieTotals. |
        calorieTotals: std vector copyRemoveAll.

        eachSum: [| :sum |
            calorieTotals add: sum.
        ].

        calorieTotals sort.
        calorieTotals reverse.
        calorieTotals copyFrom: 0 To: 3; fold: [| :acc. :item | acc + item] Initial: 0.
    ).

    run = (
        input: (std file open: 'input.txt'; readAll).
        part1 asString printLine.
        part2 asString printLine.
    ).
|) run.

The main problem I had was that we were missing various vector primitives such as sorting or reversing. Those are now in master as part of std vector. They will eventually be lifted up to the collection level so that every collection benefits from them; however, the main issue there is the fact that, for example, the sorting operation requires a collection to both be mutable and indexable, and we don’t have a mixin type that adds both of them together yet.

Additionally, performance is not very good overall, particularly because no attention to optimization has been given yet. On release mode, calculating the result with my input takes 1.30s on my machine.

ZigSelf commits: