-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
69 lines (52 loc) · 1.8 KB
/
Program.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
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
var coordinates = Resources.GetInputFileLines()
.Select(line =>
{
var coor = line.SplitBy(" -> ")
.SelectMany(c => c.Split(','))
.Select(int.Parse)
.ToArray();
return (Start: new Coor(coor[0], coor[1]), End: new Coor(coor[2], coor[3]));
});
static long CountOverlaps(IEnumerable<(Coor Start, Coor End)> coordinates, bool skipDiagonal = true)
{
var xCoordinates = coordinates.SelectMany(c => new[] { c.Start.X, c.End.X });
var yCoordinates = coordinates.SelectMany(c => new[] { c.Start.Y, c.End.Y });
var min = new Coor(xCoordinates.Min(), yCoordinates.Min());
var max = new Coor(xCoordinates.Max(), yCoordinates.Max());
var map = new int[max.X - min.X + 1, max.Y - min.Y + 1];
var overlapCount = 0L;
foreach (var coor in coordinates)
{
var step = coor.End - coor.Start;
if (step.X != 0)
{
step = step with { X = step.X / Math.Abs(step.X) };
}
if (step.Y != 0)
{
step = step with { Y = step.Y / Math.Abs(step.Y) };
}
if (skipDiagonal && step.X != 0 && step.Y != 0)
{
// Non-horizontal + non-vertical line
continue;
}
var current = coor.Start - step;
do
{
current += step;
var x = current.X - min.X;
var y = current.Y - min.Y;
map[x, y] += 1;
if (map[x, y] == 2)
{
overlapCount++;
}
} while (current != coor.End);
}
return overlapCount;
}
Console.WriteLine($"Part 1: {CountOverlaps(coordinates, true)} overlaps");
Console.WriteLine($"Part 2: {CountOverlaps(coordinates, false)} overlaps");