You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Day9Spec.hs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. {-# LANGUAGE QuasiQuotes #-}
  2. module Day9Spec (spec) where
  3. import Control.Arrow ((>>>))
  4. import Day9
  5. import Test.Hspec
  6. import Text.Heredoc
  7. import Data.List (sort)
  8. testInput :: String
  9. testInput =
  10. [str|R 4
  11. |U 4
  12. |L 3
  13. |D 1
  14. |R 4
  15. |D 1
  16. |L 5
  17. |R 2
  18. |]
  19. expectedPositions :: [(Int, Int)]
  20. expectedPositions = sort [
  21. (2,4),(3,4),
  22. (3,3),(4,3),
  23. (1,2),(2,2),(3,2),(4,2),
  24. (4,1),
  25. (0,0),(1,0),(2,0),(3,0)
  26. ]
  27. spec :: Spec
  28. spec =
  29. describe "Day9" $ do
  30. describe "Part1" $ do
  31. it "parses the instructions" $ do
  32. parseMovements testInput
  33. `shouldBe` [ right 4,
  34. up 4,
  35. left 3,
  36. down 1,
  37. right 4,
  38. down 1,
  39. left 5,
  40. right 2
  41. ]
  42. it "moves a step" $ do
  43. step (0, 0) (Step (1, 1)) `shouldBe` (1, 1)
  44. step (0, 0) (Step (1, 0)) `shouldBe` (1, 0)
  45. step (1, 0) (Step (-1, 0)) `shouldBe` (0, 0)
  46. step (0, 1) (Step (0, -1)) `shouldBe` (0, 0)
  47. it "normalizes movement" $ do
  48. normalizeMovement still `shouldBe` []
  49. normalizeMovement (left 1) `shouldBe` [Step (-1, 0)]
  50. normalizeMovement (right 1) `shouldBe` [Step (1, 0)]
  51. normalizeMovement (up 1) `shouldBe` [Step (0, 1)]
  52. normalizeMovement (down 2) `shouldBe` [Step (0, -1), Step (0, -1)]
  53. it "follows the head" $ do
  54. follow (1, 0) (0, 0) `shouldBe` Step (0, 0)
  55. follow (0, 1) (0, 0) `shouldBe` Step (0, 0)
  56. follow (1, 1) (0, 0) `shouldBe` Step (0, 0)
  57. follow (2, 1) (0, 0) `shouldBe` Step (1, 1)
  58. follow (1, 2) (0, 0) `shouldBe` Step (1, 1)
  59. follow (2, 2) (0, 0) `shouldBe` Step (1, 1)
  60. follow (4, 2) (3, 0) `shouldBe` Step (1, 1)
  61. it "moves around and records tail position" $ do
  62. let positions = parseMovements
  63. >>> concatMap normalizeMovement
  64. >>> recordPositions
  65. >>> sort
  66. $ testInput
  67. length positions `shouldBe` 13
  68. positions `shouldBe` expectedPositions
  69. it "solves the riddle" $ do
  70. input <- readFile "ressources/day09-input"
  71. putStrLn "Day9"
  72. let movements = parseMovements input
  73. let positions = concatMap normalizeMovement >>> recordPositions >>> length $ movements
  74. positions `shouldBe` 5878