-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 14.txt
204 lines (189 loc) · 6 KB
/
Day 14.txt
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
internal class Program
{
static void Main(string[] args)
{
string[] input = File.ReadAllLines(@"C:\Users\pette\Documents\Aoc14.txt");
List<Point> pointList = new List<Point>();
foreach (string s1 in input)
{
string[] temp1 = s1.Split(" -> ");
int preX = 0;
int preY = 0;
foreach (string s2 in temp1)
{
string[] temp2 = s2.Split(",");
int curX = Convert.ToInt32(temp2[0]);
int curY = Convert.ToInt32(temp2[1]);
if (preX != 0 && preX != curX)
{
if (preX < curX)
{
for (int i = 1; i < curX - preX; i++)
{
pointList.Add(new Point('#', preX + i, curY));
}
}
else if (preX > curX)
{
for (int i = 1; i < preX - curX; i++)
{
pointList.Add(new Point('#', preX - i, curY));
}
}
}
else if (preY != 0 && preY != curY)
{
if (preY < curY)
{
for (int i = 1; i < curY - preY; i++)
{
pointList.Add(new Point('#', curX, preY + i));
}
}
else if (preY > curY)
{
for (int i = 1; i < preY - curY; i++)
{
pointList.Add(new Point('#', curX, preY - i));
}
}
}
pointList.Add(new Point('#', curX, curY));
preX = curX;
preY = curY;
}
}
int highX = 0;
int lowX = 500;
int highY = 0;
int lowY = 100;
foreach (Point p in pointList)
{
if (p.x > highX)
{
highX = p.x;
}
if (p.x < lowX)
{
lowX = p.x;
}
if (p.y > highY)
{
highY = p.y;
}
if (p.y < lowY)
{
lowY = p.y;
}
}
int width = highX - lowX + 401;
int height = highY - lowY + 16;
// Creates map (2-d array).
Point[,] map = new Point[height, width];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
map[i, j] = new Point('.', j, i);
}
Console.WriteLine();
}
// Adds all rock.
foreach (Point p in pointList)
{
map[p.y - lowY + 13, p.x - lowX + 200].materia = '#';
}
// Add bottom rocks.
for (int i = 0; i < map.GetLength(1); i++)
{
map[map.GetLength(0) - 1, i].materia = '#';
}
//// Prints overview.
//for (int i = 0; i < map.GetLength(0); i++)
//{
// for (int j = 0; j < map.GetLength(1); j++)
// {
// Console.Write(map[i, j].materia);
// }
// Console.WriteLine();
//}
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine();
int sandStart = 500 - lowX + 200;
int x = sandStart;
int y = 0;
do
//for (int i = 0; i < 20000000; i++)
{
if (map[y, x].materia == '.')
{
if (map[y + 1, x].materia != '.')
{
if (map[y + 1, x - 1].materia != '.')
{
if (map[y + 1, x + 1].materia != '.')
{
map[y, x].materia = 'o';
y = 0;
x = sandStart;
}
else
{
y++;
x++;
}
}
else
{
y++;
x--;
}
}
else
{
y++;
}
}
else if (map[y, x].materia == 'o')
{
break;
}
} while (y < height - 1 && x != 0);
// Prints overview.
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Console.Write(map[i, j].materia);
}
Console.WriteLine();
}
int sum = 0;
foreach (Point p in map)
{
if (p.materia == 'o')
{
sum++;
}
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
public class Point
{
public char materia;
public int x;
public int y;
public Point(char m, int x, int y)
{
materia = m;
this.x = x;
this.y = y;
}
public Point()
{
materia = '.';
}