-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem28.cs
More file actions
77 lines (63 loc) · 1.47 KB
/
Problem28.cs
File metadata and controls
77 lines (63 loc) · 1.47 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectEulerTom {
public class Problem28 {
enum Direction { Up, Down, Left, Right }
public static void Run() {
const int rows = 5;
var nums = new int[5][];
var currentX = rows / 2;
var currentY = currentX;
var direction = Direction.Right;
var count = 1;
var changeDirection = false;
var movementCount = 1;
var numAtMovement = 0;
for (var i = 0; i < rows; i++) {
nums[i] = new int[rows];
}
while (currentX != rows && currentY != 0) {
nums[currentY][currentX] = count;
changeDirection = count == 1 || (count - numAtMovement) % movementCount == 0;
movementCount++;
switch (direction) {
case Direction.Right:
currentX += 1;
if (changeDirection) {
direction = Direction.Down;
}
break;
case Direction.Down:
currentY += 1;
if (changeDirection) {
direction = Direction.Left;
}
break;
case Direction.Left:
currentX -= 1;
if (changeDirection) {
direction = Direction.Up;
}
break;
case Direction.Up:
currentY -= 1;
if (changeDirection) {
direction = Direction.Right;
}
break;
}
if (changeDirection) {
movementCount = 1;
numAtMovement = count;
}
count++;
}
foreach (var row in nums) {
Console.WriteLine(String.Join(" ", row));
}
Console.ReadKey();
}
}
}