Advent of Code Day 1 provides a list of 2007 container IDs and asks which three IDs occur most often. Multiplying those three together produces the answer.
The puzzle input is published alongside this post as
/aoc/2026/data/day1.txt, with the worked example
from the problem statement in
day1-example.txt. Complete solutions are
available as day1.roc and
day1.raku. The snippets below are the same code,
shortened only where the full input would otherwise run to two thousand lines.
The worked example from the problem statement:
57
57
55
57
76
78
57
45
43
78
43
76
43
78
57
45
57
76
43
43
76
In that list the three most common IDs are 57, 43 and 76, and
57 * 43 * 76 gives 186276, which matches the expected answer. Both
solutions therefore perform the same four operations: count occurrences, sort
by count, select the top three, and multiply.
The algorithm itself is straightforward. The purpose of this post is to compare how the two languages require it to be written.
1Roc
Roc was chosen partly because I had not used it
for some time, and the language had changed considerably in the interval.
Version 0.1.0 bears little resemblance to the solution.roc application
headers shown in most older tutorials, so a significant part of this section
covers syntax that has changed since those tutorials were written.
The complete solution (the downloadable
day1.roc carries all 2007 input lines in place of
the three below):
# The puzzle input: 2007 IDs, one per line.
input =
\\232
\\351
\\715
tally = |counts, id| {
match Dict.get(counts, id) {
Ok(n) => Dict.insert(counts, id, n + 1)
Err(_) => Dict.insert(counts, id, 1.U64)
}
}
main! = |_| {
ids =
Str.split_on(input, "\n")
|> List.keep_oks(|line| U64.from_str(line))
counts = List.fold(ids, Dict.empty(), tally)
top3 =
Dict.to_list(counts)
|> List.sort_by_reversed(|entry| entry.1)
|> List.take_first(3)
product = List.fold(top3, 1, |acc, entry| acc * entry.0)
Echo.line!("top3: ${Str.inspect(top3)}\n")
Echo.line!("answer: ${product.to_str()}\n")
Ok({})
}
Run against the full input:
top3: [(421, 7), (832, 6), (471, 6)]
answer: 164978112
1.1Why the input is embedded rather than read from a file
In Roc 0.1.0 a bare .roc file is treated as a default app. It requires a
main! function accepting one argument, and it runs against a minimal bundled
platform whose only capability is writing to standard output. There is no
File module, no Stdin, no Env. Importing any of them produces a
FileNotFound error, because these are not modules in scope but file paths
that the compiler resolves.
The input is therefore embedded as a multiline string. Roc’s multiline syntax
prefixes each line with \\ rather than using triple quotes:
input =
\\232
\\351
\\715
Each \\ line contributes one line to the string, separated by newlines, with
no trailing newline. The full input comprises 2007 such lines.
Echo.line! does not append a newline, despite its name. The two calls above
produce separate lines only because each string carries its own \n.
1.2Counting with a Dict
Dict.get returns a Try, defined in 0.1.0 as
Try(ok, err) := [Ok(ok), Err(err)], so it pattern matches like any other tag
union. The match expression is also new syntax, written
match value { Pat => expr } with commas between arms; it replaces the older
when ... is form.
tally = |counts, id| {
match Dict.get(counts, id) {
Ok(n) => Dict.insert(counts, id, n + 1)
Err(_) => Dict.insert(counts, id, 1.U64)
}
}
Note the 1.U64 on the fallback branch. Numeric literals are inferred, and a
bare 1 defaults to a float. This is how I found that 0.1.0 deprecates the
older 1u64 suffix in favour of 1.U64.
1.3Parsing and discarding failed lines
U64.from_str also returns a Try. Rather than threading it through
map_try and handling the error case in main!, List.keep_oks retains only
the Ok values:
ids =
Str.split_on(input, "\n")
|> List.keep_oks(|line| U64.from_str(line))
This is a deliberate trade-off. Any line that is not a number is discarded
without warning, including a trailing blank line. That is acceptable for
Advent of Code, but not for input where errors matter. The real input parses
cleanly; keep_oks discards only the empty final line.
1.4Ranking and multiplying
Dict.to_list converts the tally into (id, count) pairs, and
List.sort_by_reversed sorts descending by the second element. There is no
List.reverse; the equivalent function is List.rev, which is not required
here.
top3 =
Dict.to_list(counts)
|> List.sort_by_reversed(|entry| entry.1)
|> List.take_first(3)
product = List.fold(top3, 1, |acc, entry| acc * entry.0)
entry.0 alone is an ambiguous tuple access, because Roc cannot infer the
tuple type from a single field read. Seeding the fold with 1 and multiplying
by a U64 constrains the accumulator, so the expression type checks without
an annotation.
A standalone type annotation was necessary during development. Roc places
annotations on a separate line above the definition rather than inline as
name : Type = value.
2Raku
The same puzzle in Raku requires less explanation. Bag
is a purpose-built frequency counter, and sort accepts a comparator closure
directly.
my @ids = 'input.txt'.IO.lines.map(*.Int);
my @top = @ids
.Bag
.sort(-*.value)
.head(3)
.map(*.key);
say @top.join(", ");
say @top.reduce({ $^a * $^b });
$ raku day1.raku
421, 832, 471
164978112
The downloadable day1.raku differs in one respect:
it defaults to ../data/day1.txt and accepts an optional path argument,
allowing it to run from /aoc/2026/code/ without modification:
$ cd /aoc/2026/code
$ raku day1.raku # real input
421, 832, 471
164978112
$ raku day1.raku ../data/day1-example.txt
57, 43, 76
186276
2.1Reading the file
'input.txt'.IO returns a Path object, .lines splits it into a lazy
sequence of strings, and .map(*.Int) converts each element. No error handling
is present: a failure to open the file raises X::IO::Open, which is not
handled here.
2.2Bag does the counting
@ids.Bag constructs a Bag, an immutable multiset that counts elements as
it is built. Implementing the equivalent fold manually would be unnecessary
work.
.sort(-*.value) sorts the resulting pairs by count in descending order; the
leading minus negates the key so that the largest count sorts first.
.head(3) selects the top three, and .map(*.key) removes the counts,
leaving the IDs.
2.3The product
reduce with a block using the $^a and $^b placeholders multiplies the
three IDs. It introduces no accumulator variable, no tuple access for the
compiler to reject, and no numeric literal requiring type inference.
3The answer
Both languages agree:
421 appears 7 times
832 appears 6 times
471 appears 6 times
421 * 832 * 471 = 164978112
Membership of the top three is unambiguous: one ID occurs 7 times and two
occur 6 times, while the 177 IDs tied at 5 occurrences fall well below the
threshold. The order of the two tied IDs is less stable. Raku’s Bag iterates
in hash order, so day1.raku printed 421, 832, 471 on seven of twelve runs
and 421, 471, 832 on the remaining five; the product is identical either
way, which is all the puzzle requires. Roc produced the same output on all
twelve runs.
The two solutions illustrate a trade-off in opposite directions. Roc requires
the pipeline to be assembled explicitly: split, parse, filter, fold, sort,
fold. In exchange, the compiler reports the ambiguous tuple access and the
float literal before execution. Raku provides Bag and a comparator closure,
but nothing between .IO.lines and .reduce is checked.
For a 2007-line frequency count both are effectively instantaneous. The
compiled Roc binary runs in approximately a millisecond; raku, including
interpreter startup, settles at around 80ms once the precompilation cache is
warm. The Roc version is thirty-one lines against Raku’s ten. That difference
reflects the cost of a language that requires these operations to be written
explicitly rather than supplied by a standard container type.
4Raku and Roc as functional languages
Both languages build programs from pure functions over immutable values, but
they disagree about where that commitment is enforced. Roc places it in the
language: values are immutable by default, tag unions are algebraic data types,
match is checked for exhaustiveness, types are inferred, and failure is
carried in a Try rather than thrown as an exception. Raku is multi-paradigm
and dynamically typed, and its functional facilities are a vocabulary rather
than a constraint. Bag, Set and Map are immutable value types with
structural equality, lists are lazy by default, and higher-order functions are
ordinary methods.
Judged as a notation for functional programming, Raku is the stronger of the
two for this class of problem, and the reason is not immutability, since both
languages have it. It is what each one ships. Roc supplies the mechanism: a
fold, an accumulator, and the obligation to write the recursion out. Raku
supplies the abstraction. @ids.Bag is an immutable, structurally comparable
frequency map produced by a single pure application, with no accumulator to
name and no base case to get wrong, and because it is an ordinary value it
composes with the rest of the language without ceremony.
Laziness is the clearest instance. Raku evaluates lists on demand, so an unbounded sequence is an ordinary value:
my @nats = 1, * + 1 ... *;
say @nats[99];
That prints 100 without materialising the sequence. Roc is strict, so the
equivalent requires an explicit recursive construction. The asymmetry runs
through the rest of the vocabulary as well: a junction such as
@ids.all > 0 states a property of an entire collection as an expression
rather than a loop with an early exit, and [+] reduces a list without
introducing a binding at all.
What Roc retains is the guarantee rather than the expressiveness: exhaustiveness checking, static types, and immutability that cannot be circumvented. Those matter in a codebase large enough for the compiler to be worth consulting. For a short transformation of a flat list they buy less than Raku’s shorthand, because there is less for the compiler to catch and the vocabulary does most of the writing.