Skip to content

Commit

Permalink
Refactored day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimitroulas committed Dec 11, 2023
1 parent 31886f2 commit bf3c30a
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/Solutions/Day09.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Solutions.Day09 (day09, parser, part1, part2) where

import Lib.AOC (runSolution)
import Data.List (foldl')

type Input = [[Int]]

Expand All @@ -15,29 +14,21 @@ differences [] = []
differences [_] = []
differences (x1:x2:xs) = x2 - x1 : differences (x2:xs)

-- Returns the next/previous element for a sequence
extrapolateSequence1 :: ExtrapolateDir -> [Int] -> Int
extrapolateSequence1 dir xs = go [] xs
extrapolateSequence1 :: [Int] -> Int
extrapolateSequence1 xs = go [] xs
where
-- When extrapolating forwards we sum differences whereas when going backwards we minus
-- them
combiner = if dir == Forward then (+) else (-)
-- If extrapolating forwards, we always grab the last element from the lists.
-- Otherwise we always grab the first element.
accessor = if dir == Forward then last else head
foldFn = foldl' (flip combiner) 0

go :: [Int] -> [Int] -> Int
go diffs next
| all (== 0) next = accessor xs `combiner` foldFn diffs
| all (== 0) next = last xs + sum diffs
| otherwise = let seqDifferences = differences next
in go (accessor seqDifferences : diffs) seqDifferences
in go (last seqDifferences : diffs) seqDifferences

part1 :: Input -> Int
part1 = sum . map (extrapolateSequence1 Forward)
part1 = sum . map extrapolateSequence1

-- Extrapolating backwards is just the same as extrapolating forwards on the reversed list
part2 :: Input -> Int
part2 = sum . map (extrapolateSequence1 Back)
part2 = part1 . map reverse

day09 :: IO ()
day09 = runSolution "09" parser part1 part2

0 comments on commit bf3c30a

Please sign in to comment.