-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD04.cs
171 lines (149 loc) · 4.69 KB
/
D04.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOC2024;
/// <summary>
/// Day 4: Ceres Search
/// </summary>
public class D04
{
private readonly AOCHttpClient _client = new AOCHttpClient(4);
public void Part1()
{
string input = _client.RetrieveFile();
// input = @"MMMSXXMASM
// MSAMXMSMSA
// AMXSXMAAMM
// MSAMASMSMX
// XMASAMXAMM
// XXAMMXXAMA
// SMSMSASXSS
// SAXAMASAAA
// MAMMMXMMMM
// MXMXAXMASX";
char[,] array = input.ConvertToCharArray();
long sum = 0;
for (int y = 0; y < array.GetLength(1); y++)
{
for (int x = 0; x < array.GetLength(0); x++)
{
if (array[x, y] == 'X')
{
var neighbors = GetNeighborIndices(array, x, y);
foreach (List<(int X, int Y)> n in neighbors)
{
char M = array[n[0].X, n[0].Y];
char A = array[n[1].X, n[1].Y];
char S = array[n[2].X, n[2].Y];
if (M == 'M' && A == 'A' && S == 'S')
{
sum++;
}
}
}
}
}
Console.WriteLine(sum);
}
private readonly List<(int X, int Y)> _directions = new List<(int X, int Y)>() {
(-1, -1), ( 0, -1), ( 1, -1),
(-1, 0), ( 1, 0),
(-1, 1), ( 0, 1), ( 1, 1),
};
private List<List<(int, int)>> GetNeighborIndices(char[,] array, int x, int y)
{
var result = new List<List<(int, int)>>();
foreach ((int X, int Y) dir in _directions)
{
var temp = new List<(int, int)>();
for (int i = 1; i < 4; i++)
{
int newX = x + dir.X * i;
int newY = y + dir.Y * i;
if (array.IsWithinBounds(newX, newY))
{
temp.Add((newX, newY));
}
}
if (temp.Count == 3) // Only detect word length of 3, e.g. -"MAS"
{
result.Add(temp);
}
}
return result;
}
public void Part2()
{
string input = _client.RetrieveFile();
// input = @"MMMSXXMASM
// MSAMXMSMSA
// AMXSXMAAMM
// MSAMASMSMX
// XMASAMXAMM
// XXAMMXXAMA
// SMSMSASXSS
// SAXAMASAAA
// MAMMMXMMMM
// MXMXAXMASX";
char[,] array = input.ConvertToCharArray();
long sum = 0;
for (int y = 0; y < array.GetLength(1); y++)
{
for (int x = 0; x < array.GetLength(0); x++)
{
if (array[x, y] == 'A')
{
List<(int X, int Y)> corners = GetCornerIndices(array, x, y);
if (corners.Count != 4)
{
continue;
}
char topLeft = array[corners[0].X, corners[0].Y];
char topRight = array[corners[1].X, corners[1].Y];
char bottomLeft = array[corners[2].X, corners[2].Y];
char bottomRight = array[corners[3].X, corners[3].Y];
// Beautiful :') <3
if (topLeft == 'M' && topRight == 'M' &&
bottomLeft == 'S' && bottomRight == 'S')
{
sum++;
}
else if (topLeft == 'S' && topRight == 'M' &&
bottomLeft == 'S' && bottomRight == 'M')
{
sum++;
}
else if (topLeft == 'S' && topRight == 'S' &&
bottomLeft == 'M' && bottomRight == 'M')
{
sum++;
}
else if (topLeft == 'M' && topRight == 'S' &&
bottomLeft == 'M' && bottomRight == 'S')
{
sum++;
}
}
}
}
Console.WriteLine(sum);
}
private readonly List<(int X, int Y)> _corners = new List<(int X, int Y)>() {
(-1, -1), ( 1, -1),
(-1, 1), ( 1, 1),
};
private List<(int, int)> GetCornerIndices(char[,] array, int x, int y)
{
var result = new List<(int, int)>();
foreach ((int X, int Y) c in _corners)
{
int newX = x + c.X;
int newY = y + c.Y;
if (array.IsWithinBounds(newX, newY))
{
result.Add((newX, newY));
}
}
return result;
}
}