Oh dear. I haven’t posted anything since two AoCs ago. There are reasons, and I’m still hoping it will change, but…
Anyway.
Advent of Code is back for a new season, and while I’ll try and publish a bit about it, I’ll keep the verbosity to a minimum to not risk last year’s situation again. The first installment, Historian Hysteria, is a straightforward implementation exercise. A few imports to clear out the literate Haskell.
import Control.Arrow ((***),(&&&))
import Data.List (sort)
The input is provided as two lists of integers, held side-by-side. Those integers represent so-called location IDs. So in a way, a large part of the puzzle is getting them parsed.
type Input = ([Int],[Int])
parse :: String -> Input
= unzip . pairs . map read . words where
parse :b:xs) = (a,b) : pairs xs
pairs (a= [] pairs []
In part 1, we count the total distance between the lists, total distance being the sum of total distances between pairwise location IDs from both lists, each considered in ascending order.
part1 :: Input -> Int
= sum . uncurry (zipWith ((abs .) . subtract)) . (sort *** sort) part1
In part 2, we implement a slightly more convoluted operation, a similarity score representing the sum of products of elements of the left list by the number of occurences of that same element in the right list.
part2 :: Input -> Int
= sum $ map (\l -> l * length (filter (== l) rs)) ls part2 (ls,rs)
That’s it. A wrapper for completion, and we’re done.
main :: IO ()
= interact $ show . (part1 &&& part2) . parse main
This concludes day 1. See you tomorrow!