-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolutionSpec.hs
131 lines (124 loc) · 4.76 KB
/
SolutionSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module Day24.SolutionSpec (spec) where
import Data.Foldable (for_)
import qualified Data.HashSet as Set
import qualified Data.IntMap.Strict as IntMap
import Day24.Solution
( Coordinates (..),
Neighbor (..),
asCoordinates,
asFlippedTileSet,
livingArtDay,
parseTilePaths,
part1,
part2,
)
import Test.Hspec
spec :: Spec
spec = parallel $ do
it "solves Part 1" $ do
input <- readFile "./test/Day24/input.txt"
part1 input `shouldBe` "275"
it "solves Part 2" $ do
input <- readFile "./test/Day24/input.txt"
part2 input `shouldBe` "3537"
let exampleTilePaths =
[ [SE, SE, NW, NE, NE, NE, W, SE, E, SW, W, SW, SW, W, NE, NE, W, SE, W, SW],
[NE, E, E, NE, SE, NW, NW, W, SW, NE, NE, W, NW, W, SE, W, NE, NW, SE, SW, E, SW],
[SE, SW, NE, SW, SW, SE, NW, W, NW, SE],
[NW, NW, NE, SE, E, SW, SW, NE, NE, W, NE, SW, W, NE, W, SE, SW, NE, SE, E, NE],
[SW, W, E, SW, NE, SW, NE, NW, SE, W, NW, NE, NE, SE, E, NW],
[E, E, SE, NW, SE, SW, SW, NE, NW, SW, NW, NW, SE, W, W, NW, SE, NE],
[SE, W, NE, NE, NE, NE, SE, NW, SE, W, NE, NW, W, W, SE],
[W, E, NW, W, W, E, SE, E, E, W, E, SW, W, W, NW, W, E],
[W, SW, E, E, SE, NE, NE, W, NW, W, NW, SE, NE, W, SE, NW, W, SE, SE, SE, NW, NE],
[NE, E, SW, SE, E, NW, W, SW, NW, SW, SW, NW],
[NE, NW, SW, W, SE, W, SW, NE, NE, NE, W, SE, NW, SE, NW, NE, SE, SE, NE, W],
[E, NE, W, NW, E, W, NE, SW, SE, W, NW, SW, E, NW, E, SW, NE, NW, SE, NW, SW],
[SW, E, NE, SW, NE, SW, NE, NE, E, NW, NE, W, E, NE, W, W, NE, SW, SW, NE, SE],
[SW, W, E, SE, NE, SE, W, E, NW, NE, SW, NW, W, NE, SE, SW, W, NE],
[E, NE, SE, NW, SW, W, SW, NE, NE, SW, SE, NW, NE, W, SW, SE, E, NW, SE, SE],
[W, NW, NE, SE, NE, SE, NE, NW, W, NE, NW, SE, W, E, SE, W, SE, SE, SE, W],
[NE, NE, W, SW, NW, E, W, SW, NE, NE, SE, NW, NE, SE, W, E, SW],
[E, NE, SW, NW, SW, NW, SE, NE, NW, NW, NW, W, SE, E, SW, NE, E, W, SE, NE, SE],
[NE, SW, NW, E, W, NW, NW, SE, E, NW, SE, E, SE, W, SE, NW, SW, E, E, W, E],
[W, SE, W, E, E, E, NW, NE, SE, NW, W, W, SW, NE, W]
]
describe "parseTilePaths" $ do
it "parses the example" $ do
input <- readFile "./test/Day24/example.txt"
parseTilePaths input `shouldBe` Right exampleTilePaths
let coordinates =
[ Coordinates (-3, 1, 2),
Coordinates (1, 2, -3),
Coordinates (-3, 0, 3),
Coordinates (2, 0, -2),
Coordinates (1, 1, -2),
Coordinates (-1, 1, 0),
Coordinates (1, 2, -3),
Coordinates (-2, 2, 0),
Coordinates (0, 1, -1),
Coordinates (-2, 1, 1),
Coordinates (0, 2, -2),
Coordinates (0, 2, -2),
Coordinates (3, 0, -3),
Coordinates (-1, 1, 0),
Coordinates (0, -2, 2),
Coordinates (0, 0, 0),
Coordinates (1, 1, -2),
Coordinates (2, 0, -2),
Coordinates (2, -2, 0),
Coordinates (-1, 2, -1)
]
describe "asCoordinates" $ do
it "reduces a materialized path into coordinates" $ do
asCoordinates [NW, W, SW, E, E] `shouldBe` Coordinates (0, 0, 0)
it "reduces an empty list to (0,0,0)" $ do
asCoordinates [] `shouldBe` Coordinates (0, 0, 0)
it "works on the example" $ do
map asCoordinates exampleTilePaths `shouldBe` coordinates
let exampleFlippedTileSet =
Set.fromList
[ Coordinates (-3, 1, 2),
Coordinates (-3, 0, 3),
Coordinates (-2, 2, 0),
Coordinates (0, 1, -1),
Coordinates (-2, 1, 1),
Coordinates (3, 0, -3),
Coordinates (0, -2, 2),
Coordinates (0, 0, 0),
Coordinates (2, -2, 0),
Coordinates (-1, 2, -1)
]
describe "asFlippedTileSet" $ do
it "is the result of flipping tiles" $ do
asFlippedTileSet coordinates `shouldBe` exampleFlippedTileSet
it "has 10 Black tiles" $ do
length exampleFlippedTileSet `shouldBe` 10
describe "livingArtDay" $ do
let tileMaps = livingArtDay 100 exampleFlippedTileSet
let cases =
[ (0, 10),
(1, 15),
(2, 12),
(3, 25),
(4, 14),
(5, 23),
(6, 28),
(7, 41),
(8, 37),
(9, 49),
(10, 37),
(20, 132),
(30, 259),
(40, 406),
(50, 566),
(60, 788),
(70, 1106),
(80, 1373),
(90, 1844),
(100, 2208)
] ::
[(Int, Int)]
let test (n, expected) = it ("Day " ++ show n ++ ": " ++ show expected) $ do
(length . (IntMap.! n)) tileMaps `shouldBe` expected
for_ cases test