From be6c3d37ea5b91821f678d70026cec1fef0ee71a Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 7 Dec 2025 22:38:39 +0100 Subject: [PATCH] Minor cleanup Avoid repeatedly creating sets that live for almost no time, as well as some readability fixes --- 2025/day07/solve.hs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/2025/day07/solve.hs b/2025/day07/solve.hs index 020457b..0ea0157 100644 --- a/2025/day07/solve.hs +++ b/2025/day07/solve.hs @@ -1,7 +1,6 @@ import Data.Array import Data.List import Data.Maybe -import Data.Set import System.Environment import System.Exit @@ -13,18 +12,17 @@ stringToArray s = listArray (0, length s - 1) s run :: String -> IO () run input = let gridLines = lines input - startingPoint = findStartingPoint (head gridLines) - initial = Data.Set.singleton startingPoint - arrLines = Data.List.map stringToArray (tail gridLines) + startingPoint = findStartingPoint $ head gridLines + arrLines = Data.List.map stringToArray $ tail gridLines in do - print (part1 arrLines startingPoint) - print (part2 arrLines startingPoint) + print $ part1 arrLines startingPoint + print $ part2 arrLines startingPoint findStartingPoint :: [Char] -> Int -findStartingPoint line = fromJust (elemIndex 'S' line) +findStartingPoint line = fromJust $ elemIndex 'S' line part1 arrLines startingPoint = - let initial = Data.Set.singleton startingPoint + let initial = [startingPoint] in simulate arrLines 0 initial checkHit :: Array Int Char -> Int -> [Int] @@ -35,8 +33,8 @@ checkHit line idx = case line ! idx of simulate [] count active = count simulate lines count active = let arr = head lines - followUp = Data.Set.fromList $ concatMap (checkHit arr) (Data.Set.toList active) - hits = length (Data.Set.filter (\i -> arr ! i == '^') active) + followUp = nub $ concatMap (checkHit arr) active + hits = length $ Data.List.filter (\i -> arr ! i == '^') active remainder = tail lines in simulate remainder (hits + count) followUp