aoc2025

Advent of Code 2025
git clone git://git.rr3.xyz/aoc2025
Log | Files | Refs | Submodules

commit f52d5fc351e7563e8e75ef296e236679e1bef867
parent e28c90bfbcb4378736d1d56f178cdec2edceebde
Author: Robert Russell <robert@rr3.xyz>
Date:   Sun,  7 Dec 2025 21:28:00 -0800

Solve 07 Part 1

Diffstat:
A07/Common | 2++
A07/Main.hs | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A07/input.txt | 142+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlib/Common/Util.hs | 5++++-
Mpackage.yaml | 3+++
5 files changed, 225 insertions(+), 1 deletion(-)

diff --git a/07/Common b/07/Common @@ -0,0 +1 @@ +../lib/Common +\ No newline at end of file diff --git a/07/Main.hs b/07/Main.hs @@ -0,0 +1,74 @@ +module Main (main) where + +import Common.Cellular (Cellular) +import Common.Cellular qualified as C +import Common.Matrix (Matrix) +import Common.Matrix qualified as M +import Common.Util ((.:)) +import Control.Applicative +import Data.Bool +import Data.ByteString qualified as BS +import Data.Functor +import Sparsec +import System.Exit (die) + +-------------------------------------------------------------------------------- +-- Solution + +data State = Empty | Source | ColdSplitter | HotSplitter | Beam deriving (Eq, Show) + +belowPowered :: Cellular State Bool +belowPowered = any (`elem` [Source, Beam]) <$> C.neighbor (-1, 0) + +besideHotSplitter :: Cellular State Bool +besideHotSplitter = fmap (elem (Just HotSplitter)) . traverse C.neighbor $ [(0, -1), (0, 1)] + +tachyon :: Cellular State State +tachyon = + C.self >>= \case + Empty -> bool Empty Beam .: (||) <$> belowPowered <*> besideHotSplitter + Source -> pure Source + ColdSplitter -> bool ColdSplitter HotSplitter <$> belowPowered + HotSplitter -> pure HotSplitter + Beam -> pure Beam + +part1 :: Matrix State -> Int +part1 = M.count (== HotSplitter) . C.stabilize tachyon + +-------------------------------------------------------------------------------- +-- Parsing + +data ParseError = ErrorUtf8 Loc | ErrorMatrix deriving (Show) +instance Utf8Error ParseError where + utf8Error = ErrorUtf8 +type P a = Parse ParseError a + +pCell :: P State +pCell = char '.' $> Empty <|> char 'S' $> Source <|> char '^' $> ColdSplitter + +pRow :: P [State] +pRow = many pCell <* char '\n' + +pMatrix :: P (Matrix State) +pMatrix = + many pRow >>= \rs -> case M.fromList rs of + Just m -> pure m + Nothing -> err ErrorMatrix + +-------------------------------------------------------------------------------- +-- Main + +getInput :: IO (Matrix State) +getInput = do + raw <- BS.getContents + case runParse pMatrix raw locZero of + Ok input _ _ -> pure input + Fail -> die "parse failure" + Error e -> die $ "parse error: " ++ show e + +main :: IO () +main = do + input <- getInput + print . part1 $ input + +-- print . part2 $ input diff --git a/07/input.txt b/07/input.txt @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^...^.^................................................................... +............................................................................................................................................. +..................................................................^...^.^.^.................................................................. +............................................................................................................................................. +.................................................................^...^.....^................................................................. +............................................................................................................................................. +................................................................^.^.......^.^................................................................ +............................................................................................................................................. +...............................................................^.....^.^.^...^............................................................... +............................................................................................................................................. +..............................................................^...^.^.^.^...^.^.............................................................. +............................................................................................................................................. +.............................................................^.^.^.........^.^.^............................................................. +............................................................................................................................................. +............................................................^...^.^.^.^.^...^.^.^............................................................ +............................................................................................................................................. +...........................................................^.^.^.....^.^.^.....^.^........................................................... +............................................................................................................................................. +..........................................................^.....^.^.......^.^.^.^.^.......................................................... +............................................................................................................................................. +.........................................................^.....^.....^...^.^...^...^......................................................... +............................................................................................................................................. +........................................................^.....^.^.^.^.^...^.^.....^.^........................................................ +............................................................................................................................................. +.......................................................^...^...^.^...^.^.^.....^.....^....................................................... +............................................................................................................................................. +......................................................^.^.^...^.^.^.^...^.^.....^.^...^...................................................... +............................................................................................................................................. +.....................................................^...^.^.^.^.^.^.^.^.^.....^.^.^.^.^..................................................... +............................................................................................................................................. +....................................................^.^.^.....^.^.^.^.^.^.^.^.^...^.^...^.................................................... +............................................................................................................................................. +...................................................^.^.^.^.^...^.....^...^.......^.^.....^................................................... +............................................................................................................................................. +..................................................^.^.^...^.^.^.....^.^...^.^.^.^...^.^...^.................................................. +............................................................................................................................................. +.................................................^...^.^.........^...^.^.^...^.....^.^.^...^................................................. +............................................................................................................................................. +................................................^.^.^...^...^.^.^.^.^.^...^.^.^...^.^.^.^...^................................................ +............................................................................................................................................. +...............................................^.^.^.^.....^.^.^.^...^.^...^...^.....^.^.^.^.^............................................... +............................................................................................................................................. +..............................................^.^.^.^.^...^.^.....^.^.^.^.^.....^...^.^.^...^.^.............................................. +............................................................................................................................................. +.............................................^.^.^.^...^.^.......^.^.^.^...^.^.^...^...^...^.^.^............................................. +............................................................................................................................................. +............................................^.^.........^.^.^...^...^.^.^.....^.^.^.....^...^.^.^............................................ +............................................................................................................................................. +...........................................^.^.....^.^.^.^.^.^...^.^...^.^.^.....^.......^...^.^.^........................................... +............................................................................................................................................. +..........................................^.^.......^.^.^.....^.^.....^.................^.^.^.^.^.^.......................................... +............................................................................................................................................. +.........................................^...^.^.^...^...^.^.^.^.^...^.^.^.....^.^.^...^.^.^.^.^...^......................................... +............................................................................................................................................. +........................................^.^...^.....^.^.......^.^...^.......^...^.^.^.^.^...^...^.^.^........................................ +............................................................................................................................................. +.......................................^.^.^.^.....^.^.^...^...^.^.^.^.^.^.^.^.^...^.^.....^.^.^.^.^.^....................................... +............................................................................................................................................. +......................................^.^.........^.....^.^.^.....^.^...^...........^.^.^.^.......^.^.^...................................... +............................................................................................................................................. +.....................................^...^...^.....^.^.^.....^.^.^...^.....^.^.^.^.^.^.^.^...^.^.^.....^..................................... +............................................................................................................................................. +....................................^.......^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^.^.^.................................... +............................................................................................................................................. +...................................^...^.^.^.^.^...^...^.^.^.^.^...^.^.^...^...^.^...^.^.^.^.^...^.^.^.^.^................................... +............................................................................................................................................. +..................................^.^.^.^.^...^.^.^...^...^.^.^.^.^.^...^.^.......^.....^.^.^.^.......^.^.^.................................. +............................................................................................................................................. +.................................^...^.^.^.^.^.^.^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^.^.^.....^.^.^................................. +............................................................................................................................................. +................................^...^.^...^.^.^.........^.^.^.....^.^.....^...^.^.^.^.^.^...^.^.^...^.^.^.^.^................................ +............................................................................................................................................. +...............................^.^.^.....^.^...^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.....^.^.^...^.^.^...^.^.^.^............................... +............................................................................................................................................. +..............................^.^...^.^.^...^.^.^.^.^...^...^.....^...^.^.^.....^.....^.^.^.^.....^.^.^.^.^...^.............................. +............................................................................................................................................. +.............................^.......^.^.^...^.^...^.^.^.^.^.....^.^.........^.^.^...^.^.^.^.^.^.^.^.......^.^.^............................. +............................................................................................................................................. +............................^.^...^.^.^.....^.^.......^.^.^...^.^...^.^.....^.^...^.....^...^.^.^.^.^...^.^...^.^............................ +............................................................................................................................................. +...........................^...^.^.^.......^.^...^.^.^.^.^.^.....^...^...^.^.^.^.....^.^.^...^.^.^...^.^.....^...^........................... +............................................................................................................................................. +..........................^.......^...^.^.^...^.^.^.^...^.^...^.^.^.^.^.^.^...^...^.^...^.....^...^.....^.^...^...^.......................... +............................................................................................................................................. +.........................^.^.^.^.^.....^...^.^.^...^...^.^...^...^.^...^.^.^.^.....^...^...^.^.^...^...^.^.^.^.^.^.^......................... +............................................................................................................................................. +........................^.^...^.^.^.....^.^...^.^.^.^.^...^.^.^.^.^...^.....^.....^.^.^.^.^.^.^...^...^...^.^.^...^.^........................ +............................................................................................................................................. +.......................^.^.^.^.^...^.^.......^.......^.^.^.^.....^...^.^.^.....^.^.^.......^.^.^...^...^.^.^.^.^.^.^.^....................... +............................................................................................................................................. +......................^.....^.^.......^.^.....^.^.^.^.^...^.....^.^.^.^.^.^.^.^.....^...^.^.^.....^...^.........^.^.^.^...................... +............................................................................................................................................. +.....................^.^.^...^.^...^.^.^.^...^...^.^.^...^.....^.^.....^.^.....^.^.........^.^...^.^.^.^.^.^.....^.^.^.^..................... +............................................................................................................................................. +....................^.^.^.^.^...^...........^...^...^...^...^.^...^...^.....^.^.....^.^.....^.^.^.^.^.^.^...^.^.^.....^.^.................... +............................................................................................................................................. +...................^.^...^.^...^...........^.^.^.^.^.^...^.^.....^.^.....^.....^.^.^...^.^.^.^.^...^.^.....^...^.^...^.^.^................... +............................................................................................................................................. +..................^.^.....^.............^.^.^.^.^.^.^.^.^.^...^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.....^.......^...^.^.................. +............................................................................................................................................. +.................^.....^...^.^...^...^.^...^.^.^...^...^.^.^.^.....^.^.^...^.^.^.^.^...^.^.^.^.....^.....^...^...^.^...^.^.^................. +............................................................................................................................................. +................^.^.^.^.....^...^...^.^...^.^...^.^...^.^.....^.^.^.^...^.^.^.............^.^...^.^.........^.^.^...^.^.^.^.^................ +............................................................................................................................................. +...............^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.........^.^.^.^...^.^.^.^.^...^...........^...^...^.^.^.^.....^...^.^............... +............................................................................................................................................. +..............^.....^.^...^.^.....^.^...^.^.^.^.^...^.^...^...^.^...^.......^.^...^.......^.^.....^.^...^.....^.......^.^.^.^.^.............. +............................................................................................................................................. +.............^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^...^.....^.^...^.^.^.....^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^.^.^.^.^............. +............................................................................................................................................. +............^.^.^.^.^.^...^...^...........^.^.^.^.^.^.^...^.^...^...^...^.^.^.^.........^.^...^.^.^.^.^.^.^...^.....^.......^.^.^............ +............................................................................................................................................. +...........^.^.....^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.....^.^.^.^.^...^.......^...^.^.^.^.....^...^.^.^.^.^.....^.^.^.^.^...^.^.^........... +............................................................................................................................................. +..........^.^.^.^.^.^.^...^.....^.^.^...^.^.^...^...^.^.^.^.^.^.^.......^.^.......^.^.^.^.^.....^.^.^.^.^.^.....^.^...^.^...^.^.^.^.......... +............................................................................................................................................. +.........^.....^.^.^.^...^...^...^...^.^...^.^.......^.^.^.^.^...^.^.^.....^.^.^.^.......^.^.^...^.^.^.^.^.......^...^.....^.^...^.^......... +............................................................................................................................................. +........^.^...^.^.^.^.^.^.....^...^.....^.^.^...^...^...^.^.^...^.^.^...^.^.....^...^.....^.^.^.^...^...^.^.^.^.....^.^.^...^.....^.^........ +............................................................................................................................................. +.......^...^...^.^.^.....^.^...^...^.^.....^...^...^.^.^.^.^...^.^...^.^.^.^.....^.^.^...^.^.^...^.^...^.^.^.....^.^.^.^...^.^.^.^.^.^....... +............................................................................................................................................. +......^...........^.^.^.^.....^.^.^.^...^...............^.^.......^.^...^.^.^.^.....^...^...^.^.^.^.^...^.^...^.^.^.........^...^.^.^.^...... +............................................................................................................................................. +.....^.^.^.^...^.^.^.^.^.^...^.......^.^...^.^.^.^.....^.^.^.^.^.^.^.^.^.^.^...^.^.....^.^.^...^.^...^.......^.^...^.^...^.^.^...^.^.^.^..... +............................................................................................................................................. +....^.^.^...^...^...^.^.^.^...^.^.......^.....^.....^...^.^...^...^.^.^.^.^.......^.^.^...^.^.^.....^.^.^...^...^.^.^.^.^...^.......^...^.... +............................................................................................................................................. +...^.^.....^.^.^...^...^.^.^...^.^.^.^...^...^...^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.....^.^.^...^.^.^...^.^.^.^.^.......^.^...^.^.^.^... +............................................................................................................................................. +..^.^.^.^...^.^.^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.....^...^.^.^.^.^.^...^...^.^.^.^.....^.......^.^.^.^.^.....^.^.^.^.^.........^.^...^.. +............................................................................................................................................. +.^.^.^.^.^.^.^.^.^...^.^.^...^...^.....^.^.....^.^.^.^.^.^...^...^.^...^.^.^.^.^...^.^.^.^...^.^.^.^...^...^...^...^.^.^.^.....^.^.^.....^.^. +............................................................................................................................................. diff --git a/lib/Common/Util.hs b/lib/Common/Util.hs @@ -1,4 +1,7 @@ -module Common.Util (count, consensus, untilFixed) where +module Common.Util ((.:), count, consensus, untilFixed) where + +(.:) :: (c -> d) -> (a -> b -> c) -> (a -> b -> d) +(f .: g) a b = f (g a b) count :: (a -> Bool) -> [a] -> Int count p = length . filter p diff --git a/package.yaml b/package.yaml @@ -59,3 +59,6 @@ executables: 06: source-dirs: ./06 main: Main.hs + 07: + source-dirs: ./07 + main: Main.hs