Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace ArrayStuff
- {
- class Program
- {
- static void Main(string[] args)
- {
- var arr = new int[6][]
- {
- new []{1,1,1,0,0,0},
- new []{0,1,0,0,0,0},
- new []{1,1,1,0,0,0},
- new []{0,0,-2,4,-4,0},
- new []{0,0,0,2,0,0},
- new []{0,0,1,2,4,0}
- };
- var result = hourglassSum(arr);
- Console.WriteLine(result);
- Console.ReadLine();
- }
- static int hourglassSum(int[][] arr)
- {
- int winningAddition = 0;
- for (int i = 0; i <= 3; i++)
- {
- for (int j = 0; j <= 3; j++)
- {
- var topHourglass = GetTopHourGlass(arr,i,j);
- var middleHourglass = arr[i + 1][j + 1];
- var bottomHourglass = GetBottomHourGlass(arr,i,j);
- var result = topHourglass.Sum() + middleHourglass + bottomHourglass.Sum();
- if (result > winningAddition)
- {
- winningAddition = result;
- }
- }
- }
- return winningAddition;
- }
- private static int[] GetBottomHourGlass(int[][] arr, int i, int j)
- {
- // i +2 j +2
- var newArray = arr[i +2].Skip(j).Take(3).ToArray();
- return newArray;
- }
- private static int[] GetTopHourGlass(int[][] arr,int i,int j)
- {
- var newArray = arr[i].Skip(j).Take(3).ToArray();
- return newArray;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement