52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
// #define Day13
|
|
#if Day13
|
|
using System.Text.RegularExpressions;
|
|
|
|
var text = File.ReadAllText("day13/input").Split("\n\n");
|
|
var points = text[0].Split("\n").Select(l => l.Split(","))
|
|
.Select(p => new Point(int.Parse(p[0]), int.Parse(p[1]))).ToHashSet();
|
|
|
|
var reg = new Regex(@"fold along (?<c>x|y)=(?<p>\d+)");
|
|
var folds = reg.Matches(text[1]).Select(m => new Fold(m.Groups["c"].Value[0], int.Parse(m.Groups["p"].Value))).ToList();
|
|
|
|
foreach (var curFold in folds) {
|
|
HashSet<Point> next = new HashSet<Point>();
|
|
if (curFold.c == 'x') {
|
|
foreach(var p in points) {
|
|
if (p.x < curFold.p) {
|
|
next.Add(p);
|
|
} else if (p.x > curFold.p) {
|
|
next.Add(new Point(curFold.p - (p.x - curFold.p), p.y));
|
|
}
|
|
}
|
|
} else {
|
|
foreach(var p in points) {
|
|
if (p.y < curFold.p) {
|
|
next.Add(p);
|
|
} else if (p.y > curFold.p){
|
|
next.Add(new Point(p.x, curFold.p - (p.y - curFold.p)));
|
|
}
|
|
}
|
|
}
|
|
points = next;
|
|
Console.WriteLine(points.Count);
|
|
}
|
|
|
|
var maxX = points.Max(p => p.x);
|
|
var maxY = points.Max(p => p.y);
|
|
|
|
for (int y = 0; y <= maxY; y++) {
|
|
for (int x = 0; x <= maxX; x++) {
|
|
if (points.Contains(new Point(x, y))) {
|
|
Console.Write("#");
|
|
} else {
|
|
Console.Write(".");
|
|
}
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
|
|
record Point(int x, int y);
|
|
record Fold(char c, int p);
|
|
#endif |