From e61e9606aeaa0c70f93bd74c92fb8e76585065b0 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 9 Dec 2024 17:47:38 +0000 Subject: [Day 09] Part 1: made it take half the time with obvious optimisation --- day09/day09.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'day09') diff --git a/day09/day09.hs b/day09/day09.hs index 0e03865..ca87b85 100644 --- a/day09/day09.hs +++ b/day09/day09.hs @@ -21,15 +21,15 @@ frag1 :: HDD -> HDD -> HDD -> HDD frag1 acc dt (Free _:rest) = frag1 acc dt rest frag1 acc (Data di dl:dr) p@(Data pi pl:_) - | di == pi = acc ++ [Data pi pl] - | otherwise = frag1 (acc ++ [Data di dl]) dr p + | di == pi = (Data pi pl:acc) + | otherwise = frag1 (Data di dl:acc) dr p frag1 acc (Free f:rest) (Data id d:pack) - | d < f = frag1 (acc ++ [Data id d]) (Free (f - d):rest) pack - | d == f = frag1 (acc ++ [Data id d]) rest pack - | d > f = frag1 (acc ++ [Data id f]) rest (Data id (d - f):pack) + | d < f = frag1 (Data id d:acc) (Free (f - d):rest) pack + | d == f = frag1 (Data id d:acc) rest pack + | d > f = frag1 (Data id f:acc) rest (Data id (d - f):pack) frag :: HDD -> HDD -frag hdd = frag1 [] hdd (reverse hdd) +frag hdd = reverse $ frag1 [] hdd (reverse hdd) checksum :: Int -> Int -> HDD -> Int checksum acc _ [] = acc -- cgit v1.2.1