r/haskell • • 1d ago

AoC Advent of Code 2026 Day 1

https://adventofcode.com/2026/day/1
19 Upvotes

12 comments sorted by

5

u/sondr3_ 1d 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.

parse = |str| List.map_try(str.trim().split_on("\n"), |n| U64.from_str(n))?
	|> List.sort
	|> List.fold(
		Dict.empty(),
		|acc, n| acc.update(
			n,
			|x| match x {
				Err(Missing) => Ok(1.U8)
				Ok(val) => Ok(val + 1)
			},
		),
	)
	|> Dict.to_list
	|> Ok

part = |vals, sort| sort(vals, |(_, v)| v)
	|> List.map(|(k, _)| k)
	|> List.take_first(3)
	.fold(1, U64.times)

part1 = |vals| part(vals, List.sort_by_reversed)
part2 = |vals| part(vals, List.sort_by)

3

u/AustinVelonaut 1d ago

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

3

u/AustinVelonaut 1d ago

I think I would miss having curried functions by default, because you can't easily pass a partially-applied function to a higher-order function.

2

u/ur_frnd_the_footnote 1d ago

Count me in the curious crowd! I’d also be curious to hear more about what you miss from Haskell. 

2

u/gilgamec 1d ago

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.

4

u/Atijohn 1d ago
import Control.Monad
import Data.Bifunctor
import Data.List
                                     
main = print
     . join bimap (product . take 3)
     . ((,) <*> reverse)
     . map head
     . sortOn (negate . length)
     . group
     . sort
     . map (read :: String -> Int)
     . lines
   =<< getContents

2

u/drislands 1d 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 1d 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

2

u/AustinVelonaut 1d ago

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?

1

u/vitelaSensei 1d ago

I'm gonna build my own base package every day bit by bit to reuse it. Otherwise I'm gonna end up with 10 implementations of a Map :p

Then when I reuse something I'll link to the post where it was implemented

1

u/mstksg 1d ago

Wow am I washed :) Some silly mistakes I made:

  1. Parsed as Double instead of Int even though Double never shows up in Advent of Code.

  2. Calculated the sum of the first three frequencies instead of the product

  3. 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

https://github.com/mstksg/advent-of-code/wiki/Reflections-2026

1

u/ChavXO 17h ago

Using GHCi and dataframes.

ghci> df <- D.readCsvWithOpts (defaultReadOptions { D.headerSpec = D.NoHeader }) "../../aoc/01/input.txt"
ghci> df |> D.groupBy ["0"] |> D.aggregate ["total" .= F.countAll] |> D.sortBy [D.Desc (D.int "total")] |> D.take 3 |> D.columnAsList (D.int "0") |> product
ghci> df |> D.groupBy ["0"] |> D.aggregate ["total" .= F.countAll] |> D.sortBy [D.Asc (D.int "total")] |> D.take 3 |> D.columnAsList (D.int "0") |> product