Skip to content

Commit cb69c0b

Browse files
author
schoenk
committed
From position 0,0, 2 recursive methods are called: right and down.
From those positions 4 recursive methods are called: right, down, left, and up. Test works with simple mazes. Still need to test for more complex, larger mazes. Maze solver now saves possible paths into a list of arrays containing pointers. Path translator now finds the shortest path, checks if there are other paths with the same lenght. If positive, it stores them in a list. Then, it translates the pointer coordinates into the output numbers required and adds them to the string that contains the final result. I still need to move the path translator into the pointer class.
1 parent 26d034e commit cb69c0b

File tree

3 files changed

+191
-78
lines changed

3 files changed

+191
-78
lines changed

Week 1/Karen/Week1_Maze/Maze_Test/Maze_UnitTest.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Text;
33
using System.Collections.Generic;
4+
using System.Reflection;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56
using Week1_Maze;
7+
using Pointer = Week1_Maze.Pointer;
68

79
namespace Maze_Test
810
{
@@ -18,7 +20,7 @@ public class Maze_UnitTest
1820
[TestMethod]
1921
public void TextFileWasReadIntoArray()
2022
{
21-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input0.txt");
23+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input0.txt");
2224

2325
int expected = 6;
2426
int actual = myMaze1.GetDesiredEndResultNum();
@@ -40,7 +42,7 @@ public void TextFileWasReadIntoArray()
4042
[TestMethod]
4143
public void MatrixZeroWasCreated()
4244
{
43-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input0.txt");
45+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input0.txt");
4446

4547
int expected = 6;
4648
int actual = myMaze1.GetDesiredEndResultNum();
@@ -66,7 +68,7 @@ public void MatrixZeroWasCreated()
6668
[TestMethod]
6769
public void MatrixOneWasCreated()
6870
{
69-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input1.txt");
71+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input1.txt");
7072

7173
int expected = 6;
7274
int actual = myMaze1.GetDesiredEndResultNum();
@@ -114,7 +116,7 @@ public void MatrixOneWasCreated()
114116
[TestMethod]
115117
public void SolveMazeEmpty()
116118
{
117-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\InputEmpty.txt");
119+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\InputEmpty.txt");
118120
myMaze1.SolveMaze();
119121

120122
int expectedAmountOfNumsInPath = 0;
@@ -129,7 +131,7 @@ public void SolveMazeEmpty()
129131
[TestMethod]
130132
public void SolveMazeThree()
131133
{
132-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input3.txt");
134+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input3.txt");
133135
myMaze1.SolveMaze();
134136

135137
int expectedAmountOfNumsInPath = 1;
@@ -144,7 +146,7 @@ public void SolveMazeThree()
144146
[TestMethod]
145147
public void SolveMazeZero()
146148
{
147-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input0.txt");
149+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input0.txt");
148150
myMaze1.SolveMaze();
149151

150152
int expectedAmountOfNumsInPath = 3;
@@ -155,5 +157,50 @@ public void SolveMazeZero()
155157
string actualPath = myMaze1.GetPath();
156158
Assert.AreEqual(expectedPath, actualPath);
157159
}
160+
161+
[TestMethod]
162+
public void SolveMazeOne()
163+
{
164+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input1.txt");
165+
myMaze1.SolveMaze();
166+
167+
int expectedAmountOfNumsInPath = 7;
168+
int actualAmountOfNumsInPath = myMaze1.GetAmountOfNumsInPath();
169+
Assert.AreEqual(expectedAmountOfNumsInPath, actualAmountOfNumsInPath);
170+
171+
string expectedPath = "1 4 5 2 3 6 9";
172+
string actualPath = myMaze1.GetPath();
173+
Assert.AreEqual(expectedPath, actualPath);
174+
}
175+
176+
[TestMethod]
177+
public void SolveMazeTwo()
178+
{
179+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input2.txt");
180+
myMaze1.SolveMaze();
181+
182+
int expectedAmountOfNumsInPath = 15;
183+
int actualAmountOfNumsInPath = myMaze1.GetAmountOfNumsInPath();
184+
Assert.AreEqual(expectedAmountOfNumsInPath, actualAmountOfNumsInPath);
185+
186+
string expectedPath = "1 2 3 4 9 14 13 12 17 22 23 24 25 20 25";
187+
string actualPath = myMaze1.GetPath();
188+
Assert.AreEqual(expectedPath, actualPath);
189+
}
190+
191+
[TestMethod]
192+
public void populateListOfPointers()
193+
{
194+
List<Pointer> myList = new List<Pointer>();
195+
myList.Add(new Pointer(2,1));
196+
197+
int expectedY = 2;
198+
int actualY = myList[0].getY();
199+
Assert.AreEqual(expectedY, actualY);
200+
201+
int expectedX = 1;
202+
int actualX = myList[0].getX();
203+
Assert.AreEqual(expectedX, actualX);
204+
}
158205
}
159206
}

Week 1/Karen/Week1_Maze/Week1_Maze/Pointer.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,37 @@
66

77
namespace Week1_Maze
88
{
9-
class Pointer
9+
public class Pointer
1010
{
11+
private int y;
12+
private int x;
13+
14+
public Pointer() { }
15+
16+
public Pointer(int y, int x)
17+
{
18+
this.y = y;
19+
this.x = x;
20+
}
21+
22+
public int getY()
23+
{
24+
return y;
25+
}
26+
27+
public int getX()
28+
{
29+
return x;
30+
}
31+
32+
public void setY(int y)
33+
{
34+
y = this.y;
35+
}
36+
37+
public void setX(int x)
38+
{
39+
x = this.x;
40+
}
1141
}
1242
}

Week 1/Karen/Week1_Maze/Week1_Maze/Program.cs

Lines changed: 107 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
using System;
3636
using System.Collections.Generic;
3737
using System.Collections.Specialized;
38+
using System.Diagnostics.Eventing.Reader;
3839
using System.Dynamic;
3940
using System.IO;
4041
using System.Linq;
4142
using System.Net;
43+
using System.Runtime.CompilerServices;
4244
using System.Text;
4345
using System.Threading.Tasks;
4446

@@ -55,8 +57,8 @@ public class Maze
5557
private int mazeSize;
5658

5759
// later, save all correct paths, compare sizes and print optimal paths
58-
int shortestListSize = 0;
59-
List<List<int>> possiblePaths = new List<List<int>>();
60+
private int shortestListSize = 0;
61+
private List<Pointer[]> possiblePaths = new List<Pointer[]>();
6062

6163
public Maze() { }
6264

@@ -130,6 +132,16 @@ public string GetMatrix(int row, int column)
130132
return matrix[row, column];
131133
}
132134

135+
public List<Pointer[]> GetPossiblePaths()
136+
{
137+
return possiblePaths;
138+
}
139+
140+
// It creates a matrix with this coordinates
141+
// Y,X
142+
// 0,0 | 0,1 | 0,2 ...
143+
// 1,0 | 1,1 | 1,2 ...
144+
// 2,0 | 2,1 | 2,2 ...
133145
private void CreateMatrix(string[] lines)
134146
{
135147
for (int rowNum = 1; rowNum < mazeSize; rowNum++)
@@ -163,88 +175,54 @@ public void SolveMaze()
163175
}
164176
else
165177
{
166-
List<int> yxPath = new List<int>(); // saves path as y*10 and x*1
167-
ReachTargetNumber(currentY, currentX, current, yxPath);
178+
List<Pointer> yxPath = new List<Pointer>(); // saves path as y*10 and x*1
179+
180+
// It tries to find a path right and down
181+
ReachTargetNumber(currentY, currentX +1, current, yxPath);
182+
ReachTargetNumber(currentY + 1, currentX, current, yxPath);
168183
}
184+
185+
TranslatePossiblePaths();
169186
}
170187
}
171188

172189

173-
public void ReachTargetNumber(int currentY, int currentX, int current, List<int> yxPath)
190+
public void ReachTargetNumber(int currentY, int currentX, int current, List<Pointer> yxPath)
174191
{
175-
// Checks if we have reach the bottom right of the maze
176-
if (currentY == mazeSize - 2 && currentX == mazeSize - 2)
192+
// Next if statements check if we are at the edge of the maze or inside
193+
if (currentX <= mazeSize - 2 && currentY <= mazeSize - 2 && currentX >= 0 && currentY >= 0 && !(currentY == 0 && currentX == 0))
177194
{
178-
if (current == desiredEndResultNum)
179-
{
180-
possiblePaths.Add(yxPath);
181-
return;
182-
}
183-
// Next if statements check if we are at the edge of the maze or inside
184-
else
185-
{
186-
// gets next right number -->
187-
if (currentX <= mazeSize - 3)
188-
{
189-
currentX++;
190-
string integerString = matrix[currentY, currentX];
191-
char nextOperator = integerString[0];
192-
int nextNum = Convert.ToInt32(integerString.Substring(1));
193-
194-
// Performs the operation and updates the position
195-
current = Calculator(current, nextOperator, nextNum);
195+
yxPath.Add(new Pointer(currentY, currentX));
196+
string integerString = matrix[currentY, currentX];
197+
char nextOperator = integerString[0];
198+
int nextNum = Convert.ToInt32(integerString.Substring(1));
196199

197-
// Calls the function to see if we have reach our goal
198-
ReachTargetNumber(currentY, currentX, current, yxPath);
199-
}
200+
// Performs the operation and updates the position
201+
current = Calculator(current, nextOperator, nextNum);
200202

201-
// gets next down number
202-
if (currentY <= mazeSize - 3)
203-
{
204-
currentY++;
205-
string integerString = matrix[currentY, currentX];
206-
char nextOperator = integerString[0];
207-
int nextNum = Convert.ToInt32(integerString.Substring(1));
208-
209-
// Performs the operation and updates the position
210-
current = Calculator(current, nextOperator, nextNum);
211-
212-
// Calls the function to see if we have reach our goal
213-
ReachTargetNumber(currentY, currentX, current, yxPath);
214-
}
215-
216-
// gets next left number <--
217-
// Once it has reach the bottom right, it won't keep going back into the maze
218-
if (currentX >= 0 && currentX <= mazeSize - 3 && currentY <= mazeSize - 3)
203+
// Checks if we have reach the bottom right of the maze & have reach our goal
204+
if (currentY == mazeSize - 2 && currentX == mazeSize - 2)
205+
{
206+
if (current == desiredEndResultNum)
219207
{
220-
currentX--;
221-
string integerString = matrix[currentY, currentX];
222-
char nextOperator = integerString[0];
223-
int nextNum = Convert.ToInt32(integerString.Substring(1));
224-
225-
// Performs the operation and updates the position
226-
current = Calculator(current, nextOperator, nextNum);
227-
228-
// Calls the function to see if we have reach our goal
229-
ReachTargetNumber(currentY, currentX, current, yxPath);
208+
Pointer[] addableList = new Pointer[yxPath.Count];
209+
addableList = yxPath.ToArray();
210+
possiblePaths.Add(addableList);
211+
yxPath.Clear();
230212
}
231-
232-
// gets next up number
233-
// Once it has reach the bottom right, it won't keep going back into the maze
234-
if (currentY >= 0 && currentX <= mazeSize - 3 && currentY <= mazeSize - 3)
213+
else
235214
{
236-
currentY--;
237-
string integerString = matrix[currentY, currentX];
238-
char nextOperator = integerString[0];
239-
int nextNum = Convert.ToInt32(integerString.Substring(1));
240-
241-
// Performs the operation and updates the position
242-
current = Calculator(current, nextOperator, nextNum);
243-
244-
// Calls the function to see if we have reach our goal
245-
ReachTargetNumber(currentY, currentX, current, yxPath);
215+
yxPath.Clear();
246216
}
247217
}
218+
else
219+
{
220+
// it tries to find a path right, down, left, and up.
221+
ReachTargetNumber(currentY, currentX + 1, current, yxPath);
222+
ReachTargetNumber(currentY + 1, currentX, current, yxPath);
223+
ReachTargetNumber(currentY, currentX - 1, current, yxPath);
224+
ReachTargetNumber(currentY - 1, currentX, current, yxPath);
225+
}
248226
}
249227
}
250228

@@ -269,13 +247,71 @@ public int Calculator(int current, char nextRightOperator, int nextRightNum)
269247
return current + nextRightNum;
270248
}
271249
}
250+
251+
public void TranslatePossiblePaths()
252+
{
253+
if (possiblePaths != null)
254+
{
255+
// finds the lenght of the shortest possible path
256+
int shortestPathLenght = possiblePaths[0].Length;
257+
for (int i = 1; i < possiblePaths.Count; i++)
258+
{
259+
if (possiblePaths[i].Length < shortestPathLenght)
260+
{
261+
shortestPathLenght = possiblePaths[i].Length;
262+
}
263+
}
264+
265+
// saves all correct paths of the shortest lenght
266+
List<Pointer[]> listOfShortPaths = new List<Pointer[]>();
267+
for (int i = 0; i < possiblePaths.Count; i++)
268+
{
269+
if (possiblePaths[i].Length == shortestPathLenght)
270+
{
271+
listOfShortPaths.Add(possiblePaths[i]);
272+
}
273+
}
274+
275+
// saves path lenght
276+
amountOfNumsInPath = shortestPathLenght;
277+
278+
// translate path pointers
279+
for (int pathNumber = 0; pathNumber < listOfShortPaths.Count; pathNumber++)
280+
{
281+
path += "1";
282+
for (int arrayIndex = 1; arrayIndex < listOfShortPaths[pathNumber].Length; arrayIndex++)
283+
{
284+
if (listOfShortPaths[pathNumber][arrayIndex].getY() == 0)
285+
{
286+
path += " " + listOfShortPaths[pathNumber][arrayIndex].getX() + 1;
287+
}
288+
else
289+
{
290+
for (int y = 1; y <= mazeSize - 2; y++)
291+
{
292+
if (listOfShortPaths[pathNumber][arrayIndex].getY() == y)
293+
{
294+
path += " " + (mazeSize * y - (y - 1)) +
295+
listOfShortPaths[pathNumber][arrayIndex].getX();
296+
}
297+
}
298+
}
299+
}
300+
301+
if (pathNumber != listOfShortPaths.Count - 1)
302+
{
303+
path += "\n";
304+
}
305+
}
306+
}
307+
}
272308
}
273309

274310
class Program
275311
{
276312
static void Main(string[] args)
277313
{
278-
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\Visual Studio 2015\Projects\Week1_Maze\Week1_Maze\Input0.txt");
314+
Maze myMaze1 = new Maze(@"C:\Users\ana_k\Documents\CodingChallenges\Week 1\Karen\Week1_Maze\Week1_Maze\Input0.txt");
279315

280316
}
281317
}

0 commit comments

Comments
 (0)