| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace D22._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) return;
- if (File.Exists(args[0]) == false) return;
- var file = File.OpenText(args[0]);
- int depth = int.Parse(file.ReadLine().Substring("depth: ".Length));
- int[] arrayCoord = file.ReadLine().Substring("target: ".Length).Split(",").Select(s => int.Parse(s)).ToArray();
- (int x, int y) coord = (arrayCoord[0], arrayCoord[1]);
- file.Close();
- Dictionary<(int, int), int> erosionLevels = new Dictionary<(int, int), int>();
- int risk = 0;
- for (int i = 0; i <= Math.Min(coord.x, coord.y); ++i)
- {
- for (int y = i; y <= coord.y; ++y)
- risk += ComputeRisk(coord, erosionLevels, (i, y), depth);
- for (int x = i + 1; x <= coord.x; ++x)
- risk += ComputeRisk(coord, erosionLevels, (x, i), depth);
- }
- Console.WriteLine($"Total risk is : {risk}");
- }
- private static int ComputeRisk((int x, int y) coord, Dictionary<(int, int), int> erosionLevels, (int x, int y) pos, int depth)
- {
- int geo = 0;
- if (pos != (0, 0) && pos != coord)
- {
- if (pos.x == 0) geo = pos.y * 48271;
- else if (pos.y == 0) geo = pos.x * 16807;
- else geo = erosionLevels[(pos.x - 1, pos.y)] * erosionLevels[(pos.x, pos.y - 1)];
- }
- int ero = (geo + depth) % 20183;
- int type = ero % 3;
- erosionLevels.Add(pos, ero);
- return type;
- }
- }
- }
|