| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace D01._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- string input = File.ReadAllText(args[0]);
- var visited = new HashSet<(int, int)>()
- {
- (0, 0)
- };
- (int x, int y, int r) coord = (0, 0, 0);
- foreach (var i in input.Split(", "))
- {
- int n = int.Parse(i.Substring(1));
- switch (i[0])
- {
- case 'R': coord.r += 90; break;
- case 'L': coord.r -= 90; break;
- }
- if (coord.r < 0) coord.r += 360;
- if (coord.r >= 360) coord.r -= 360;
- if (Visit(ref coord, n, visited) == false)
- break;
- }
- var manhattan = Math.Abs(coord.x) + Math.Abs(coord.y);
- Console.WriteLine($"Distance is : {manhattan}");
- }
- private static bool Visit(ref (int x, int y, int r) coord, int n, HashSet<(int, int)> visited)
- {
- for (var i = 0; i < n; ++i)
- {
- switch (coord.r)
- {
- case 0: coord.y --; break;
- case 90: coord.x ++; break;
- case 180: coord.y ++; break;
- case 270: coord.x --; break;
- }
- if (visited.Add((coord.x, coord.y)) == false)
- return false;
- }
- return true;
- }
- private static void HashSet(int v1, object x, int v2, object y, int v3, object r)
- {
- throw new NotImplementedException();
- }
- }
- }
|