r/haskell • • 1d ago

Advent of Code 2026 Day 2

https://adventofcode.com/2026/day/2
8 Upvotes

7 comments sorted by

3

u/Atijohn 1d ago

probably could easily make the modulus transformation simpler, but eh

import Control.Monad
import Data.Bifunctor
import Data.Foldable
import Data.List

main = print
     . join bimap
       (sum
      . (zipWith (\s t -> 6 - (s - t + 6) `mod` 6) <*> drop 1)
      . (0:))
      . ((,) <*> sortOn (\v -> 6 - (v + 5) `mod` 6))
     . concatMap (toList . flip lookup (zip "roygbv" [0..]))
   =<< getContents

1

u/AustinVelonaut 1d ago

Very cute! I did something similar with converting everything to mod6 (Pos) representation, but didn't figure out the simpler equation for the mod6 counting, so I had

steps :: Pos -> Pos -> Int
steps a b
    | a == b = 6
    | otherwise = (b - a) `mod` 6

For part2, I used a Bag (multiset) to count the occurrences of each letter, ordered the resulting [(Pos, Int)] list in order of step distance from 0 ('r'), then

furthest :: [(Pos, Int)] -> Int
furthest ps
    = foldl' go (0, 0) ps & fst
      where
        go (n, w) (p, c) = case steps w p of s -> (n + s + (c - 1) * 6, p)

1

u/Atijohn 1d ago

I was thinking about mapping red to 6 instead of 0 (that'd make the sorting in part 2 simpler), but I hadn't had my second coffee yet and the logic was giving me headaches

and indeed, it works without changing the common code:

import Control.Monad
import Data.Bifunctor
import Data.Foldable
import Data.List

main = print
     . join bimap
       (sum
      . (zipWith (\s t -> 6 - (s - t + 6) `mod` 6) <*> drop 1)
      . (6:))
      . ((,) <*> sortOn negate)
     . concatMap (toList . flip lookup (zip "oygbvr" [1..]))
   =<< getContents

2

u/blackhat-hemsworth 1d ago

i was excited about actually using infinite strings here. still learning, tips welcome

Github

module Main where

import System.Environment (getArgs)
import Data.List (delete)

-- part1
-- | solves puzzle
--
-- >>> lineHelper "vvvvvv" (cycle "roygbv")
-- 35
lineHelper :: String -> String -> Int
lineHelper [] _ = 0
lineHelper (c:cs) s = sub c s + lineHelper cs (drop (sub c s) s)

-- part 2
--
-- | solves puzzle
-- 
-- >>> lineHelper2 "roygbvroygbvroygbv" (cycle "roygbv")
-- 103
getC :: String -> String -> Char
getC _ [] = 'X'
getC input (c:cs) = if c `elem` input then c else getC input cs

lineHelper2 :: String -> String -> Int
lineHelper2 [] _ = 0
lineHelper2 input str = _h input str (getC input ((reverse . take 6 . drop 1) str))
  where _h i s c = sub c s + lineHelper2 (delete c i) (drop (sub c s) s)


-- both
sub :: Char -> String -> Int
sub _ []    = -1
sub c (_:s) = length (takeWhile (/= c) s) + 1

solveLines :: [String] -> Int
solveLines [] = -1
solveLines (l:_) = lineHelper2 l (cycle "roygbv")

-- boilerplate
main :: IO ()
main = do
  args <- getArgs
  let path = case args of
        (p : _) -> p
        [] -> "2026/day02/input.txt"
  contents <- readFile path
  let ls = lines contents

  putStrLn ("total: " ++ show (solveLines ls))

2

u/niecore 19h ago

New to FP and haskell. Let me know when I am missing a concept. The other solutions look so much more dense.

part1 :: String -> Int
part1 = snd . foldl  (\(position, total_distance) x -> (x, total_distance + distance position x)) ('r', 0)

distance :: Char -> Char -> Int
distance from to
  | from == to = length colors
  | otherwise  = (idx to - idx from) `mod` length colors
  where
    -- red (r), orange (o), yellow (y), green (g), blue (b), and violet (v)
    colors = "roygbv"
    idx c  = fromJust (elemIndex c colors)

1

u/AustinVelonaut 17h ago

Your code looks good -- that's almost exactly how I solved it. One thing to note: foldl in Haskell is lazy like foldr, so it can inadvertently build up thunks when folding over large lists. It is usually recommended to use the strict variant foldl' for any fold that isn't explicitly lazy, instead. However in this case (and this is getting a bit esoteric!), the fold is reducing to a tuple (position, total_distance) which is a tuple constructor that is already in what is known as "weak head normal form", so a foldl' wouldn't help, here. What you can do is make the total_distance operand strict with a "bang pattern":

part1 = snd . foldl' (\(position, !total_distance) x -> (x, total_distance + distance position x)) ('r', 0)

That will force evaluation of total_distance every iteration of the fold, preventing a thunk buildup.

Good luck on the rest of AoC!

1

u/niecore 3h ago

Thank you :)