Browse Source

Day 9 - Part 1 refactor with state

main
Jens Kadenbach 2 years ago
parent
commit
dc5a757eb6
2 changed files with 21 additions and 11 deletions
  1. 19
    9
      src/Day9.hs
  2. 2
    2
      test/Day9Spec.hs

+ 19
- 9
src/Day9.hs View File

19
 where
19
 where
20
 
20
 
21
 import Control.Arrow ((>>>))
21
 import Control.Arrow ((>>>))
22
-import Data.List (nub)
22
+import Control.Monad.Trans.State.Strict (State, runState, modify)
23
+import qualified Data.Set as S
23
 
24
 
24
 newtype Move = Move (Int, Int)
25
 newtype Move = Move (Int, Int)
25
   deriving (Show, Eq)
26
   deriving (Show, Eq)
49
 
50
 
50
 data Grid = Grid {h :: Pos, t :: Pos} deriving (Show, Eq)
51
 data Grid = Grid {h :: Pos, t :: Pos} deriving (Show, Eq)
51
 
52
 
52
-recordPositions :: [Step] -> [Pos]
53
-recordPositions steps = nub (lastTailPos:positions)
53
+data MovementLog = MovementLog { visited :: S.Set Pos, recordedSteps :: [Step] }
54
+  deriving (Show, Eq)
55
+
56
+appendOne :: (Pos, Step) -> MovementLog -> MovementLog
57
+appendOne (p, s) (MovementLog v steps) =
58
+   MovementLog (S.insert p v) (s:steps)
59
+
60
+recordPositions :: [Step] -> ([Pos], [Step])
61
+recordPositions steps = (allPositions, allSteps)
54
   where
62
   where
55
-    (positions, _, Grid { t = lastTailPos}) = recordPositions' [] steps startGrid
56
-    recordPositions' :: [Pos] -> [Step] -> Grid -> ([Pos], [Step], Grid)
57
-    recordPositions' pos [] grid = (pos, [], grid)
58
-    recordPositions' pos (m : ms) Grid {h = headPos, t = tailPos} =
63
+    allPositions = S.toList $ S.insert lastTailPos (visited state)
64
+    allSteps = reverse (recordedSteps state)
65
+    ((_, Grid { t = lastTailPos}), state) = runState (recordPositions' steps startGrid) (MovementLog S.empty [])
66
+    recordPositions' :: [Step] -> Grid -> State MovementLog ([Step], Grid)
67
+    recordPositions' [] grid = return ([], grid)
68
+    recordPositions' (m : ms) Grid {h = headPos, t = tailPos} =
59
       let newHead = headPos `step` m
69
       let newHead = headPos `step` m
60
           followStep = follow newHead tailPos
70
           followStep = follow newHead tailPos
61
           newTail = tailPos `step` followStep
71
           newTail = tailPos `step` followStep
62
-       in recordPositions' (tailPos : pos) ms Grid {h = newHead, t = newTail}
72
+       in modify (appendOne (newTail, followStep)) >> recordPositions' ms Grid {h = newHead, t = newTail}
63
 
73
 
64
 startGrid :: Grid
74
 startGrid :: Grid
65
 startGrid = Grid {h = p, t = p}
75
 startGrid = Grid {h = p, t = p}
106
    input <- readFile "ressources/day09-input"
116
    input <- readFile "ressources/day09-input"
107
    putStrLn "Day9"
117
    putStrLn "Day9"
108
    let movements = parseMovements input
118
    let movements = parseMovements input
109
-   let positions = concatMap normalizeMovement >>> recordPositions >>> length $ movements
119
+   let positions = concatMap normalizeMovement >>> recordPositions >>> fst >>> length $ movements
110
    putStrLn ("Number of distinct positions " ++ show positions)
120
    putStrLn ("Number of distinct positions " ++ show positions)

+ 2
- 2
test/Day9Spec.hs View File

67
         let positions = parseMovements
67
         let positions = parseMovements
68
                 >>> concatMap normalizeMovement
68
                 >>> concatMap normalizeMovement
69
                 >>> recordPositions
69
                 >>> recordPositions
70
+                >>> fst
70
                 >>> sort
71
                 >>> sort
71
                 $ testInput
72
                 $ testInput
72
         length positions `shouldBe` 13
73
         length positions `shouldBe` 13
73
         positions `shouldBe` expectedPositions
74
         positions `shouldBe` expectedPositions
74
       it "solves the riddle" $ do
75
       it "solves the riddle" $ do
75
         input <- readFile "ressources/day09-input"
76
         input <- readFile "ressources/day09-input"
76
-        putStrLn "Day9"
77
         let movements = parseMovements input
77
         let movements = parseMovements input
78
-        let positions = concatMap normalizeMovement >>> recordPositions >>> length $ movements
78
+        let positions = concatMap normalizeMovement >>> recordPositions >>> fst >>> length $ movements
79
         positions `shouldBe` 5878
79
         positions `shouldBe` 5878

Loading…
Cancel
Save