-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.cs
82 lines (77 loc) · 2.38 KB
/
day3.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
// https://adventofcode.com/2018/day/3
// Returns the number of square inches covered by 2 or more blocks
static int Day3a(string[] lines)
{
HashSet<int> first = new HashSet<int>();
HashSet<int> results = new HashSet<int>();
string pattern = @"#(?<id>\d*) \@ (?<x>\d*),(?<y>\d*): (?<w>\d*)x(?<h>\d*)";
Regex rx = new System.Text.RegularExpressions.Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (string text in lines)
{
Match m = rx.Match(text);
if (m.Success)
{
int x = int.Parse(m.Groups["x"].Value);
int y = int.Parse(m.Groups["y"].Value);
int w = int.Parse(m.Groups["w"].Value);
int h = int.Parse(m.Groups["h"].Value);
for (int iy = 0; iy < h; ++iy)
{
for (int ix = 0; ix < w; ++ix)
{
int key = (x + ix) + (y + iy) * 1000;
if (!first.Add(key)) // second or more?
{
results.Add(key);
}
}
}
}
}
return results.Count;
}
struct Rectangle
{
public string id;
public int x0, y0, x1, y1;
public bool Overlaps(Rectangle other)
{
return (x0 <= other.x1) && (y0 <= other.y1) && (x1 >= other.x0) && (y1 >= other.y0);
}
};
// Returns ID of first box that does not overlap any other boxes
static string Day3b(string[] lines)
{
List<Rectangle> boxes = new List<Rectangle>();
HashSet<string> all = new HashSet<string>();
string pattern = @"#(?<id>\d*) \@ (?<x>\d*),(?<y>\d*): (?<w>\d*)x(?<h>\d*)";
Regex rx = new System.Text.RegularExpressions.Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (string text in lines)
{
Match m = rx.Match(text);
if (m.Success)
{
Rectangle box = new Rectangle();
box.id = m.Groups["id"].Value;
box.x0 = int.Parse(m.Groups["x"].Value);
box.y0 = int.Parse(m.Groups["y"].Value);
box.x1 = box.x0 + int.Parse(m.Groups["w"].Value) - 1;
box.y1 = box.y0 + int.Parse(m.Groups["h"].Value) - 1;
all.Add(box.id);
foreach (Rectangle other in boxes)
{
if (box.Overlaps(other))
{
all.Remove(box.id);
all.Remove(other.id);
}
}
boxes.Add(box);
}
}
if (all.Count != 0)
{
return all.First();
}
return null;
}