Lambda Discipline

Examples

Advent of Code 2024, day 1, part 1

examples/aoc-24/day-01/part_01.lbd

-- Problem: https://adventofcode.com/2024/day/1 (part one)
-- Run from anywhere: lbd run examples/aoc-24/day-01/part_01.lbd

-- The input is known to be well formed, so a parse error may stop the
-- program: `unwrapOk` takes the number out of `parseInt`'s `Result`.
parseLine : String -> List<Int> = \line. (map \word. (unwrapOk (parseInt word)) (split "   " line))

-- The puzzle is a pure function of the input text.
distance : String -> Int = \input.
    let columns = (transpose (map parseLine (lines input))) in
    let left = (sort (unwrap (get 0 columns))) in
    let right = (sort (unwrap (get 1 columns))) in
    (sum (zipWith \a. \b. (abs (sub a b)) left right))

-- Reading the input is an action; `<-` names its result. `__DIR__` is
-- this file's directory, so the input is found from any working directory.
let input <- (readFile (concat __DIR__ "/input.txt")) in
(println (concat "Answer: " (show (distance input))))

Output

Answer: 1223326

Run it yourself from the repository root: lbd run examples/aoc-24/day-01/part_01.lbd