-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ps1
156 lines (148 loc) · 4.23 KB
/
main.ps1
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# It script not accept any argument for hour
if($args.Length -ne 0)
{
"Number of arguments > 0 but this script doesn't accept args"
exit
}
# Search for a Input file in the same folder
$InputPath = 'Input.txt'
if(-Not (Test-Path $InputPath))
{
"Cannot find the path: $InputPath. Exiting the application"
exit
}
# Retrieve the content of an input file filtering out empty lines
$Content = Get-Content $InputPath | Where {($_ -ne '')}
if($Content -eq $Null)
{
"Cannot retrieve the content from path: $InputPath. Exiting the application"
exit
}
# Split the first line of content in tokens
$Limits = $Content[0].Split(' ')
# Use base 10 to convert a string to an integer and set the limits of matrix
# MAXIMUM Y
[int]$LimitY = ([convert]::ToInt32($Limits[0], 10))
# MAXIMUM X
[int]$LimitX = ([convert]::ToInt32($Limits[1], 10))
# Set the number of rovers in the Mars plateau.
# In this case is two
$RoverNumbers = 2
# Set the first position and run the command string to each one of rovers
for($i=1; $i -le $RoverNumbers; $i++)
{
# $i is the index of rover
# Get position and orientation line split in tokens (Even lines)
$InitPositions = $Content[($i*2)-1].Split(' ')
# Store Initial Position in X and Y axis
# To increment or decrement a position is necessary convert it to an integer
[int]$PositionX = ([convert]::ToInt32($InitPositions[0], 10))
[int]$PositionY = ([convert]::ToInt32($InitPositions[1], 10))
# Store the initial position
[char]$Orientation = $InitPositions[2]
# Get command line (Odd lines)
$StringCommand = $Content[$i*2]
# Interpreting the command string like an array of char
Foreach($Letter in $StringCommand[0..$StringCommand.Length])
{
# Move over matrix
if($Letter -eq 'M')
{
# UP
if($Orientation -eq 'N')
{
if($PositionY -lt $LimitY)
{
$PositionY += 1;
}
}
# RIGTH
elseif($Orientation -eq 'E')
{
if($PositionY -lt $LimitX)
{
$PositionX += 1;
}
}
# DOWN
elseif($Orientation -eq 'S')
{
if($PositionY -gt 0)
{
$PositionY -= 1;
}
}
# LEFT
elseif($Orientation -eq 'W')
{
if($PositionX -gt 0)
{
$PositionX -= 1;
}
}
# If first set of orientation was invalid, enter here
else
{
"Orientation: $Orientation is not valid to move. The rover will not move."
}
}
# Set a new orientation
else
{
# Array to set the new orientation
$CardinalPoints = ('N','E','S','W')
# Get current orientation like pivot on $CardinalPoints array
$CurrentOrientationIndex = $CardinalPoints.Indexof("$Orientation")
if($CurrentOrientationIndex -eq (-1))
{
"Index of orientation $Orientation was not found on cardinal points array"
exit
}
# Validation of '$Letter' variable is case sensitive. So 'n' is not equal 'N' in comparison
if(($Letter -eq 'R') -or ($Letter -eq 'L'))
{
# Pointer to the next orientation
$NextIndex = ''
# Change the orientation of rover using a circle array
# Each command can change the orientation in 90 degrees only, not more
if($Letter -eq 'R')
{
# MAX INDEX 3
if($CurrentOrientationIndex -lt 3)
{
$NextIndex = $CurrentOrientationIndex+1
}
# RETURN TO INDEX 0
else
{
$NextIndex = 0
}
}
# $Letter equal 'L'
else
{
# MIN INDEX 0.
if($CurrentOrientationIndex -gt 0)
{
$NextIndex = $CurrentOrientationIndex-1
}
# RETURN TO INDEX 3
else
{
$NextIndex = 3
}
}
$Orientation = $CardinalPoints[$NextIndex]
}
else
{
# Alert some error in command string
"Invalid orientation can not be set with letter [$Letter]"
}
}
# Store the final position of the rover on matrix followed by the orientation
$FinalPosition = "$PositionX $PositionY $Orientation"
}
# Add the result like the content of Output.txt file
$FinalPosition | Add-Content 'Output.txt'
}