-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAy6.cs
173 lines (144 loc) · 5.12 KB
/
DAy6.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace AdventOfCode2018
{
class Day6 : Day
{
int[,] grid;
int xmax;
int ymax;
int[] counts;
public override bool Test()
{
return Utils.Test(Part1, "1, 1\n1, 6\n8, 3\n3, 4\n5, 5\n8, 9", "17" ) &&
Utils.Test(Part2, "1, 1\n1, 6\n8, 3\n3, 4\n5, 5\n8, 9", "16", new { max = 32 });
}
public override dynamic Options => new { max = 10000 };
public override string Part1(string input, dynamic options)
{
var points = Utils.splitLines((string)input).Select(parseLine).ToList();
xmax = points.Max(p => p.X);
ymax = points.Max(p => p.Y);
grid = new int[xmax + 2,ymax + 2];
counts = new int[points.Count];
for (int x = 1; x <= xmax; x++)
{
for (int y = 1; y <= ymax; y++)
{
var newValue = NearestRoot(x, y, points);
grid[x, y] = newValue;
if (newValue > 0)
{
counts[newValue - 1]++;
if (x == 1 || y == 1 || x == xmax || y == ymax)
{
counts[newValue - 1] = int.MinValue;
}
}
}
}
//Print();
return (counts.Max()).ToString();
}
private int NearestRoot(int x, int y, List<Point> points)
{
int minDistance = int.MaxValue;
int closest = 0;
bool multipleClosest = false;
for (int i = 0; i < points.Count; i++)
{
var p = points[i];
var distance = Math.Abs(x - p.X) + Math.Abs(y - p.Y);
if (distance < minDistance)
{
minDistance = distance;
closest = i;
multipleClosest = false;
}
else if (distance == minDistance)
{
multipleClosest = true;
}
}
return multipleClosest ? -1 : closest + 1;
}
private void Print()
{
if (xmax < 20) return;
StringBuilder sb = new StringBuilder();
for (int y = 1; y <= ymax; y++)
{
sb.Append("\n");
for (int x = 1; x <= xmax; x++)
{
if (grid[x, y] < 0)
{
sb.Append(".");
}
else if (grid[x, y] == 0)
{
sb.Append(" ");
}
else
{
sb.Append((char) (grid[x, y] + 32));
}
//Console.Write(" ");
}
}
string temp = System.IO.Path.GetTempFileName().Replace(".tmp", ".txt");
System.IO.File.WriteAllText(temp, sb.ToString());
Process.Start(temp);
}
private int MostCommonNeighbour(int x, int y)
{
var neighbours = new List<int>();
neighbours.Add(grid[x - 1, y]);
neighbours.Add(grid[x + 1, y]);
neighbours.Add(grid[x, y - 1]);
neighbours.Add(grid[x, y + 1]);
var grouped = neighbours.Where(n => n != 0).GroupBy(n => n).OrderByDescending(g => g.Count()).ToList();
if (grouped.Count() == 0)
{
return 0;
}
if (grouped.Count() == 1)
{
return grouped[0].Key;
}
if (grouped[0].Count() != grouped[1].Count())
{
return grouped[0].Key;
}
return -1;
}
public override string Part2(string input, dynamic options)
{
var points = Utils.splitLines((string)input).Select(parseLine).ToList();
xmax = points.Max(p => p.X);
ymax = points.Max(p => p.Y);
var threshold = options.max;
grid = new int[xmax + 2, ymax + 2];
for (int x = 1; x <= xmax; x++)
{
for (int y = 1; y <= ymax; y++)
{
var distance = points.Sum(p => Math.Abs(p.X - x) + Math.Abs(p.Y - y));
grid[x, y] = distance;
}
}
return grid.flatten().Count(x => x > 0 && x < threshold).ToString();
}
private Point parseLine(string line)
{
var parts = line.Split(',').Select(s => int.Parse(s.Trim())).ToList();
return new Point(parts[0], parts[1]);
}
}
}