-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3_Pt1.linq
83 lines (68 loc) · 1.96 KB
/
Day3_Pt1.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void Main()
{
string[] input = File.ReadAllText(@"c:\Users\Kaylyn\Desktop\AOC\day3.txt").Trim().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
string[] test = new string[] { "R75,D30,R83,U83,L12,D49,R71,U7,L72", "U62,R66,U55,R34,D71,R55,D58,R83" };
string[] wire1 = input[0].Split(',');
string[] wire2 = input[1].Split(',');
WirePlotter firstWire = new WirePlotter();
firstWire.CompileCoordinates(wire1);
WirePlotter secondWire = new WirePlotter();
secondWire.CompileCoordinates(wire2);
var intersects = firstWire.AllCoords.Intersect(secondWire.AllCoords);
List<int> manhattanDistances = new List<int>();
foreach (var coord in intersects)
{
manhattanDistances.Add(Math.Abs(coord.X) + Math.Abs(coord.Y));
}
Console.WriteLine(manhattanDistances.Where(x => x != 0).OrderByDescending(m => m).Min(c => c));
}
public class Coordinates : HashSet<(int X, int Y)>
{ }
class WirePlotter
{
public int cachedX { get; set; }
public int cachedY { get; set; }
public Coordinates AllCoords { get; set; }
public WirePlotter()
{
AllCoords = new Coordinates();
(cachedX, cachedY) = (0, 0);
AllCoords.Add((cachedX, cachedY));
}
public void CompileCoordinates(string[] path)
{
foreach (string instruction in path)
{
char director = instruction[0];
int distance = int.Parse(instruction.Remove(0, 1));
FeedCoordinates(director, distance);
}
}
private void FeedCoordinates(char director, int distance)
{
int xDiff = 0;
int yDiff = 0;
switch (director)
{
case 'R':
xDiff = Math.Abs(1);
break;
case 'U':
yDiff = Math.Abs(1);
break;
case 'D':
yDiff = -1;
break;
case 'L':
xDiff = -1;
break;
}
for (int i = 0; i < distance; i++)
{
(int localX, int localY) = (cachedX, cachedY);
(int newX, int newY) = (localX + xDiff, localY + yDiff);
AllCoords.Add((newX, newY));
(cachedX, cachedY) = (newX, newY);
}
}
}