| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace D06._1
- {
- 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(@"(?<verb>turn on|turn off|toggle) (?<x1>\d+),(?<y1>\d+) through (?<x2>\d+),(?<y2>\d+)");
- const int size = 1000;
- var lights = new bool[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] = true; break;
- case "turn off": lights[x, y] = false; break;
- case "toggle": lights[x, y] = ! lights[x, y]; break;
- }
- }
- }
- }
- var answer = 0;
- for (int x = 0; x < size; ++x)
- for (int y = 0; y < size; ++y)
- if (lights[x, y]) answer++;
- Console.WriteLine($"The answer is : {answer}");
- }
- }
- }
|