Day 9 - Part 1 refactor with state
This commit is contained in:
28
src/Day9.hs
28
src/Day9.hs
@@ -19,7 +19,8 @@ module Day9
|
|||||||
where
|
where
|
||||||
|
|
||||||
import Control.Arrow ((>>>))
|
import Control.Arrow ((>>>))
|
||||||
import Data.List (nub)
|
import Control.Monad.Trans.State.Strict (State, runState, modify)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
|
||||||
newtype Move = Move (Int, Int)
|
newtype Move = Move (Int, Int)
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
@@ -49,17 +50,26 @@ type Pos = (Int, Int)
|
|||||||
|
|
||||||
data Grid = Grid {h :: Pos, t :: Pos} deriving (Show, Eq)
|
data Grid = Grid {h :: Pos, t :: Pos} deriving (Show, Eq)
|
||||||
|
|
||||||
recordPositions :: [Step] -> [Pos]
|
data MovementLog = MovementLog { visited :: S.Set Pos, recordedSteps :: [Step] }
|
||||||
recordPositions steps = nub (lastTailPos:positions)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
appendOne :: (Pos, Step) -> MovementLog -> MovementLog
|
||||||
|
appendOne (p, s) (MovementLog v steps) =
|
||||||
|
MovementLog (S.insert p v) (s:steps)
|
||||||
|
|
||||||
|
recordPositions :: [Step] -> ([Pos], [Step])
|
||||||
|
recordPositions steps = (allPositions, allSteps)
|
||||||
where
|
where
|
||||||
(positions, _, Grid { t = lastTailPos}) = recordPositions' [] steps startGrid
|
allPositions = S.toList $ S.insert lastTailPos (visited state)
|
||||||
recordPositions' :: [Pos] -> [Step] -> Grid -> ([Pos], [Step], Grid)
|
allSteps = reverse (recordedSteps state)
|
||||||
recordPositions' pos [] grid = (pos, [], grid)
|
((_, Grid { t = lastTailPos}), state) = runState (recordPositions' steps startGrid) (MovementLog S.empty [])
|
||||||
recordPositions' pos (m : ms) Grid {h = headPos, t = tailPos} =
|
recordPositions' :: [Step] -> Grid -> State MovementLog ([Step], Grid)
|
||||||
|
recordPositions' [] grid = return ([], grid)
|
||||||
|
recordPositions' (m : ms) Grid {h = headPos, t = tailPos} =
|
||||||
let newHead = headPos `step` m
|
let newHead = headPos `step` m
|
||||||
followStep = follow newHead tailPos
|
followStep = follow newHead tailPos
|
||||||
newTail = tailPos `step` followStep
|
newTail = tailPos `step` followStep
|
||||||
in recordPositions' (tailPos : pos) ms Grid {h = newHead, t = newTail}
|
in modify (appendOne (newTail, followStep)) >> recordPositions' ms Grid {h = newHead, t = newTail}
|
||||||
|
|
||||||
startGrid :: Grid
|
startGrid :: Grid
|
||||||
startGrid = Grid {h = p, t = p}
|
startGrid = Grid {h = p, t = p}
|
||||||
@@ -106,5 +116,5 @@ day9 = do
|
|||||||
input <- readFile "ressources/day09-input"
|
input <- readFile "ressources/day09-input"
|
||||||
putStrLn "Day9"
|
putStrLn "Day9"
|
||||||
let movements = parseMovements input
|
let movements = parseMovements input
|
||||||
let positions = concatMap normalizeMovement >>> recordPositions >>> length $ movements
|
let positions = concatMap normalizeMovement >>> recordPositions >>> fst >>> length $ movements
|
||||||
putStrLn ("Number of distinct positions " ++ show positions)
|
putStrLn ("Number of distinct positions " ++ show positions)
|
||||||
|
|||||||
@@ -67,13 +67,13 @@ spec =
|
|||||||
let positions = parseMovements
|
let positions = parseMovements
|
||||||
>>> concatMap normalizeMovement
|
>>> concatMap normalizeMovement
|
||||||
>>> recordPositions
|
>>> recordPositions
|
||||||
|
>>> fst
|
||||||
>>> sort
|
>>> sort
|
||||||
$ testInput
|
$ testInput
|
||||||
length positions `shouldBe` 13
|
length positions `shouldBe` 13
|
||||||
positions `shouldBe` expectedPositions
|
positions `shouldBe` expectedPositions
|
||||||
it "solves the riddle" $ do
|
it "solves the riddle" $ do
|
||||||
input <- readFile "ressources/day09-input"
|
input <- readFile "ressources/day09-input"
|
||||||
putStrLn "Day9"
|
|
||||||
let movements = parseMovements input
|
let movements = parseMovements input
|
||||||
let positions = concatMap normalizeMovement >>> recordPositions >>> length $ movements
|
let positions = concatMap normalizeMovement >>> recordPositions >>> fst >>> length $ movements
|
||||||
positions `shouldBe` 5878
|
positions `shouldBe` 5878
|
||||||
|
|||||||
Reference in New Issue
Block a user