4 min read

Advent of Code 2026Part 2

Day 1, Prelude only

Solving day 1 with nothing but the Prelude

  • Haskell
  • Advent of Code

Part 1

In my previous post I solved day 1 the usual way.

But importing packages for a problem this small always feels a bit unsatisfying, so this time let’s do it with nothing but the Prelude.

We declare our module, and import nothing chef’s kiss.

module Day01BaseOnly where

Let’s once again start by defining the input type.

type Input = [Int]

And our parsing function.

parse :: String -> Input
parse = map read . lines

Now let’s build a frequency list of (ID, frequency) pairs. We sort the IDs first, so equal IDs sit next to each other, then walk the list with getFreqs:

  • No IDs left: return the frequencies we’ve built.
  • First ID: nothing’s been counted yet, so it starts at 1.
  • Otherwise: if the ID matches the one on top of the frequency list, add 1 to its count. If not, push it on top with a count of 1.

There’s one problem: sort isn’t in the Prelude. It lives in Data.List, which is part of base, so we’ll have to write it ourselves.

frequencies :: (Ord a) => [a] -> [(a, Int)]
frequencies xs = getFreqs (sort xs) []
  where
  getFreqs :: (Eq a) => [a] -> [(a, Int)] -> [(a, Int)]
  getFreqs [] ys = ys
  getFreqs (x:rest) [] = getFreqs rest [(x, 1)]
  getFreqs (x:rest) ((y,f):ys)
    | x == y    = getFreqs rest ((x, f+1) : ys)
    | otherwise = getFreqs rest ((x,1) : (y, f) : ys)

Let’s start with sortBy, which takes a function that compares two items, and a list, and sorts it.

I went with the postcard Haskell “quicksort”: take the first item, put the smaller items on its left and the rest on its right, then sort each side the same way.

sortBy :: (a -> a -> Ordering) -> [a] -> [a]
sortBy _ [] = []
sortBy fn (x:xs) = sortBy fn smaller ++ [x] ++ sortBy fn larger
  where
  smaller = filter (\it -> fn it x == LT) xs
  larger  = filter (\it -> fn it x /= LT) xs

sortOn is a very useful variant. It differs from sortBy in a small way: if you can map the items in the list to something that’s sortable, you can use those to sort the original list.

sortOn :: (Ord b) => (a -> b) -> [a] -> [a]
sortOn f = sortBy (\a b -> compare (f a) (f b))

Finally, sort is just sortOn id. It’s the one that needs the elements themselves to be Ord.

sort :: (Ord a) => [a] -> [a]
sort = sortOn id

We’re almost there! part1 takes the input string and returns the result.

  • We parse to get a list of IDs
  • get their frequencies as [(ID, frequency)]
  • sortOn the frequency in reverse order
  • take the top 3
  • keep only their IDs
  • and finally multiply them
part1 :: String -> Int
part1 = parse
    >>> frequencies
    >>> sortOn (OrdReversed . snd)
    >>> take 3
    >>> map fst
    >>> product

And that’s it for part 1! We just need to implement some of the missing functions:

In the last post I called this “and then”. Its proper name is reversed function composition, and it lets us chain functions left to right.

(>>>) :: (a -> b) -> (b -> c) -> a -> c
(>>>) = flip (.)

OrdReversed is what we used to sort the list in descending order. By default our sort function sorts in ascending order, and this type flips it around. It’s our own version of Down from Data.Ord, which we used in the last post.

newtype OrdReversed a = OrdReversed a
  deriving (Eq)

Its Ord instance just compares the two values the other way around.

instance (Ord a) => Ord (OrdReversed a) where
  compare (OrdReversed a) (OrdReversed b) = compare b a

Part 2

For part 2 we just get rid of the OrdReversed constructor to get the 3 least frequent IDs.

part2 :: String -> Int
part2 = parse
    >>> frequencies
    >>> sortOn snd
    >>> take 3
    >>> map fst
    >>> product