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

Day10Spec.hs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. {-# LANGUAGE QuasiQuotes #-}
  2. module Day10Spec (spec) where
  3. import Control.Arrow ((>>>))
  4. import Day10
  5. import Test.Hspec
  6. import Text.Heredoc
  7. testInput :: String
  8. testInput = [str|noop
  9. |addx 3
  10. |addx -5
  11. |]
  12. testProgram :: [Instruction]
  13. testProgram = [Noop, Addx 3, Addx (-5)]
  14. testInput2 :: String
  15. testInput2 = [str|addx 15
  16. |addx -11
  17. |addx 6
  18. |addx -3
  19. |addx 5
  20. |addx -1
  21. |addx -8
  22. |addx 13
  23. |addx 4
  24. |noop
  25. |addx -1
  26. |addx 5
  27. |addx -1
  28. |addx 5
  29. |addx -1
  30. |addx 5
  31. |addx -1
  32. |addx 5
  33. |addx -1
  34. |addx -35
  35. |addx 1
  36. |addx 24
  37. |addx -19
  38. |addx 1
  39. |addx 16
  40. |addx -11
  41. |noop
  42. |noop
  43. |addx 21
  44. |addx -15
  45. |noop
  46. |noop
  47. |addx -3
  48. |addx 9
  49. |addx 1
  50. |addx -3
  51. |addx 8
  52. |addx 1
  53. |addx 5
  54. |noop
  55. |noop
  56. |noop
  57. |noop
  58. |noop
  59. |addx -36
  60. |noop
  61. |addx 1
  62. |addx 7
  63. |noop
  64. |noop
  65. |noop
  66. |addx 2
  67. |addx 6
  68. |noop
  69. |noop
  70. |noop
  71. |noop
  72. |noop
  73. |addx 1
  74. |noop
  75. |noop
  76. |addx 7
  77. |addx 1
  78. |noop
  79. |addx -13
  80. |addx 13
  81. |addx 7
  82. |noop
  83. |addx 1
  84. |addx -33
  85. |noop
  86. |noop
  87. |noop
  88. |addx 2
  89. |noop
  90. |noop
  91. |noop
  92. |addx 8
  93. |noop
  94. |addx -1
  95. |addx 2
  96. |addx 1
  97. |noop
  98. |addx 17
  99. |addx -9
  100. |addx 1
  101. |addx 1
  102. |addx -3
  103. |addx 11
  104. |noop
  105. |noop
  106. |addx 1
  107. |noop
  108. |addx 1
  109. |noop
  110. |noop
  111. |addx -13
  112. |addx -19
  113. |addx 1
  114. |addx 3
  115. |addx 26
  116. |addx -30
  117. |addx 12
  118. |addx -1
  119. |addx 3
  120. |addx 1
  121. |noop
  122. |noop
  123. |noop
  124. |addx -9
  125. |addx 18
  126. |addx 1
  127. |addx 2
  128. |noop
  129. |noop
  130. |addx 9
  131. |noop
  132. |noop
  133. |noop
  134. |addx -1
  135. |addx 2
  136. |addx -37
  137. |addx 1
  138. |addx 3
  139. |noop
  140. |addx 15
  141. |addx -21
  142. |addx 22
  143. |addx -6
  144. |addx 1
  145. |noop
  146. |addx 2
  147. |addx 1
  148. |noop
  149. |addx -10
  150. |noop
  151. |noop
  152. |addx 20
  153. |addx 1
  154. |addx 2
  155. |addx 2
  156. |addx -6
  157. |addx -11
  158. |noop
  159. |noop
  160. |noop
  161. |]
  162. spec :: Spec
  163. spec =
  164. describe "Day10" $ do
  165. describe "Part1" $ do
  166. it "parses" $ do
  167. parseProgram testInput `shouldBe` testProgram
  168. it "executes" $ do
  169. executeProgram testProgram `shouldBe` [1,1,1,4,4,-1]
  170. it "executes larger program" $ do
  171. let registerValues = parseProgram >>> executeProgram $ testInput2
  172. registerValues !! 20 `shouldBe` 21
  173. registerValues !! 60 `shouldBe` 19
  174. registerValues !! 100 `shouldBe` 18
  175. registerValues !! 140 `shouldBe` 21
  176. registerValues !! 180 `shouldBe` 16
  177. registerValues !! 220 `shouldBe` 18
  178. it "computes signal strength" $ do
  179. let registerValues = parseProgram >>> executeProgram $ testInput2
  180. signalStrength registerValues `shouldBe` 13140
  181. it "prints a crt" $ do
  182. let registerValues = parseProgram >>> executeProgram $ testInput2
  183. putStrLn $ draw registerValues
  184. it "sprites drawn" $ do
  185. isSpriteDrawn 1 1 `shouldBe` True
  186. isSpriteDrawn 1 1 `shouldBe` True