-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day5part2.linq
173 lines (146 loc) · 3.01 KB
/
adventOfCode2021day5part2.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 5".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input5.txt"));
}
private void Simulate(List<string> commands)
{
var size = 1000;
if (commands.Count < 11)
size = 10;
var array = new int[size, size];
foreach (var com in commands)
{
//com.Dump();
var command = ParseCommand(com);
var points = GetPointsOnLine(command.from.x, command.from.y, command.to.x, command.to.y);
//points.Dump();
foreach (var point in points)
{
array[point.y, point.x] = array[point.y, point.x] + 1;
}
}
//array.Dump();
CountPointsToAvoid(array,size).Dump();
}
private int CountPointsToAvoid(int[,] array, int size){
int counter=0;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if(array[y,x]>1)
counter++;
}
}
return counter;
}
private ((int x, int y) from, (int x, int y) to) ParseCommand(string command)
{
var comm = command.Replace("->", ",").Replace(" ", "");
var split = comm.Split(",");
return ((int.Parse(split[0]), int.Parse(split[1])), (int.Parse(split[2]), int.Parse(split[3])));
}
private IEnumerable<Point> GetPointsOnLine(int x0, int y0, int x1, int y1)
{
bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (steep)
{
int t;
t = x0; // swap x0 and y0
x0 = y0;
y0 = t;
t = x1; // swap x1 and y1
x1 = y1;
y1 = t;
}
if (x0 > x1)
{
int t;
t = x0; // swap x0 and x1
x0 = x1;
x1 = t;
t = y0; // swap y0 and y1
y0 = y1;
y1 = t;
}
int dx = x1 - x0;
int dy = Math.Abs(y1 - y0);
int error = dx / 2;
int ystep = (y0 < y1) ? 1 : -1;
int y = y0;
for (int x = x0; x <= x1; x++)
{
yield return new Point((steep ? y : x), (steep ? x : y));
error = error - dy;
if (error < 0)
{
y += ystep;
error += dx;
}
}
yield break;
}
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
private int[,] GetArray(List<string> commands)
{
int width = commands.Count;
int height = commands.Count;
var array = new int[height, width];
int x = 0;
int y = 0;
foreach (var command in commands)
{
var str = command.Split(" ");
foreach (var by in str)
{
if (by == "")
continue;
array[y, x] = int.Parse(by);
x++;
}
x = 0;
y++;
}
return array;
}
private List<string> ReadMyFile(string uri)
{
var newList = new List<string>();
string line;
System.IO.StreamReader reader = new StreamReader(uri);
while ((line = reader.ReadLine()) != null)
{
newList.Add(line);
}
return newList;
}
private List<string> GetSampleList()
{
var newList = new List<string>();
newList.Add("0,9 -> 5,9");
newList.Add("8,0 -> 0,8");
newList.Add("9,4 -> 3,4");
newList.Add("2,2 -> 2,1");
newList.Add("7,0 -> 7,4");
newList.Add("6,4 -> 2,0");
newList.Add("0,9 -> 2,9");
newList.Add("3,4 -> 1,4");
newList.Add("0,0 -> 8,8");
newList.Add("5,5 -> 8,2");
return newList;
}
// Define other methods and classes here