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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. {-# LANGUAGE QuasiQuotes #-}
  2. module Day13Spec (spec) where
  3. import Day13
  4. import Test.Hspec
  5. import Text.Heredoc
  6. import Data.List (sort, elem, elemIndex)
  7. testInput :: String
  8. testInput = [str|[1,1,3,1,1]
  9. |[1,1,5,1,1]
  10. |
  11. |[[1],[2,3,4]]
  12. |[[1],4]
  13. |
  14. |[9]
  15. |[[8,7,6]]
  16. |
  17. |[[4,4],4,4]
  18. |[[4,4],4,4,4]
  19. |
  20. |[7,7,7,7]
  21. |[7,7,7]
  22. |
  23. |[]
  24. |[3]
  25. |
  26. |[[[]]]
  27. |[[]]
  28. |
  29. |[1,[2,[3,[4,[5,6,7]]]],8,9]
  30. |[1,[2,[3,[4,[5,6,0]]]],8,9]
  31. |]
  32. falsePositive :: String
  33. falsePositive = "[[],[[[10,2,6],[1,4,9,2,5]],[],[5,2],[1,3,5]],[[[7,2,2,0],[0,1],10],10,[9,[4,8],[8,2,10],[1,4,2]],7,[[2]]]]\n" ++
  34. "[[],[[[],[],[9,10,5,6],2,[0]],10],[[],[[4,9,3,7],7,4],[],2,2],[8,[6,8,[7,5,5],[2,4,8,0,7],[3,1,7,3,9]],[[9,1,5,1],[5,1,1],[],[2,1,2,1]],[[0,9,1,10],5,[],10,[5,0,10,2]],[10,[3,4,8,8,4],5]]]\n"
  35. spec :: Spec
  36. spec =
  37. describe "Day12" $ do
  38. describe "Part1" $ do
  39. it "parser a single pair" $ do
  40. let input = "[1,[],3]\n[]\n"
  41. show (head $ parsePackets input) `shouldBe` input
  42. it "checks order" $ do
  43. Entry 1 <= Entry 2 `shouldBe` True
  44. Entry 2 <= Entry 1 `shouldBe` False
  45. List [] <= List [] `shouldBe` True
  46. List [] <= List [Entry 1] `shouldBe` True
  47. List [Entry 1] <= List [] `shouldBe` False
  48. List [Entry 1] <= Entry 2 `shouldBe` True
  49. List [Entry 2] <= Entry 1 `shouldBe` False
  50. let pair = head $ parsePackets falsePositive
  51. checkPair pair `shouldBe` False
  52. it "checks examples" $ do
  53. let pairs = parsePackets testInput
  54. length pairs `shouldBe` 8
  55. sumIndices pairs `shouldBe` 13
  56. map checkPair pairs `shouldBe` [True, True, False, True, False, True, False, False]
  57. describe "Part2" $ do
  58. it "checks examples" $ do
  59. let packets = sort $ addDividers $ parsePacketList testInput
  60. let a = packets !! 9
  61. let b = head dividers
  62. a `shouldBe` b
  63. elem (head dividers) packets `shouldBe` True
  64. findDividers packets `shouldBe` [10, 14]