summaryrefslogtreecommitdiff
path: root/day09
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-12-09 17:47:38 +0000
committerNat Lasseter <user@4574.co.uk>2024-12-09 17:47:38 +0000
commite61e9606aeaa0c70f93bd74c92fb8e76585065b0 (patch)
treeb7f52f7e1de098f6292f36a5d978bcf9080ed1eb /day09
parent980105ec341e3d9b04a4d39533fabd898944cfc4 (diff)
[Day 09] Part 1: made it take half the time with obvious optimisation
Diffstat (limited to 'day09')
-rw-r--r--day09/day09.hs12
1 files changed, 6 insertions, 6 deletions
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