您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Day9Spec.hs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. testInput2 :: String
  28. testInput2 = [str|R 5
  29. |U 8
  30. |L 8
  31. |D 3
  32. |R 17
  33. |D 10
  34. |L 25
  35. |U 20
  36. |]
  37. spec :: Spec
  38. spec =
  39. describe "Day9" $ do
  40. describe "Part1" $ do
  41. it "parses the instructions" $ do
  42. parseMovements testInput
  43. `shouldBe` [ right 4,
  44. up 4,
  45. left 3,
  46. down 1,
  47. right 4,
  48. down 1,
  49. left 5,
  50. right 2
  51. ]
  52. it "moves a step" $ do
  53. step (0, 0) (Step (1, 1)) `shouldBe` (1, 1)
  54. step (0, 0) (Step (1, 0)) `shouldBe` (1, 0)
  55. step (1, 0) (Step (-1, 0)) `shouldBe` (0, 0)
  56. step (0, 1) (Step (0, -1)) `shouldBe` (0, 0)
  57. it "normalizes movement" $ do
  58. normalizeMovement still `shouldBe` []
  59. normalizeMovement (left 2) `shouldBe` [Step (-1, 0),Step (-1, 0)]
  60. normalizeMovement (right 1) `shouldBe` [Step (1, 0)]
  61. normalizeMovement (up 1) `shouldBe` [Step (0, 1)]
  62. normalizeMovement (down 2) `shouldBe` [Step (0, -1), Step (0, -1)]
  63. it "follows the head" $ do
  64. follow (1, 0) (0, 0) `shouldBe` Step (0, 0)
  65. follow (0, 1) (0, 0) `shouldBe` Step (0, 0)
  66. follow (1, 1) (0, 0) `shouldBe` Step (0, 0)
  67. follow (2, 1) (0, 0) `shouldBe` Step (1, 1)
  68. follow (1, 2) (0, 0) `shouldBe` Step (1, 1)
  69. follow (2, 2) (0, 0) `shouldBe` Step (1, 1)
  70. follow (4, 2) (3, 0) `shouldBe` Step (1, 1)
  71. it "moves around and records tail position" $ do
  72. let positions = parseMovements
  73. >>> concatMap normalizeMovement
  74. >>> recordPositions
  75. >>> fst
  76. >>> sort
  77. $ testInput
  78. length positions `shouldBe` 13
  79. positions `shouldBe` expectedPositions
  80. it "solves the riddle" $ do
  81. input <- readFile "ressources/day09-input"
  82. let headSteps = parseMovements >>> concatMap normalizeMovement $ input
  83. let allTails = buildTails headSteps
  84. let positions = (!! 1) >>> fst >>> length $ allTails
  85. positions `shouldBe` 5878
  86. it "solves example of part 2" $ do
  87. let headSteps = parseMovements >>> concatMap normalizeMovement $ testInput2
  88. let allTails = buildTails headSteps
  89. let tail9Positions = fst $ allTails !! 9
  90. length tail9Positions `shouldBe` 36
  91. it "solves the riddle part 2" $ do
  92. input <- readFile "ressources/day09-input"
  93. let headSteps = parseMovements >>> concatMap normalizeMovement $ input
  94. let allTails = buildTails headSteps
  95. let positions = (!! 9) >>> fst >>> length $ allTails
  96. positions `shouldBe` 2405