app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br", } import pf.Stdout import "data.txt" as data : Str main = lines = Str.splitOn data "\n" |> List.keepIf (\l -> Str.countUtf8Bytes l > 0) # the last line is empty. only keep non-empties # |> List.dropLast 1 # cleaner but scarier # split the lines into a 2-tuple of lists lists = List.walk lines ([], []) \state, elem -> parts = Str.splitOn elem " " # man I gotta figure out how to deal with errors better, this is not pretty # tried using try but couldn't get walk to work with functions that return errors left = Str.toU64 (Result.withDefault (List.first parts) "0") right = Str.toU64 (Result.withDefault (List.last parts) "0") (List.appendIfOk state.0 left, List.appendIfOk state.1 right) sorted = (List.sortAsc lists.0, List.sortAsc lists.1) Stdout.line! "Distance: $(Num.toStr (part1 sorted))" Stdout.line! "Similarity: $(Num.toStr (part2 sorted))" # tried to dry this up by having 1 walk and a record for part1 and part2, but it got ugly part1 = \(left, right) -> List.map2 left right Pair |> List.walk 0 \state, (Pair l r) -> state + (Num.absDiff l r) part2 = \(left, right) -> List.map2 left right Pair |> List.walk 0 \state, (Pair l _) -> count = List.countIf right (\e -> e == l) state + count * l