Advent of Code 2025

Day 10 part 1, and a bit of part 2

Changed files
+78 -2
src
+78 -2
src/Day10.hs
···
module Day10 where
+
import Data.Ratio ((%), Ratio)
+
import Util (split)
+
import Data.Maybe (catMaybes)
+
import Data.Ord (comparing)
+
import Data.List (sortBy)
+
import qualified Data.Map as Map
+
import Data.Map (Map)
+
import Debug.Trace
+
+
type Button = [Int]
+
type Display = [Int]
+
type Machine = (Button, [Button], [Int])
+
+
parse :: String -> [Machine]
+
parse = map parseLine . lines
+
where parseDisplay :: String -> [Int]
+
parseDisplay s = [i | (i,c) <- zip [0..] (init (tail s)), c == '#']
+
+
parseInts :: String -> [Int]
+
parseInts s = map read $ split ',' $ (init (tail s))
+
+
parseLine line =
+
let w = words line
+
disp = head w
+
jolt = last w
+
in (parseDisplay disp, map parseInts (init (tail w)), parseInts jolt)
+
+
xor :: [Int] -> [Int] -> [Int]
+
xor a [] = a
+
xor [] b = b
+
xor (a:as) (b:bs) | a < b = (a : xor as (b:bs))
+
xor (a:as) (b:bs) | a == b = xor as bs
+
xor (a:as) (b:bs) | a > b = (b : xor (a:as) bs)
+
+
minimumMaybe :: [Maybe Int] -> Maybe Int
+
minimumMaybe ns =
+
case catMaybes ns of
+
[] -> Nothing
+
ns' -> Just $ minimum ns'
+
+
solveMachine :: Machine -> Maybe Int
+
solveMachine ([],[],_) = Just 0
+
solveMachine (_,[],_) = Nothing
+
solveMachine (goal, (b:bs), jolt) =
+
minimumMaybe [solveMachine (goal, bs, jolt),
+
(+ 1) <$> solveMachine (xor goal b, bs, jolt)]
+
part1 :: String -> String
-
part1 _ = "Day 10 part 1"
+
part1 input =
+
let machines = parse input
+
solutions = map solveMachine machines
+
in case sequence solutions of
+
Just ss -> show $ sum ss
+
Nothing -> "Failed"
+
+
buttonToRepeat :: Int -> Button -> [Int]
+
buttonToRepeat i button = go 0 button
+
where go n [] = repeat 0
+
go n (b:bs) =
+
if n == b then
+
(i : go (n+1) bs)
+
else
+
(0 : go (n+1) (b:bs))
+
+
gaussJordan :: Map (Int,Int) (Ratio Int) -> Map Int (Ratio Int) -> (Map (Int,Int) (Ratio Int), Map Int (Ratio Int), Int)
+
gaussJordan m g = error "GJ"
+
+
solveMachine2 :: [Button] -> [Int] -> Maybe Int
+
solveMachine2 buttons goal =
+
let mat = Map.fromList [((i,j),1 % 1) | (i,b) <- zip [0..] buttons, j <- b]
+
g = Map.fromList $ zip [0..] (map (% 1) goal)
+
(mat', g', rank) = gaussJordan mat g
+
in Just 0
part2 :: String -> String
-
part2 _ = "Day 10 part 2"
+
part2 input =
+
let machines = parse input
+
solutions = map (\(_,b,g) -> solveMachine2 b g) machines
+
in case sequence solutions of
+
Just ss -> show $ sum ss
+
Nothing -> "Failed"