-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.cs
124 lines (105 loc) · 3.25 KB
/
Day13.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
namespace AdventOfCode2021;
public static class Day13
{
public static void Part1()
{
string[] inputs = InputHelper.GetInput(13);
HashSet<Point> points = new HashSet<Point>();
List<Point> foldInstructions = new List<Point>();
ParseInput(inputs, points, foldInstructions);
Point foldPoint = foldInstructions[0];
FoldPointsAt(points, foldPoint);
Console.WriteLine(points.Count);
}
private static void FoldPointsAt(HashSet<Point> points, Point foldPoint)
{
Point[] movedPoints = points
.Where(p => p.X >= foldPoint.X && p.Y >= foldPoint.Y)
.ToArray();
foreach (Point point in movedPoints)
{
points.Remove(point);
points.Add(new Point(Math.Abs(2 * foldPoint.X - point.X), Math.Abs(2 * foldPoint.Y - point.Y)));
}
}
private static void ParseInput(string[] inputs, HashSet<Point> points, List<Point> foldInstructions)
{
for (int i = 0; i < inputs.Length; i++)
{
string input = inputs[i];
int delimIndex = input.IndexOf(',');
if (delimIndex == -1)
{
ParseFoldInstruction(foldInstructions, input);
}
else
{
Point p = ParsePoint(delimIndex, input);
points.Add(p);
}
}
}
private static void ParseFoldInstruction(List<Point> foldInstructions, string input)
{
int delimIndex = input.LastIndexOf('=');
if (delimIndex == -1)
{
return;
}
if (input[delimIndex - 1] == 'x')
{
foldInstructions.Add(new Point(int.Parse(input[(delimIndex + 1)..]), 0));
}
else
{
foldInstructions.Add(new Point(0, int.Parse(input[(delimIndex + 1)..])));
}
}
private static Point ParsePoint(int delimIndex, string pointAsString)
{
return new Point(int.Parse(pointAsString[..delimIndex]),
int.Parse(pointAsString[(delimIndex + 1)..]));
}
public static void Part2()
{
string[] inputs = InputHelper.GetInput(13);
HashSet<Point> points = new HashSet<Point>();
List<Point> foldInstructions = new List<Point>();
ParseInput(inputs, points, foldInstructions);
for (int i = 0; i < foldInstructions.Count; i++)
{
Point foldPoint = foldInstructions[i];
FoldPointsAt(points, foldPoint);
}
PrintPoints(points);
}
private static void PrintPoints(HashSet<Point> points)
{
int maxX = 0;
int maxY = 0;
foreach (var point in points)
{
if (point.X > maxX)
{
maxX = point.X;
}
if (point.Y > maxY)
{
maxY = point.Y;
}
}
bool[,] pointMap = new bool[maxX + 1, maxY + 1];
foreach (var point in points)
{
pointMap[point.X, point.Y] = true;
}
for (int y = 0; y <= maxY; y++)
{
for (int x = 0; x <= maxX; x++)
{
Console.Write(pointMap[x, y] ? '#' : '.');
}
Console.WriteLine();
}
}
}