-
Notifications
You must be signed in to change notification settings - Fork 3
/
robot_test_results.go
82 lines (71 loc) · 1.31 KB
/
robot_test_results.go
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
package model
//go:generate generator
type RobotTestResults struct {
Failed bool
ReachedFinish bool
Moves []RobotLogEntry
}
type RobotDirection int
const (
Up RobotDirection = iota
Right
Down
Left
)
type Position struct {
X,
Y int
}
type RobotEvent string
const (
Move RobotEvent = RobotEvent("MOVE")
OutOfBounds = RobotEvent("OUT_OF_BOUNDS")
Obstacle = RobotEvent("HIT_OBSTACLE")
MissedCoin = RobotEvent("MISSED_COIN")
WrongPick = RobotEvent("WRONG_PICK")
Picked = RobotEvent("PICKED")
)
type RobotLogEntry struct {
Position
Event RobotEvent
}
func (p *RobotLogEntry) Validate(m *Map) bool {
if p.X < 0 || p.Y < 0 || p.X > m.Max.X || p.X > m.Max.Y {
p.Event = OutOfBounds
return false
}
for _, val := range m.Obstacles {
if p.X == val.X && p.Y == val.Y {
p.Event = Obstacle
return false
}
}
return true
}
func (p RobotLogEntry) Move(d RobotDirection) RobotLogEntry {
tp := p
tp.Event = Move
switch d {
case Up:
tp.X--
case Down:
tp.X++
case Left:
tp.Y--
case Right:
tp.Y++
}
return tp
}
type Map struct {
Start, Finish, Max Position
Coins, Obstacles []Position
}
func (m Map) CoinAt(p Position) bool {
for _, coin := range m.Coins {
if p == coin {
return true
}
}
return false
}