From b3565d5596f5e63ef91543734373f62efd6099d9 Mon Sep 17 00:00:00 2001 From: KoenDR06 Date: Mon, 1 Dec 2025 15:12:37 +0100 Subject: [PATCH] final day 1 --- src/Day2501.hs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Day2501.hs b/src/Day2501.hs index 3ffb7b6..e2a4400 100644 --- a/src/Day2501.hs +++ b/src/Day2501.hs @@ -1,17 +1,27 @@ module Main where +import Debug.Trace (trace) + +convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it)) + part1 :: String -> Int part1 = length . dropNonZero . scanl (+) 50 . convertToInts . lines where - convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it)) dropNonZero = filter ((0==) . (`mod` 100)) part2 :: String -> Int -part2 = length . dropNonZero . scanl (+) 50 . makeSingleSteps . convertToInts . lines +part2 = go 50 . convertToInts . lines where - convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it)) - dropNonZero = filter ((0==) . (`mod` 100)) - makeSingleSteps = concatMap (\it -> replicate (abs it) (signum it)) + go :: Int -> [Int] -> Int + go p [] = 0 + go p (x:xs) = n + edgeCase + go p' xs + where + n = abs $ p' `div` 100 - p `div` 100 + + edgeCase = if x < 0 && p' `mod` 100 == 0 then 1 else 0 + + if x < 0 && p `mod` 100 == 0 then -1 else 0 + + p' = (p + x) main :: IO () main = do