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

Day5Spec.hs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. {-# LANGUAGE QuasiQuotes #-}
  2. module Day5Spec (spec) where
  3. import Test.Hspec
  4. import Text.Heredoc
  5. import qualified Data.Sequence as S
  6. import Day5.Part1
  7. import Day5.Part2
  8. import Day5.Shared
  9. inputPart1 :: String
  10. inputPart1 = [str| [D]
  11. |[N] [C]
  12. |[Z] [M] [P]
  13. | 1 2 3
  14. |
  15. |move 1 from 2 to 1
  16. |move 3 from 1 to 3
  17. |move 2 from 2 to 1
  18. |move 1 from 1 to 2
  19. |]
  20. inputStacks :: String
  21. inputStacks = [str| [D]
  22. |[N] [C]
  23. |[Z] [M] [P]
  24. | 1 2 3
  25. |]
  26. inputOperations :: String
  27. inputOperations = [str|move 1 from 2 to 1
  28. |move 3 from 1 to 3
  29. |move 2 from 2 to 1
  30. |move 1 from 1 to 2
  31. |]
  32. spec :: Spec
  33. spec =
  34. describe "Day5" $ do
  35. describe "Part1" $ do
  36. it "parses Operations" $ do
  37. let operation = head . lines $ inputOperations
  38. parseOperation operation `shouldBe` Operation { count = 1, from = StackIndex 2, to = StackIndex 1 }
  39. it "parses stacks" $ do
  40. parseStacks (lines inputStacks) `shouldBe` S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
  41. it "parses a single line of stacks" $ do
  42. let row = head . lines $ inputStacks
  43. parseRow row `shouldBe` S.fromList [Stack "", Stack "D"]
  44. it "combines stacks" $ do
  45. (Stack "AB" <> Stack "CD") `shouldBe` Stack "ABCD"
  46. it "combines rows" $ do
  47. S.fromList [Stack "", Stack "A"] `combineRows` S.fromList [Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "AB"]
  48. S.empty `combineRows` S.fromList [Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "B"]
  49. S.fromList [Stack "A", Stack "B"] `combineRows` S.empty `shouldBe` S.fromList [Stack "A", Stack "B"]
  50. S.singleton (Stack "") `combineRows` S.fromList[Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "B"]
  51. S.fromList [Stack "A", Stack "B"] `combineRows` S.singleton (Stack "") `shouldBe` S.fromList [Stack "A", Stack "B"]
  52. it "splits input into stacks and operations" $ do
  53. parseInput inputPart1 `shouldBe`
  54. (S.fromList [Stack "NZ", Stack "DCM", Stack "P"],
  55. [ Operation { count = 1, from = StackIndex 2, to = StackIndex 1},
  56. Operation { count = 3, from = StackIndex 1, to = StackIndex 3},
  57. Operation { count = 2, from = StackIndex 2, to = StackIndex 1},
  58. Operation { count = 1, from = StackIndex 1, to = StackIndex 2}])
  59. it "exeutes a count=1 operation on a row" $ do
  60. let op = Operation { count = 1, from = StackIndex 2, to = StackIndex 3}
  61. let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
  62. executeOperation row op `shouldBe` S.fromList [Stack "NZ", Stack "CM", Stack "DP"]
  63. it "exeutes a count > 1 operation on a row" $ do
  64. let op = Operation { count = 2, from = StackIndex 2, to = StackIndex 3}
  65. let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
  66. executeOperation row op `shouldBe` S.fromList [Stack "NZ", Stack "M", Stack "CDP"]
  67. it "finds the top items on stacks" $ do
  68. let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
  69. findTopOfStacks row `shouldBe` S.fromList "NDP"
  70. it "solves the demo" $ do
  71. day5_1 inputPart1 `shouldBe` "CMZ"
  72. describe "Part1" $ do
  73. it "executes 9001 operations on a row" $ do
  74. let op = Operation { count = 2, from = StackIndex 2, to = StackIndex 3}
  75. let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
  76. execute9001Operation row op `shouldBe` S.fromList [Stack "NZ", Stack "M", Stack "DCP"]
  77. it "solves the demo" $ do
  78. day5_2 inputPart1 `shouldBe` "MCD"