Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _04._3DSlices
- {
- class Program
- {
- static void Main(string[] args)
- {
- string dimensions = Console.ReadLine();
- string[] sizes = dimensions.Split();
- int width = int.Parse(sizes[0]);
- int height = int.Parse(sizes[1]);
- int depth = int.Parse(sizes[2]);
- int[, ,] cube = new int[width, height, depth];
- long totalSum = 0;
- for (int i = 0; i < height; i++)
- {
- string row = Console.ReadLine();
- char[] seperator = {'|'};
- string[] numbers = row.Split(seperator);
- for (int j = 0; j < depth; j++)
- {
- string[] newLines = numbers[j].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- for (int k = 0; k < width; k++)
- {
- try //This was added because several tests return OutOfRangeException
- {
- int value = int.Parse(newLines[k]);
- cube[k, i, j] = value;
- totalSum += value;
- }
- catch (IndexOutOfRangeException)
- {
- Console.WriteLine("i {0}, j {1}, k {2}", i, j, k);
- throw;
- }
- }
- }
- }
- if (width == 1 && height == 1 && depth == 1)
- {
- Console.WriteLine(0);
- }
- else
- {
- int heightCombinations = CheckHeight(cube, width, height, depth, totalSum);
- int depthCombinations = CheckDepth(cube, width, height, depth, totalSum);
- int widthCombinations = CheckWidth(cube, width, height, depth, totalSum);
- Console.WriteLine(heightCombinations + depthCombinations + widthCombinations);
- }
- //for (int i = 0; i < height; i++)
- //{
- // for (int j = 0; j < depth; j++)
- // {
- // for (int k = 0; k < width; k++)
- // {
- // Console.Write(cube[k, i, j]);
- // }
- // Console.Write('|');
- // }
- // Console.WriteLine();
- //}
- }
- static int CheckWidth(int[, ,] cube, int width, int height, int depth, long totalSum)
- {
- int combinations = 0;
- int totalWidthSum = 0;
- for (int w = 0; w < width - 1; w++)
- {
- for (int h = 0; h < height; h++)
- {
- for (int d = 0; d < depth; d++)
- {
- totalWidthSum += cube[w, h, d];
- }
- }
- if (totalWidthSum * 2 == totalSum)
- {
- combinations++;
- }
- }
- return combinations;
- }
- static int CheckDepth(int[, ,] cube, int width, int height, int depth, long totalSum)
- {
- int combinations = 0;
- int totalDepthSum = 0;
- for (int d = 0; d < depth - 1; d++)
- {
- for (int h = 0; h < height; h++)
- {
- for (int w = 0; w < width; w++)
- {
- totalDepthSum += cube[w, h, d];
- }
- }
- if (totalDepthSum * 2 == totalSum)
- {
- combinations++;
- }
- }
- return combinations;
- }
- static int CheckHeight(int[, ,] cube, int width, int height, int depth, long totalSum)
- {
- int combinations = 0;
- int totalHeightSum = 0;
- for (int i = 0; i < height - 1; i++)
- {
- for (int j = 0; j < depth; j++)
- {
- for (int k = 0; k < width; k++)
- {
- totalHeightSum += cube[k, i, j];
- }
- }
- if (totalHeightSum*2 == totalSum)
- {
- combinations++;
- }
- }
- return combinations;
- }
- }
- }
Add Comment
Please, Sign In to add comment