選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Day11Spec.hs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. module Day11Spec (spec) where
  4. import Data.Foldable (toList)
  5. import qualified Data.Sequence as S
  6. import Data.Text (Text)
  7. import Day11
  8. import Day11.Parser
  9. import Test.Hspec
  10. import Text.Heredoc
  11. import Data.Int (Int64)
  12. testInput :: Text
  13. testInput =
  14. [str|Monkey 0:
  15. | Starting items: 79, 98
  16. | Operation: new = old * 19
  17. | Test: divisible by 23
  18. | If true: throw to monkey 2
  19. | If false: throw to monkey 3
  20. |
  21. |Monkey 1:
  22. | Starting items: 54, 65, 75, 74
  23. | Operation: new = old + 6
  24. | Test: divisible by 19
  25. | If true: throw to monkey 2
  26. | If false: throw to monkey 0
  27. |
  28. |Monkey 2:
  29. | Starting items: 79, 60, 97
  30. | Operation: new = old * old
  31. | Test: divisible by 13
  32. | If true: throw to monkey 1
  33. | If false: throw to monkey 3
  34. |
  35. |Monkey 3:
  36. | Starting items: 74
  37. | Operation: new = old + 3
  38. | Test: divisible by 17
  39. | If true: throw to monkey 0
  40. | If false: throw to monkey 1
  41. |]
  42. exampleMonkeys :: [Monkey]
  43. exampleMonkeys = parseMonkeys testInput
  44. spec :: Spec
  45. spec =
  46. describe "Day11" $ do
  47. describe "Part1" $ do
  48. it "parses" $ do
  49. head exampleMonkeys `shouldBe` Monkey 0 (S.fromList [79, 98]) (Multiply (Fixed 19)) 23 2 3 0
  50. _items (exampleMonkeys !! 1) `shouldBe` S.fromList [54, 65, 75, 74]
  51. it "runs part 1 monkey machine for 20 rounds" $ do
  52. let monkeys = runMonkeyMachine divideBy3 20 exampleMonkeys
  53. map _inspectionCount monkeys
  54. `shouldBe` [101, 95, 7, 105]
  55. map _inspectionCount (mostActiveMonkeys monkeys) `shouldBe` [105, 101]
  56. map _nr (mostActiveMonkeys monkeys) `shouldBe` [3, 0]
  57. monkeyBusiness (mostActiveMonkeys monkeys) `shouldBe` 10605
  58. it "runs part 2 monkey machine for 10000 rounds" $ do
  59. let monkeys = runMonkeyMachine (worryLimit exampleMonkeys) 10000 exampleMonkeys
  60. map _inspectionCount monkeys
  61. `shouldBe` [52166, 47830, 1938, 52013]
  62. monkeyBusiness (mostActiveMonkeys monkeys) `shouldBe` 2713310158