Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<Direction> directions = new List<Direction>();
- List<int> xStarts = new List<int>();
- List<int> xFinishes = new List<int>();
- List<int> yStarts = new List<int>();
- List<int> yFinishes = new List<int>();
- int[,] grid;
- List<Tuple<int, int>> intersections = new List<Tuple<int, int>>();
- void Main()
- {
- var input = File.ReadAllLines(@"C:\Users\dostj\Documents\LINQPad Queries\AdventOfCode\2021\05.txt");
- //input = File.ReadAllLines(@"C:\Users\dostj\Documents\LINQPad Queries\AdventOfCode\2021\05_test.txt");
- foreach (var line in input)
- {
- ProcessLine(line);
- }
- var xMax = Math.Max(xFinishes.Max(), xStarts.Max()) + 1;
- var yMax = Math.Max(yFinishes.Max(), yStarts.Max()) + 1;
- grid = new int[yMax, xMax];
- for (var x = 0; x < xMax; x++)
- {
- for (var y = 0; y < yMax; y++)
- {
- grid[y,x] = 0;
- }
- }
- //grid.Dump();
- var dc = new DumpContainer();
- //dc.Content = grid;
- //dc.Dump();
- for (var i = 0; i < directions.Count; i++)
- {
- //dc.UpdateContent(grid);
- //input[i].Dump();
- if (directions[i] == Direction.Diagonal)
- {
- // don't make squares
- // 45 degrees is important because you basically want each point on an y = mx + b, where m is 1
- // So we mainly just need to determine if we have a negative or positive slope and then we just move from
- // one point to the other by one.
- // Oh, yeah. And rise over run would help get the slope.
- var slope = (yStarts[i] - yFinishes[i]) / (xStarts[i] - xFinishes[i]);
- var b = yStarts[i] - slope * xStarts[i];
- var lowerX = Math.Min(xStarts[i], xFinishes[i]);
- var upperX = Math.Max(xStarts[i], xFinishes[i]);
- for (var x = lowerX; x <= upperX; x++)
- {
- //x.Dump();
- var y = slope * x + b;
- //y.Dump();
- //b.Dump();
- grid[y, x]++;
- if (grid[y, x] >= 2)
- {
- intersections.Add(new Tuple<int, int>(x, y));
- }
- }
- }
- else
- {
- var lowerX = Math.Min(xStarts[i], xFinishes[i]);
- var upperX = Math.Max(xStarts[i], xFinishes[i]);
- var lowerY = Math.Min(yStarts[i], yFinishes[i]);
- var upperY = Math.Max(yStarts[i], yFinishes[i]);
- for (var x = lowerX; x <= upperX; x++)
- {
- for (var y = lowerY; y <= upperY; y++)
- {
- //x.Dump();
- //y.Dump();
- grid[y, x]++;
- if (grid[y, x] >= 2)
- {
- intersections.Add(new Tuple<int, int>(x, y));
- }
- }
- }
- }
- }
- //grid.Dump();
- intersections.Distinct().Count().Dump();
- // 954973 not right
- }
- // Define other methods and classes here
- void ProcessLine(string line)
- {
- var parts = line.Split(new char[]{ ' ', ',', '-', '>' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
- var x1 = parts[0];
- var y1 = parts[1];
- var x2 = parts[2];
- var y2 = parts[3];
- var direction = Direction.Diagonal;
- if (x1 == x2)
- {
- direction = Direction.Horizontal;
- }
- if (y1 == y2)
- {
- direction = Direction.Vertical;
- }
- directions.Add(direction);
- xStarts.Add(x1);
- xFinishes.Add(x2);
- yStarts.Add(y1);
- yFinishes.Add(y2);
- }
- enum Direction
- {
- Horizontal,
- Vertical,
- Diagonal
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement