diff options
Diffstat (limited to '2026/2.hs')
| -rw-r--r-- | 2026/2.hs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/2026/2.hs b/2026/2.hs new file mode 100644 index 0000000..b05c400 --- /dev/null +++ b/2026/2.hs @@ -0,0 +1,19 @@ +{-# OPTIONS -Wno-x-partial -Wno-incomplete-uni-patterns #-} +module Main where + +import Control.Applicative (asum) +import Data.List (foldl', findIndex) +import Data.Map.Strict qualified as Map +import Data.Maybe (fromJust) + +main :: IO () +main = do + input <- head . words <$> getContents + let nexts i = drop ((i + 1) `rem` 6) (cycle "roygbv") + print $ foldl' (\i c -> i + 1 + fromJust (findIndex (==c) (nexts i))) 0 input + print . fst $ + foldl' (\(i, mp) _ -> + let Just (c', dist, n) = asum [(c,d,) <$> mp Map.!? c + | (c, d) <- zip (reverse (take 6 (nexts i))) [6,5..]] + in (i + dist, (if n > 1 then Map.adjust pred else Map.delete) c' mp)) + (0, Map.fromListWith (+) (map (,1::Int) input)) input |
