-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem011.cs
86 lines (75 loc) · 2.2 KB
/
Problem011.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
namespace ProjectEuler.Solutions
{
using System;
using NUnit.Framework;
using ProjectEuler.Input;
/// <summary>
/// Largest product in a grid.
/// In the 20×20 grid, four numbers along a diagonal line have been marked in bold.
/// <para>What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?</para>
/// </summary>
/// <remarks>The grid is stored in the file <a href="Problem011.txt">Problem011.txt</a>.</remarks>
public class Problem011 : Problem
{
public override long Solution()
{
long largestProduct = 0;
string gridInput = Input.Problem011;
byte[,] grid = CreateGrid(gridInput);
int maxY = grid.GetUpperBound(0);
int maxX = grid.GetUpperBound(1);
for (int y = 0; y < maxY; y++)
{
for (int x = 0; x < maxX; x++)
{
if (x <= maxX - 3)
{
largestProduct = LargestProduct(largestProduct, grid, x, y, 1, 0);
if (y >= 3)
{
largestProduct = LargestProduct(largestProduct, grid, x, y, 1, -1);
}
if (y <= maxY - 3)
{
largestProduct = LargestProduct(largestProduct, grid, x, y, 1, 1);
}
}
if (y <= maxY - 3)
{
largestProduct = LargestProduct(largestProduct, grid, x, y, 0, 1);
}
}
}
return largestProduct;
}
private static byte[,] CreateGrid(string gridInput)
{
string[] rows = gridInput.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
byte[,] grid = new byte[20, 20];
for(int x = 0; x < 20; x++)
{
string[] row = rows[x].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for(int y = 0; y < 20; y++)
{
grid[x, y] = byte.Parse(row[y]);
}
}
return grid;
}
private static long LargestProduct(long currentLargest, byte[,] grid, int x, int y, int xOffset, int yOffset)
{
long currentProduct = grid[y, x] *
grid[y + yOffset, x + xOffset] *
grid[y + (yOffset * 2), x + (xOffset * 2)] *
grid[y + (yOffset * 3), x + (xOffset * 3)];
return currentLargest > currentProduct
? currentLargest
: currentProduct;
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(70600674, this.Solution());
}
}
}