using System; using System.IO; using System.Text.RegularExpressions; namespace D06._2 { class Program { static void Main(string[] args) { if (args.Length < 1) throw new ArgumentException(); if (File.Exists(args[0]) == false) throw new FileNotFoundException(); var regex = new Regex(@"(?turn on|turn off|toggle) (?\d+),(?\d+) through (?\d+),(?\d+)"); const int size = 1000; var lights = new int[size, size]; using (var file = File.OpenText(args[0])) { while (true) { var line = file.ReadLine(); if (line == null) break; var result = regex.Match(line); (int x, int y) p1 = (int.Parse(result.Groups["x1"].Value), int.Parse(result.Groups["y1"].Value)); (int x, int y) p2 = (int.Parse(result.Groups["x2"].Value), int.Parse(result.Groups["y2"].Value)); var verb = result.Groups["verb"].Value; for (int x = p1.x; x <= p2.x; ++x) for (int y = p1.y; y <= p2.y; ++y) { switch (verb) { case "turn on": lights[x, y]++; break; case "turn off": lights[x, y] = Math.Max(lights[x, y] - 1, 0); break; case "toggle": lights[x, y] = lights[x, y] += 2; break; } } } } var answer = 0; for (int x = 0; x < size; ++x) for (int y = 0; y < size; ++y) answer += lights[x, y]; Console.WriteLine($"The answer is : {answer}"); } } }