Stream: beginners

Topic: Advent of code 2025 day 1 parsing


view this post on Zulip TeaDrinkingProgrammer (Dec 04 2025 at 21:56):

I am trying to parse the input for day 1, but I am getting stuck on separating the letter from the number, any tips? This is what I have so far (old compiler):

app [main!] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br",
    ascii: "https://github.com/Hasnep/roc-ascii/releases/download/v0.3.1/1PCTQ0tzSijxfhxDg1k_yPtfOXiAk3j283b8EWGusVc.tar.br",
}

import ascii.Ascii exposing [from_str]
import pf.Stdout
import "data/day1.txt" as input : Str

main! =
    Stdout.line! "part 1"

example =
    """
    L68
    L30
    R48
    L5
    R60
    L55
    L1
    L99
    R14
    L82
    """

part1 = |text|
    lines = inputToLists(text)
    lines
    |> List.keep_oks Ascii.from_str
    |> List.map(
        |astr|
            when astr is
                ['L', first, second] -> Ascii.to_u32(first)? + Ascii.to_u32(second)? * -1
                ['R', first, second] -> Ascii.to_u32(first)? + Ascii.to_u32(second)?
                _ -> 0,
    )

inputToLists = |in|
    in
    |> Str.split_on("\n")

view this post on Zulip Anton (Dec 05 2025 at 10:56):

Hi @TeaDrinkingProgrammer,
This is from my solution:

Rotation : [Left I64, Right I64]

parse_rotation : Str -> Result Rotation _
parse_rotation = |line|
    trimmed = Str.trim(line)
    bytes = Str.to_utf8(trimmed)

    when bytes is
        ['L', .. as rest] ->
            distance_str = Str.from_utf8(rest)?
            distance = Str.to_i64(distance_str)?
            Ok(Left(distance))

        ['R', .. as rest] ->
            distance_str = Str.from_utf8(rest)?
            distance = Str.to_i64(distance_str)?
            Ok(Right(distance))

        _ -> Err(InvalidFormat)

Note that you want to avoid ['L', first, second] because your line can have a single digit like L1.

view this post on Zulip TeaDrinkingProgrammer (Dec 05 2025 at 11:47):

Thanks, that's very helpful! I was missing the final Ok for the variants


Last updated: Dec 21 2025 at 12:15 UTC