-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwelveTest.hs
103 lines (90 loc) · 2.18 KB
/
TwelveTest.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
module TwelveTest (main) where
import Protolude
import Test.Hspec hiding (shouldBe)
import Test.Hspec.Expectations.Pretty (shouldBe)
import Text.RawString.QQ
import Twelve
main :: IO ()
main = hspec $ do
describe "main" $ do
it "calculates the manhattan distance of the final ship position" $ do
let
input = [r|
F10
N3
F7
R90
F11
|]
result = run input
result `shouldBe` Right 286
describe "parseInput" $ do
it "parses input text into list of commands" $ do
let
input = [r|
F10
N3
F7
R90
F11
|]
expected = [
ForwardCmd 10
, NorthCmd 3
, ForwardCmd 7
, RightCmd 90
, ForwardCmd 11
]
result = parseInput input
result `shouldBe` Right expected
describe "moveToWaypoint" $ do
it "moves the ship to the waypoint, and pushes out the waypoint" $ do
let
initState = State {
shipX = 0
, shipY = 0
, waypointX = 10
, waypointY = 1
}
expected = State {
shipX = 10
, shipY = 1
, waypointX = 20
, waypointY = 2
}
result = moveToWaypoint initState
result `shouldBe` expected
describe "turnLeft" $ do
it "rotates the waypoint counterclockwise 90 deg around the ship" $ do
let
initState = State {
shipX = 2
, shipY = 2
, waypointX = 3
, waypointY = 3
}
expected = State {
shipX = 2
, shipY = 2
, waypointX = 1
, waypointY = 3
}
result = turnLeft initState
result `shouldBe` expected
context "with the waypoint on the same y-axis as the ship" $ do
it "rotates the waypoint counterclockwise 90 deg around the ship" $ do
let
initState = State {
shipX = 0
, shipY = 0
, waypointX = 3
, waypointY = 0
}
expected = State {
shipX = 0
, shipY = 0
, waypointX = 0
, waypointY = 3
}
result = turnLeft initState
result `shouldBe` expected