-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.txt
187 lines (166 loc) · 5.55 KB
/
day6.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
// https://adventofcode.com/2018/day/6
static int[] Manhattan(int x, int y, List<KeyValuePair<int, int>> points)
{
int[] dist = new int[points.Count];
for (int i = 0; i < dist.Length; ++i)
{
// Manhattan length
int dx = Math.Abs(x - points[i].Key);
int dy = Math.Abs(y - points[i].Value);
dist[i] = dx + dy;
}
return dist;
}
static int Shortest(int[] list)
{
int best = 987654321;
int index = -1;
bool unique = false;
for (int i = 0; i < list.Length; ++i)
{
if (list[i] < best)
{
index = i;
best = list[i];
unique = true;
}
else if (list[i] == best)
{
unique = false;
}
}
return unique ? index : -1;
}
class Line
{
Dictionary<int, int> Row = new Dictionary<int, int>();
public int Width = 0;
public int Get(int column)
{
return Row.ContainsKey(column) ? Row[column] : -1;
}
public void Set(int column, int value)
{
Row[column] = value;
Width = Math.Max(Width, column + 1);
}
};
static HashSet<int> Edge(Line[] grid)
{
HashSet<int> all = new HashSet<int>();
int width = grid[0].Width;
int height = grid.Length;
Line top = grid[0];
Line bot = grid[height-1];
for (int i = 0; i < width; ++i)
{
int t = top.Get(i); if (t >= 0) all.Add(t);
int b = bot.Get(i); if (b >= 0) all.Add(b);
}
for (int i = 0; i < height; ++i)
{
int left = grid[i].Get(0);
int right = grid[i].Get(width-1);
all.Add(left);
all.Add(right);
}
return all;
}
static Dictionary<int, int> CountAll(Line[] grid)
{
Dictionary<int, int> result = new Dictionary<int, int>();
int width = grid[0].Width;
for (int i = 0; i < grid.Length; ++i)
{
for (int x = 0; x < width; ++x)
{
int ch = grid[i].Get(x);
int old = result.ContainsKey(ch) ? result[ch] : 0;
result[ch] = old + 1;
}
}
return result;
}
static List<KeyValuePair<int, int>> GetPoints(string[] lines)
{
List<KeyValuePair<int, int>> points = new List<KeyValuePair<int, int>>();
foreach (string item in lines)
{
string[] parts = item.Split(',');
int x = int.Parse(parts[0]);
int y = int.Parse(parts[1]);
points.Add(new KeyValuePair<int, int>(x, y));
}
return points;
}
static KeyValuePair<int, int> GetBounds(List<KeyValuePair<int, int>> points)
{
KeyValuePair<int, int> maxim = new KeyValuePair<int, int>(0, 0);
int x = 0;
int y = 0;
foreach (KeyValuePair<int, int> pos in points)
{
x = Math.Max(x, pos.Key);
y = Math.Max(x, pos.Value);
}
return new KeyValuePair<int, int>(x, y);
}
static int Day5a(string[] lines)
{
List<KeyValuePair<int, int>> points = GetPoints(lines);
// GetRectangleBounds()
KeyValuePair<int, int> maxim = GetBounds(points);
int x1 = maxim.Key + 1;
int y1 = maxim.Value + 1;
Line[] grid = new Line[y1];
for (int iy = 0; iy < y1; ++iy)
{
Line line = new Line();
for (int ix = 0; ix < x1; ++ix)
{
int[] dist = Manhattan(ix, iy, points);
int index = Shortest(dist);
if (index >= 0)
{
line.Set(ix, index);
}
else // same distance from multiple points
{
line.Set(ix, -1);
}
}
grid[iy] = line;
}
HashSet<int> infinite = Edge(grid);
Dictionary<int, int> counts = CountAll(grid);
int best = 0;
for (int ch = 0; ch < points.Count; ++ch)
{
if (!infinite.Contains(ch))
{
best = Math.Max(best, counts[ch]);
}
}
return best;
}
static int Day5b(string[] lines)
{
List<KeyValuePair<int, int>> points = GetPoints(lines);
KeyValuePair<int, int> maxim = GetBounds(points);
int x1 = maxim.Key;
int y1 = maxim.Value;
int region = 0;
for (int iy = 0; iy < y1; ++iy)
{
for (int ix = 0; ix < x1; ++ix)
{
int[] dist = Manhattan(ix, iy, points);
int sum = dist.Sum();
if (sum < 10000)
{
region += 1;
}
}
}
return region;
}