Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1.  
  2.             HashSet<(int x, int y)> wireone = new HashSet<(int x, int y)>();
  3.  
  4.             Dictionary<(int, int), int> stepsone = new Dictionary<(int, int), int>();
  5.  
  6.             int x = 0;
  7.             int y = 0;
  8.             int totalstep = 0;
  9.             foreach( string step in wires[0].Split(','))
  10.             {
  11.                 string dir = step[0].ToString();
  12.                 int n = Convert.ToInt32( step.Substring(1) );
  13.  
  14.                 int xd=0;
  15.                 int yd=0;
  16.                 if( dir == "U") {  xd = 0; yd = -1;}
  17.                 if (dir == "D") { xd = 0; yd = 1; }
  18.                 if (dir == "L") { xd = -1; yd = 0; }
  19.                 if (dir == "R") { xd = 1; yd = 0; }
  20.  
  21.                 for( int s=0;s<n;s++ )
  22.                 {
  23.                     if( !stepsone.ContainsKey((x,y)))
  24.                     {
  25.                         stepsone.Add((x,y), totalstep);
  26.                     }
  27.                     wireone.Add( (x,y));
  28.                     x+=xd;
  29.                     y+=yd;
  30.                     totalstep++;
  31.                     wireone.Add((x, y));
  32.                     if (!stepsone.ContainsKey((x, y)))
  33.                     {
  34.                         stepsone.Add((x, y), totalstep);
  35.                     }
  36.                 }
  37.             }
  38.  
  39.  
  40.             x = 0;
  41.             y = 0;
  42.             int answer = int.MaxValue;
  43.             int steptwo = 0;
  44.             foreach (string step in wires[1].Split(','))
  45.             {
  46.                 string dir = step[0].ToString();
  47.                 int n = Convert.ToInt32(step.Substring(1));
  48.  
  49.                 int xd = 0;
  50.                 int yd = 0;
  51.                 if (dir == "U") { xd = 0; yd = -1; }
  52.                 if (dir == "D") { xd = 0; yd = 1; }
  53.                 if (dir == "L") { xd = -1; yd = 0; }
  54.                 if (dir == "R") { xd = 1; yd = 0; }
  55.  
  56.                 for (int s = 0; s < n; s++)
  57.                 {
  58.                     if( wireone.Contains((x,y)) && !(x == 0 && y == 0))
  59.                     {
  60.                         int distance = Math.Abs(x) + Math.Abs(y);
  61.                         if(distance < answer) answer = distance;
  62.  
  63.                         Console.WriteLine(x + "," + y);
  64.                     }
  65.                     x += xd;
  66.                     y += yd;
  67.                     steptwo++;
  68.                     if (wireone.Contains((x, y)) && !(x == 0 && y == 0))
  69.                     {
  70.                         int distance = Math.Abs(x) + Math.Abs(y);
  71.                         if (distance < answer) answer = distance;
  72.  
  73.                         Console.WriteLine(x + "," + y);
  74.                     }
  75.                 }
  76.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement