-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
77 lines (59 loc) · 1.64 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
70
71
72
73
74
75
76
77
using AdventOfCode._2021_13;
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
var lines = Resources.GetInputFileLines();
var points = new HashSet<Coor>();
var folds = new List<Fold>();
foreach (var line in lines)
{
if (line.StartsWith("fold along"))
{
var pieces = line.Split(' ').Last().Split('=');
switch (pieces.First())
{
case "x":
folds.Add(new XFold(int.Parse(pieces.Last())));
break;
case "y":
folds.Add(new YFold(int.Parse(pieces.Last())));
break;
default:
throw new ArgumentException($"Invalid fold `{line}`");
}
continue;
}
var parts = line.Split(',').Select(int.Parse);
points.Add(new Coor(parts.First(), parts.Last()));
}
static HashSet<Coor> Fold(HashSet<Coor> coors, Fold fold)
{
return coors.Select(coor => fold.Transform(coor)).ToHashSet();
}
static void Display(HashSet<Coor> coors)
{
var last = new Coor(0, 0);
foreach (var point in coors.OrderBy(p => p.Y).ThenBy(p => p.X))
{
for (int i = last.Y; i < point.Y; i++)
{
Console.WriteLine();
last = new Coor(0, point.Y);
}
for (int i = last.X; i < point.X; i++)
{
Console.Write(' ');
}
Console.Write('#');
last = point with
{
X = point.X + 1,
};
}
}
Console.WriteLine($"Part 1: {Fold(points, folds.First()).Count}");
foreach (var fold in folds)
{
points = Fold(points, fold);
}
Console.WriteLine($"Part 2: {points.Count}");
Display(points);