Created
September 22, 2022 16:25
-
-
Save MinenhleNkosi/69fe16b9acbf69528c9b74d2bca07b65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description | |
You work for a company that sells robots that move around a factory floor in a grid like pattern. Your | |
boss has asked you to write a program that will test how many unique squares a robot visits given a | |
set of instructions. | |
For example the input N4, E2, S2, W4 (described below) should result in a value of 11. | |
From the starting position the robot heads north for four blocks, east for two blocks south for two and | |
west for four. During those steps it visits blocks 1-12 once except for block 2 which it visits twice. | |
Block 2 should only be counted once. The size of the grid is unknown and not given in the input. | |
# Instructions: | |
Write a C# console application that: | |
* Takes a command line parameter that is the sequence of step the robot will take | |
* Calculate the number of unique blocks that would be visited | |
* Output the result to the command line | |
**Optional Bonus Question:** also output the number of right hand turns the robot makes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Windows.Forms; | |
namespace sourcing | |
{ | |
public class Robot | |
{ | |
public static void Main(String[] args) | |
{ | |
//Instruction message for coordinates input for the user | |
string message = "For correct output, follow below guidelines:" + "\n" + | |
"Input alphabets (n, w, e, s) for directions." + | |
"Where: n = North, w = West, e = East, s = South" + "\n" + | |
"Input positive numbers for steps." | |
+ "\n" + "For Example: " + "Enter robot's coordinates: n4,e2,s2,w4"; | |
string title = "Coordinates Guideline"; | |
MessageBox.Show(message, title); | |
//Asking for user input | |
Console.Write("Enter robot's coordinates: "); | |
string? input = Console.ReadLine(); | |
input = input?.ToUpper(); | |
string[] arrOfCoord = input.Split(","); | |
//Creating x and y variable to track robot's movement | |
int x = 0; | |
int y = 0; | |
int right_hand_turns = 0; //Will keep track of robot's the right turn | |
//Create a hashset that will store non-duplicated cox and y coordinates | |
//of the robot's movements | |
HashSet<string> set = new HashSet<string>(); | |
foreach (string coordinates in arrOfCoord) | |
{ | |
//Getting the S, N, W, E characters for the robot directions | |
char direction = coordinates[0]; | |
/*Storing the second character of every element in "arrOfCoord" for | |
the robots movement */ | |
string count = coordinates.Substring(1); | |
int max = int.Parse(count); //Changing from string type to integer | |
if (direction == 'N') | |
{ | |
try | |
{ | |
for (int i = 0; i < max; i++) | |
{ | |
y += 1; | |
set.Add("[" + x + "][" + y + "]"); | |
} | |
} | |
catch (FormatException) | |
{ | |
Console.WriteLine("Invalid coordinates!!"); | |
} | |
} | |
if (direction == 'E') | |
{ | |
try | |
{ | |
for (int i = 0; i < max; i++) | |
{ | |
x += 1; | |
set.Add("[" + x + "][" + y + "]"); | |
right_hand_turns++; | |
} | |
} | |
catch (FormatException) | |
{ | |
Console.WriteLine("Invalid coordinates!!"); | |
} | |
} | |
if (direction == 'S') | |
{ | |
try | |
{ | |
for (int i = 0; i < max; i++) | |
{ | |
y -= 1; | |
set.Add("[" + x + "][" + y + "]"); | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Invalid coordinates!!"); | |
} | |
} | |
if (direction == 'W') | |
{ | |
try | |
{ | |
for (int i = 0; i < max; i++) | |
{ | |
x -= 1; | |
set.Add("[" + x + "][" + y + "]"); | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Invalid coordinates!!"); | |
} | |
} | |
} | |
Console.WriteLine("Total Robot Movement is : " + set.Count); | |
//For the optional bonus question | |
Console.WriteLine("Total Number of Right Turns : " + right_hand_turns); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment