Jens Kadenbach před 2 roky
rodič
revize
17e4d9d0ab
5 změnil soubory, kde provedl 415 přidání a 1 odebrání
  1. 2
    0
      aoc2022.cabal
  2. 139
    0
      ressources/day10-input
  3. 79
    0
      src/Day10.hs
  4. 2
    1
      src/Lib.hs
  5. 193
    0
      test/Day10Spec.hs

+ 2
- 0
aoc2022.cabal Zobrazit soubor

@@ -27,6 +27,7 @@ library
27 27
   exposed-modules:
28 28
       Day1
29 29
       Day1.Internal
30
+      Day10
30 31
       Day2
31 32
       Day2.Part1
32 33
       Day2.Part2
@@ -100,6 +101,7 @@ test-suite aoc2022-test
100 101
   type: exitcode-stdio-1.0
101 102
   main-is: Spec.hs
102 103
   other-modules:
104
+      Day10Spec
103 105
       Day1Spec
104 106
       Day2Spec
105 107
       Day3Spec

+ 139
- 0
ressources/day10-input Zobrazit soubor

@@ -0,0 +1,139 @@
1
+addx 1
2
+noop
3
+noop
4
+noop
5
+addx 5
6
+addx 5
7
+noop
8
+noop
9
+addx 9
10
+addx -5
11
+addx 1
12
+addx 4
13
+noop
14
+noop
15
+noop
16
+addx 6
17
+addx -1
18
+noop
19
+addx 5
20
+addx -2
21
+addx 7
22
+noop
23
+addx 3
24
+addx -2
25
+addx -38
26
+noop
27
+noop
28
+addx 32
29
+addx -22
30
+noop
31
+addx 2
32
+addx 3
33
+noop
34
+addx 2
35
+addx -2
36
+addx 7
37
+addx -2
38
+noop
39
+addx 3
40
+addx 2
41
+addx 5
42
+addx 2
43
+addx -5
44
+addx 10
45
+noop
46
+addx 3
47
+noop
48
+addx -38
49
+addx 1
50
+addx 27
51
+noop
52
+addx -20
53
+noop
54
+addx 2
55
+addx 27
56
+noop
57
+addx -22
58
+noop
59
+noop
60
+noop
61
+noop
62
+addx 3
63
+addx 5
64
+addx 2
65
+addx -11
66
+addx 16
67
+addx -2
68
+addx -17
69
+addx 24
70
+noop
71
+noop
72
+addx 1
73
+addx -38
74
+addx 15
75
+addx 10
76
+addx -15
77
+noop
78
+addx 2
79
+addx 26
80
+noop
81
+addx -21
82
+addx 19
83
+addx -33
84
+addx 19
85
+noop
86
+addx -6
87
+addx 9
88
+addx 3
89
+addx 4
90
+addx -21
91
+addx 4
92
+addx 20
93
+noop
94
+addx 3
95
+addx -38
96
+addx 28
97
+addx -21
98
+addx 9
99
+addx -8
100
+addx 2
101
+addx 5
102
+addx 2
103
+addx -9
104
+addx 14
105
+addx -2
106
+addx -5
107
+addx 12
108
+addx 3
109
+addx -2
110
+addx 2
111
+addx 7
112
+noop
113
+noop
114
+addx -27
115
+addx 28
116
+addx -36
117
+noop
118
+addx 1
119
+addx 5
120
+addx -1
121
+noop
122
+addx 6
123
+addx -1
124
+addx 5
125
+addx 5
126
+noop
127
+noop
128
+addx -2
129
+addx 20
130
+addx -10
131
+addx -3
132
+addx 1
133
+addx 3
134
+addx 2
135
+addx 4
136
+addx 3
137
+noop
138
+addx -30
139
+noop

+ 79
- 0
src/Day10.hs Zobrazit soubor

@@ -0,0 +1,79 @@
1
+module Day10
2
+  ( Instruction (..),
3
+    parseProgram,
4
+    signalStrength,
5
+    executeProgram,
6
+    draw,
7
+    isSpriteDrawn,
8
+    day10
9
+  )
10
+where
11
+
12
+import Control.Arrow ((>>>))
13
+import qualified Data.Matrix as M
14
+import Debug.Trace (trace)
15
+import Data.List.Split (chunksOf)
16
+import Data.List (intercalate)
17
+
18
+data Instruction = Addx Int | Noop
19
+  deriving (Show, Eq)
20
+
21
+newtype Pixel = Pixel Bool deriving (Eq)
22
+
23
+instance Show Pixel where
24
+  show (Pixel True) = "▓"
25
+  show (Pixel False) = "░"
26
+
27
+
28
+type CRT = String
29
+
30
+parseProgram :: String -> [Instruction]
31
+parseProgram = lines >>> map parseProgram'
32
+  where
33
+    parseProgram' :: String -> Instruction
34
+    parseProgram' "noop" = Noop
35
+    parseProgram' line = splitAt 4 >>> snd >>> read >>> Addx $ line
36
+
37
+
38
+isSpriteDrawn :: Int -> Int -> Bool
39
+isSpriteDrawn x p = abs (position - x) <= 1
40
+  where
41
+    position = p `mod` 40
42
+
43
+draw :: [Int] -> CRT
44
+draw registerValues = unlines lastPicture
45
+  where
46
+    pictures = zipWith isSpriteDrawn registerValues [0..]
47
+    lastPicture = concatMap (show . Pixel) >>> chunksOf 40 $ pictures
48
+
49
+
50
+
51
+signalStrength :: [Int] -> Int
52
+signalStrength e = start + sum computedRest
53
+  where
54
+    start = e !! 20 * 20
55
+    rest = drop 20 e
56
+    withCycle = zip [60, 100..] (every 40 rest)
57
+    computedRest = [c*r | (c,r) <- withCycle]
58
+
59
+every :: Int -> [a] -> [a]
60
+every n xs = case drop (n-1) xs of
61
+              y : ys -> y : every n ys
62
+              [] -> []
63
+
64
+executeProgram :: [Instruction] -> [Int]
65
+executeProgram = scanl (+) 1 . concatMap execOne
66
+  where
67
+    execOne instruction = case instruction of
68
+        Noop   -> [0]
69
+        Addx x -> [0, x]
70
+
71
+day10 :: IO ()
72
+day10 = do
73
+  input <- readFile "ressources/day10-input"
74
+  putStrLn "Day10"
75
+  let states = parseProgram >>> executeProgram $ input
76
+  let signal = signalStrength states
77
+  let crt = draw states
78
+  putStrLn ("Signal strength: " ++ show signal)
79
+  putStrLn ("CRT picture: \n" ++ crt)

+ 2
- 1
src/Lib.hs Zobrazit soubor

@@ -11,9 +11,10 @@ import Day6 (day6)
11 11
 import Day7 (day7)
12 12
 import Day8 (day8)
13 13
 import Day9 (day9)
14
+import Day10 (day10)
14 15
 
15 16
 days :: [IO ()]
16
-days = [day1, day2, day3, day4, day5, day6, day7, day8, day9]
17
+days = [day1, day2, day3, day4, day5, day6, day7, day8, day9, day10]
17 18
 
18 19
 sep :: IO ()
19 20
 sep = putStrLn "---------"

+ 193
- 0
test/Day10Spec.hs Zobrazit soubor

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

Načítá se…
Zrušit
Uložit