63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
// #define Day5
|
|
#define PartTwo
|
|
#if Day5
|
|
using System.Text.RegularExpressions;
|
|
Regex regex = new Regex(@"^(?<x1>\d+),(?<y1>\d+) -> (?<x2>\d+),(?<y2>\d+)$");
|
|
|
|
var lines = File.ReadAllLines("day5/input")
|
|
.Select(l => {
|
|
var match = regex.Match(l);
|
|
return new Line(
|
|
new Point(
|
|
int.Parse(match.Groups["x1"].Value),
|
|
int.Parse(match.Groups["y1"].Value)),
|
|
new Point(
|
|
int.Parse(match.Groups["x2"].Value),
|
|
int.Parse(match.Groups["y2"].Value))
|
|
);
|
|
}).ToList();
|
|
|
|
var vents = new Dictionary<Point, int>();
|
|
foreach (var line in lines) {
|
|
IEnumerable<Point> points;
|
|
if (line.start.x == line.end.x) {
|
|
points = Enumerable.Range(Math.Min(line.start.y, line.end.y), Math.Abs(line.start.y - line.end.y) + 1)
|
|
.Select(y => new Point(line.start.x, y));
|
|
} else if (line.start.y == line.end.y) {
|
|
points = Enumerable.Range(Math.Min(line.start.x, line.end.x), Math.Abs(line.start.x - line.end.x) + 1)
|
|
.Select(x => new Point(x, line.start.y));
|
|
} else {
|
|
#if PartTwo
|
|
Point smallerX;
|
|
Point largerX;
|
|
bool increasingY;
|
|
if (line.start.x < line.end.x) {
|
|
smallerX = line.start;
|
|
largerX = line.end;
|
|
} else {
|
|
smallerX = line.end;
|
|
largerX = line.start;
|
|
}
|
|
increasingY = smallerX.y < largerX.y;
|
|
points = Enumerable.Range(0, largerX.x - smallerX.x + 1)
|
|
.Select(i => new Point(smallerX.x + i, smallerX.y + (increasingY ? i : (i*(-1)))));
|
|
#else
|
|
points = new List<Point>();
|
|
#endif
|
|
}
|
|
|
|
foreach (var p in points) {
|
|
if (vents.ContainsKey(p)) {
|
|
vents[p] += 1;
|
|
} else {
|
|
vents[p] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(vents.Count(v => v.Value > 1));
|
|
|
|
|
|
record Line(Point start, Point end);
|
|
record Point(int x, int y);
|
|
#endif |