-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.cs
220 lines (187 loc) · 6.15 KB
/
Day3.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System.ComponentModel;
using System.Text;
namespace AOC23
{
public class Part(int num, int[] pos, char symbol)
{
public readonly int num = num;
public readonly int[] position = pos;
public readonly char symbol = symbol;
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("Part ");
sb.Append(this.num);
sb.Append(" at [");
sb.Append(this.position[0]);
sb.Append(", ");
sb.Append(this.position[1]);
sb.Append("] - symbol \"");
sb.Append(this.symbol);
sb.Append("\"");
return sb.ToString();
}
public bool LocEquals(Part other)
{
return this.position[0] == other.position[0] && this.position[1] == other.position[1];
}
}
public class Day3(bool test) : Day(3, test)
{
public bool IsNum(char c)
{
return c >= '0' & c <= '9';
}
public bool IsPart(List<string> input, int[] pos)
{
List<int> x_offs = [0], y_offs = [0];
char current = input[pos[1]][pos[0]];
if (IsNum(current))
{
if (pos[0] > 0)
{
x_offs.Add(-1);
}
if (pos[0] < input[0].Trim().Length - 1)
{
x_offs.Add(1);
}
if (pos[1] > 0)
{
y_offs.Add(-1);
}
if (pos[1] < input.Count - 1)
{
y_offs.Add(1);
}
foreach(var x_os in x_offs)
{
foreach(var y_os in y_offs)
{
if (x_os == 0 & y_os == 0)
{ continue; }
var c = input[pos[1] + y_os][pos[0] + x_os];
if (!IsNum(c))
{
if (c != '.')
{
return true;
}
}
}
}
}
return false;
}
public string ExtractPN(List<string> input, int[] pos)
{
int start = pos[0];
int end = pos[0] + 1;
int len = input[0].Trim().Length - 1;
while(end <= len && IsNum(input[pos[1]][end]))
{
end++;
}
while(start > 0 && IsNum(input[pos[1]][start - 1]))
{
start--;
}
return input[pos[1]].Substring(start, end - start);
}
public int[] GetSymbolLoc(List<string> input, int[] pos)
{
int[] offs = [-1, 0, 1];
foreach (var x_off in offs)
{
if((pos[0] + x_off > input[0].Trim().Length - 1) || (pos[0] + x_off < 0))
{
continue;
}
foreach(var y_off in offs)
{
if ((pos[1] + y_off >= input.Count) || (pos[1] + y_off < 0))
{
continue;
}
if (x_off == 0 && y_off == 0)
{
continue;
}
char curr = input[pos[1] + y_off][pos[0] + x_off];
if (!IsNum(curr) && curr != '.')
{
return [pos[0] + x_off, pos[1] + y_off];
}
}
}
return [-1, -1];
}
public override void Part1()
{
Console.Write("\tPart 1: ");
var lines = this.Load();
int len = lines[0].Trim().Length;
List<Part> parts = [];
for (int j = 0; j < lines.Count; j++)
{
for (int i = 0; i < len; i++)
{
if (IsPart(lines, [i, j]))
{
var pn = ExtractPN(lines, [i, j]);
var loc = GetSymbolLoc(lines, [i, j]);
parts.Add(new Part(int.Parse(pn), loc, lines[loc[1]][loc[0]]));
//Console.WriteLine(parts.Last());
while (i + 1 < len && IsNum(lines[j][i + 1]))
{
i++;
}
}
}
}
int psum = 0;
foreach (var part in parts)
{
psum += part.num;
}
Console.WriteLine(psum);
}
public override void Part2()
{
Console.Write("\tPart 2: ");
var lines = this.Load();
int len = lines[0].Trim().Length;
List<Part> parts = [];
for (int j = 0; j < lines.Count; j++)
{
for (int i = 0; i < len; i++)
{
if (IsPart(lines, [i, j]))
{
var pn = ExtractPN(lines, [i, j]);
var loc = GetSymbolLoc(lines, [i, j]);
parts.Add(new Part(int.Parse(pn), loc, lines[loc[1]][loc[0]]));
//Console.WriteLine(parts.Last());
while (i + 1 < len && IsNum(lines[j][i + 1]))
{
i++;
}
}
}
}
int gear_ratio = 0;
for (int i = 0; i < parts.Count; i++)
{
for (int j = i; j < parts.Count; j++)
{
if (i == j) { continue; }
if (parts[i].symbol == '*' && parts[i].LocEquals(parts[j]))
{
gear_ratio += (parts[i].num * parts[j].num);
}
}
}
Console.WriteLine(gear_ratio);
}
}
}