-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.cs
143 lines (127 loc) · 4.41 KB
/
day10.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
// https://adventofcode.com/2018/day/10
class Node10
{
public int PositionX;
public int PositionY;
public int VelocityX;
public int VelocityY;
public void Parse(string line)
{
string[] parts = line.Split('<', '>', ',');
PositionX = int.Parse(parts[1]);
PositionY = int.Parse(parts[2]);
VelocityX = int.Parse(parts[4]);
VelocityY = int.Parse(parts[5]);
}
public KeyValuePair<int, int> Get(int time)
{
int x = PositionX + VelocityX * time;
int y = PositionY + VelocityY * time;
return new KeyValuePair<int, int>(x,y);
}
};
class View10
{
int X0 = 0;
int Y0 = 0;
int X1 = 0;
int Y1 = 0;
public int Width { get { return X1 - X0; } }
public int Height { get { return Y1 - Y0; } }
List<string> screen = new List<string>();
public void Reset()
{
X0 = int.MaxValue;
Y0 = int.MaxValue;
X1 = int.MinValue;
Y1 = int.MinValue;
all.Clear();
screen.Clear();
}
List<KeyValuePair<int, int>> all = new List<KeyValuePair<int, int>>();
public void Insert(KeyValuePair<int, int> p)
{
int x = p.Key;
int y = p.Value;
X0 = Math.Min(x, X0);
Y0 = Math.Min(y, Y0);
X1 = Math.Max(x, X1);
Y1 = Math.Max(y, Y1);
all.Add(p);
}
void Resolve()
{
foreach (KeyValuePair<int, int> it in all)
{
int x = it.Key-X0;
int y = it.Value-Y0;
Plot(x, y);
}
}
void Plot(int x, int y)
{
while (y >= screen.Count)
{
screen.Add("");
}
string line = screen[y];
while (x >= line.Length)
{
line += ".";
}
screen[y] = line.Remove(x, 1).Insert(x,"#");
}
public void Show()
{
screen.Clear();
Resolve();
for (int i = 0; i < screen.Count; ++i)
{
System.Console.WriteLine(screen[i]);
}
}
}
static int Day10(string[] lines)
{
int total = 0;
List<Node10> nodes = new List<Node10>();
foreach (string it in lines)
{
Node10 node = new Node10();
node.Parse(it);
nodes.Add(node);
// ASSUME: the message appears near zero (based on data)
// Estimate time it takes for signals to reach the origin (0,0)
int estX = (node.VelocityX != 0) ? -node.PositionX / node.VelocityX : 0;
int estY = (node.VelocityY != 0) ? -node.PositionY / node.VelocityY : 0;
total += (estX + estY)/ 2;
}
// Start before estimate and display any results that seem promising
int avgTime = total / nodes.Count;
int baseTime = avgTime - 10;
int bestTime = 0;
int bestHeight = 100;
for (int t = 0; t < 20; ++t)
{
int time = baseTime + t;
View10 screen = new View10();
screen.Reset();
foreach (Node10 n in nodes)
{
KeyValuePair<int, int> p = n.Get(time);
screen.Insert(p);
}
System.Console.WriteLine("Time={0} width={1} height={2}", time, screen.Width, screen.Height);
// ASSUME: lettering has a small height based on example text
if (screen.Width < 250 && screen.Height < 16)
{
screen.Show();
}
if (screen.Height < bestHeight)
{
bestTime = time;
bestHeight = screen.Height;
}
}
return bestTime;
}