-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
90 lines (79 loc) · 2.66 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace _4_kyu__Path_Finder_4__where_are_you
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(PathFinder.iAmHere("RLrl"));
Console.WriteLine(PathFinder.iAmHere("r5L2l4"));
Console.WriteLine(PathFinder.iAmHere("r5L2l4"));
Console.WriteLine(PathFinder.iAmHere("10r5r0"));
Console.WriteLine(PathFinder.iAmHere("10r5r0"));
Console.WriteLine(PathFinder.iAmHere("229LLr314lrrrl439lLrRLrrRrRllrRl347LrR"));
Console.WriteLine(PathFinder.iAmHere("RL708rRllL670RRrL"));
Console.Read();
}
}
public class PathFinder
{
// 0:N 1:E 2:S 3:W
static int direction_index = 3;
static Point objPoint = new Point(0, 0);
public static Point iAmHere(string path)
{
path = path.Replace("L", " L ");
path = path.Replace("l", " l ");
path = path.Replace("R", " R ");
path = path.Replace("r", " r ");
string[] path_array = path.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string objPath in path_array)
{
if ("LR".IndexOf(objPath) > -1)
{
direction_index += 2;
}
else if ("l".IndexOf(objPath) > -1)
{
direction_index -= 1;
}
else if ("r".IndexOf(objPath) > -1)
{
direction_index += 1;
}
else
{
if (direction_index == 0)
{
objPoint.Y += Convert.ToInt32(objPath);
}
else if (direction_index == 1)
{
objPoint.X += Convert.ToInt32(objPath);
}
else if (direction_index == 2)
{
objPoint.Y -= Convert.ToInt32(objPath);
}
else if (direction_index == 3)
{
objPoint.X -= Convert.ToInt32(objPath);
}
}
if (direction_index > 3)
{
direction_index = direction_index - 4;
}
else if (direction_index < 0)
{
direction_index = 4 + direction_index;
}
}
return objPoint;
}
}
}