From 2137e102e7a8b8603324fbc125436f7865371cef Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Wed, 17 Dec 2025 21:04:15 -0800 Subject: [PATCH] Day 12 --- aoc2025/Day12.newt | 73 ++++++++++++++++++++++++++++++++++++++++++++ aoc2025/day12/eg.txt | 33 ++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 aoc2025/Day12.newt create mode 100644 aoc2025/day12/eg.txt diff --git a/aoc2025/Day12.newt b/aoc2025/Day12.newt new file mode 100644 index 0000000..24a2d43 --- /dev/null +++ b/aoc2025/Day12.newt @@ -0,0 +1,73 @@ +module Day12 + +import Prelude +import Node +import Aoc +import Parser +import Data.List1 + +data Row = MkR Int Int (List Int) +data Gift = MkG Int + + +record Problem where + gifts : List Int + rows : List Row + + + +parse : String → Either String Problem +parse txt = do + let chunks = split (trim txt) "\n\n" + let (c :: cs) = chunks | _ => Left "no chunks" + let (gifts, prob) = unsnoc (c ::: cs) + let lines = split prob "\n" + rows <- traverse parseRow lines + Right $ MkProblem (map weight gifts) rows + where + weight : String → Int + weight line = length' $ filter (_==_ '#') $ unpack line + + parseRow : String → Either String Row + parseRow line = do + let (a :: b :: Nil) = split line ": " | _ => Left "no colon: \{show line}" + let ns = nums b + let (w :: h :: Nil) = nums' "x" a | _ => Left "bad dims \{show a}" + Right $ MkR w h ns + +part1 : String → IO Unit +part1 text = do + let (Right prob) = parse text + | Left err => putStrLn {IO} err + printLn prob.gifts + let rows = prob.rows + let (easy, rest) = partition isEasy rows + let (maybe, imp) = partition (isPossible prob.gifts) rest + printLn "\{show $ length rows} rows, \{show $ length' easy} easy, \{show $ length' maybe} maybe, \{show $ length' imp} impossible" + -- and there is nothing to do for the input, the "maybe" group is empty. + pure MkUnit + where + isEasy : Row → Bool + isEasy (MkR w h boxes) = + let bw = w / 3 + bh = h / 3 + tbox = foldl _+_ 0 boxes + in tbox <= bw * bh + + isPossible : List Int → Row → Bool + isPossible gifts (MkR w h boxes) = + let weight = foldl _+_ 0 $ map (uncurry _*_) $ zip boxes gifts + in weight <= w * h + +part2 : String → Int + +run : String -> IO Unit +run fn = do + putStrLn fn + text <- readFile fn + part1 text + +main : IO Unit +main = do + run "aoc2025/day12/eg.txt" + run "aoc2025/day12/input.txt" diff --git a/aoc2025/day12/eg.txt b/aoc2025/day12/eg.txt new file mode 100644 index 0000000..e5e1b3d --- /dev/null +++ b/aoc2025/day12/eg.txt @@ -0,0 +1,33 @@ +0: +### +##. +##. + +1: +### +##. +.## + +2: +.## +### +##. + +3: +##. +### +##. + +4: +### +#.. +### + +5: +### +.#. +### + +4x4: 0 0 0 0 2 0 +12x5: 1 0 1 0 2 2 +12x5: 1 0 1 0 3 2