Advertisement
mastersplinter19

AOC2021_05_2

Dec 5th, 2021
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. List<Direction> directions = new List<Direction>();
  2. List<int> xStarts = new List<int>();
  3. List<int> xFinishes = new List<int>();
  4. List<int> yStarts = new List<int>();
  5. List<int> yFinishes = new List<int>();
  6.  
  7. int[,] grid;
  8. List<Tuple<int, int>> intersections = new List<Tuple<int, int>>();
  9.  
  10. void Main()
  11. {
  12.     var input = File.ReadAllLines(@"C:\Users\dostj\Documents\LINQPad Queries\AdventOfCode\2021\05.txt");
  13.     //input = File.ReadAllLines(@"C:\Users\dostj\Documents\LINQPad Queries\AdventOfCode\2021\05_test.txt");
  14.    
  15.     foreach (var line in input)
  16.     {
  17.         ProcessLine(line);
  18.     }
  19.  
  20.     var xMax = Math.Max(xFinishes.Max(), xStarts.Max()) + 1;
  21.     var yMax = Math.Max(yFinishes.Max(), yStarts.Max()) + 1;
  22.    
  23.     grid = new int[yMax, xMax];
  24.     for (var x = 0; x < xMax; x++)
  25.     {
  26.         for (var y = 0; y < yMax; y++)
  27.         {
  28.             grid[y,x] = 0;
  29.         }
  30.     }
  31.     //grid.Dump();
  32.     var dc = new DumpContainer();
  33.     //dc.Content = grid;
  34.     //dc.Dump();
  35.    
  36.     for (var i = 0; i < directions.Count; i++)
  37.     {
  38.         //dc.UpdateContent(grid);
  39.         //input[i].Dump();
  40.         if (directions[i] == Direction.Diagonal)
  41.         {
  42.             // don't make squares
  43.             // 45 degrees is important because you basically want each point on an y = mx + b, where m is 1
  44.             // So we mainly just need to determine if we have a negative or positive slope and then  we just move from
  45.             // one point to the other by one.
  46.            
  47.             // Oh, yeah. And rise over run would help get the slope.
  48.             var slope = (yStarts[i] - yFinishes[i]) / (xStarts[i] - xFinishes[i]);
  49.             var b = yStarts[i] - slope * xStarts[i];
  50.            
  51.             var lowerX = Math.Min(xStarts[i], xFinishes[i]);
  52.             var upperX = Math.Max(xStarts[i], xFinishes[i]);
  53.            
  54.             for (var x = lowerX; x <= upperX; x++)
  55.             {
  56.                 //x.Dump();
  57.                
  58.                 var y = slope * x + b;
  59.                 //y.Dump();
  60.                 //b.Dump();
  61.                 grid[y, x]++;
  62.                 if (grid[y, x] >= 2)
  63.                 {
  64.                     intersections.Add(new Tuple<int, int>(x, y));
  65.                 }
  66.             }
  67.         }
  68.         else
  69.         {
  70.             var lowerX = Math.Min(xStarts[i], xFinishes[i]);
  71.             var upperX = Math.Max(xStarts[i], xFinishes[i]);
  72.  
  73.             var lowerY = Math.Min(yStarts[i], yFinishes[i]);
  74.             var upperY = Math.Max(yStarts[i], yFinishes[i]);
  75.  
  76.             for (var x = lowerX; x <= upperX; x++)
  77.             {
  78.                 for (var y = lowerY; y <= upperY; y++)
  79.                 {
  80.                     //x.Dump();
  81.                     //y.Dump();
  82.                     grid[y, x]++;
  83.                     if (grid[y, x] >= 2)
  84.                     {
  85.                         intersections.Add(new Tuple<int, int>(x, y));
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91.    
  92.     //grid.Dump();
  93.    
  94.     intersections.Distinct().Count().Dump();
  95.     // 954973 not right
  96. }
  97.  
  98. // Define other methods and classes here
  99. void ProcessLine(string line)
  100. {
  101.     var parts = line.Split(new char[]{ ' ', ',', '-', '>' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
  102.     var x1 = parts[0];
  103.     var y1 = parts[1];
  104.     var x2 = parts[2];
  105.     var y2 = parts[3];
  106.    
  107.     var direction = Direction.Diagonal;
  108.    
  109.     if (x1 == x2)
  110.     {
  111.         direction = Direction.Horizontal;
  112.     }
  113.     if (y1 == y2)
  114.     {
  115.         direction = Direction.Vertical;
  116.     }
  117.    
  118.     directions.Add(direction);
  119.     xStarts.Add(x1);
  120.     xFinishes.Add(x2);
  121.     yStarts.Add(y1);
  122.     yFinishes.Add(y2);
  123. }
  124.  
  125. enum Direction
  126. {
  127.     Horizontal,
  128.     Vertical,
  129.     Diagonal
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement