-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day20.cs
155 lines (129 loc) · 4.08 KB
/
Day20.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
namespace AdventOfCode2021;
public static class Day20
{
public static void Part1()
{
string[] inputs = InputHelper.GetInput(20);
string algorithmString = inputs[0];
const int enhancementSteps = 2;
char[,] image = ReadImage(inputs, enhancementSteps);
for (int i = 0; i < enhancementSteps; i++)
{
EnhanceImage(image, algorithmString);
}
int litPixelCount = CountLitPixels(image, enhancementSteps);
Console.WriteLine(litPixelCount);
}
private static void PrintImage(char[,] image)
{
int height = image.GetLength(0);
int width = image.GetLength(1);
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
Console.Write(image[x, y]);
}
Console.WriteLine();
}
Console.WriteLine();
}
private static char[,] ReadImage(string[] inputs, int marginSize)
{
int height = inputs.Length - 2;
int width = inputs[2].Length;
marginSize = marginSize * 2 + 1;
int margins = 4 * marginSize;
char[,] image = new char[width + margins, height + margins];
for (int x = 0; x < image.GetLength(0); x++)
{
for (int y = 0; y < image.GetLength(1); y++)
{
image[x, y] = '.';
}
}
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
image[x + marginSize, y + marginSize] = inputs[x + 2][y];
}
}
return image;
}
private static void EnhanceImage(char[,] image, string algorithmString)
{
int height = image.GetLength(0);
int width = image.GetLength(1);
char[,] outputImage = new char[height, width];
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
string binaryString = GetBinaryStringFromPixel(x, y, image);
int outputIndex = Convert.ToInt32(binaryString, fromBase: 2);
char pixel = algorithmString[outputIndex];
outputImage[x, y] = pixel;
}
}
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
image[x, y] = outputImage[x, y];
}
}
}
private static string GetBinaryStringFromPixel(int x, int y, char[,] image)
{
char[] binaryString = new char[9];
int i = 0;
for (int dx = -1; dx <= +1; dx++)
{
for (int dy = -1; dy <= +1; dy++)
{
if (x + dx < 0 || x + dx >= image.GetLength(0)
|| y + dy < 0 || y + dy >= image.GetLength(1))
{
binaryString[i] = '0';
}
else
{
binaryString[i] = image[x + dx, y + dy] == '.' ? '0' : '1';
}
i++;
}
}
return new string(binaryString);
}
private static int CountLitPixels(char[,] twoEnhanceStepImage, int marginSize)
{
int count = 0;
int height = twoEnhanceStepImage.GetLength(0);
int width = twoEnhanceStepImage.GetLength(1);
for (int x = marginSize; x < height - marginSize; x++)
{
for (int y = marginSize; y < width - marginSize; y++)
{
if (twoEnhanceStepImage[x, y] == '#')
{
count++;
}
}
}
return count;
}
public static void Part2()
{
string[] inputs = InputHelper.GetInput(20);
string algorithmString = inputs[0];
const int enhancementSteps = 50;
char[,] image = ReadImage(inputs, enhancementSteps);
for (int i = 0; i < enhancementSteps; i++)
{
EnhanceImage(image, algorithmString);
}
int litPixelCount = CountLitPixels(image, enhancementSteps);
Console.WriteLine(litPixelCount);
}
}