This year I'm branching out and I'm trying Roc after using Haskell the last ~six years after seeing the 0.1 release pop up on HN. My solution for today is quite Haskelly, but I'm definitely missing a few things from the Haskell ecosystem and I'm still not quite sure I like how magic creating types is (though it gives me PureScript vibes with row polymorphism). I'll refrain from posting in the other daily threads unless people are curious.
I'm doing AoC with my own lazy functional language Admiran, similar to Haskell. It has left-to-right operators for apply |> and compose .>, and a bunch of functional data structures like bag which makes today's puzzle pretty easy:
%import "adventLib"
%import <bag>
day01 :: io ()
day01
= readInput "day01" >>= lines .> map intval .> fromList cmpint .> toList .> go
where
go idCounts
= output [part1, part2]
where
part1 = process descending
part2 = process comparing
process order = idCounts |> sortBy (order cmpint snd) |> map fst |> take 3 |> product |> showint
I tried Roc out a few years ago, back when the syntax was more obviously Elm-like. I still like some of the language, how it separates pure from impure .... but man, I wish they hadn't settled on the syntax of Rust of all things.
This year I'm doing a prelude-only version where I need to reinvent the wheel along the way. Today only required re-writing `sort` and `Data.Ord (Down)`
Fun project to implement (and understand) various data structures and functions. Do you build up a local library of them for the year that you can reuse for later puzzles, or is each day a "start from scratch" day?
Parsed as Double instead of Int even though Double never shows up in Advent of Code.
Calculated the sum of the first three frequencies instead of the product
Found the product of the three least common instead of the three most common.
All of these silly mistakes motivate me to get back in practice haha. The struggle is real.
Anyway in Haskell this is pretty simple, it reads the same in bash or any other text processing language probably. Parsing is lines, and you can find the freq list using Data.Map.fromListWith (+). And the difference between Part 1 and Part 2 is just the order that you sort the frequencies before you sum them.
import Data.Map qualified as M
-- | List of codes and their frequencies
freqList :: String -> [(String, Int)]
freqList = M.toList . M.fromListWith (+) . map (,1) . lines
part1 :: String -> Int
part1 = product . map (read . fst) . take 3 . sortOn (Down . snd) . freqList
part2 :: String -> Int
part2 = product . map (read . fst) . take 3 . sortOn snd . freqList
5
u/sondr3_ 4d ago
This year I'm branching out and I'm trying Roc after using Haskell the last ~six years after seeing the 0.1 release pop up on HN. My solution for today is quite Haskelly, but I'm definitely missing a few things from the Haskell ecosystem and I'm still not quite sure I like how magic creating types is (though it gives me PureScript vibes with row polymorphism). I'll refrain from posting in the other daily threads unless people are curious.
4
u/Atijohn 4d ago
2
u/drislands 3d ago
This is the first time in a long time I've tried the AoC...and the first time since I started learning Haskell! Posted my solution to my profile.
1
u/vitelaSensei 4d ago
This year I'm doing a prelude-only version where I need to reinvent the wheel along the way. Today only required re-writing `sort` and `Data.Ord (Down)`
Prelude only solution
1
u/mstksg 3d ago
Wow am I washed :) Some silly mistakes I made:
Doubleinstead ofInteven though Double never shows up in Advent of Code.All of these silly mistakes motivate me to get back in practice haha. The struggle is real.
Anyway in Haskell this is pretty simple, it reads the same in bash or any other text processing language probably. Parsing is
lines, and you can find the freq list usingData.Map.fromListWith (+). And the difference between Part 1 and Part 2 is just the order that you sort the frequencies before you sum them.https://github.com/mstksg/advent-of-code/wiki/Reflections-2026
1
u/ChavXO 3d ago
Using GHCi and dataframes.