-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.cs
More file actions
220 lines (158 loc) · 5.84 KB
/
day24.cs
File metadata and controls
220 lines (158 loc) · 5.84 KB
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;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Day24Class
{
public void Start()
{
// https://adventofcode.com/2020/day/24
Console.WriteLine("****** DIA 24 ******");
Console.WriteLine("****** FASE 1 ******");
const decimal R = 1M;
decimal x = 1.5M * R * (decimal)Math.Cos(Math.PI / 3);
decimal y = 1.5M * R * (decimal)Math.Sin(Math.PI / 3);
//https://catlikecoding.com/unity/tutorials/hex-map/part-1/
Dictionary<string, (decimal x, decimal y)> deltaPositions = new Dictionary<string, (decimal x, decimal y)>()
{
{"e", (1.5M * R, 0)},
{"w", (-1.5M * R, 0)},
{"ne", (x, -y)},
{"nw", (-x, -y)},
{"se", (x, y)},
{"sw", (-x, y)},
};
string[] inputs = File.ReadAllLines("./inputs/inputs_dia24.txt");
Dictionary<(decimal x, decimal y), bool> tiles = new Dictionary<(decimal x, decimal y), bool>();
List<string[]> instrucciones = GenerarInstrucciones(inputs);
for (int i = 0; i < instrucciones.Count; i++)
{
(decimal x, decimal y) currentPos = (0, 0);
for (int j = 0; j < instrucciones[i].Length; j++)
{
var deltaCurrentPos = deltaPositions[instrucciones[i][j]];
currentPos.x += deltaCurrentPos.x;
currentPos.y += deltaCurrentPos.y;
}
if (tiles.ContainsKey(currentPos) == true)
{
// true <-> false
tiles[currentPos] = !tiles[currentPos];
}
else
{
tiles[currentPos] = false;
}
}
ulong contador = 0;
foreach(var valor in tiles)
{
if (valor.Value == false)
{
contador++;
}
}
Console.WriteLine("Resultado=" + contador);
Console.WriteLine("****** FASE 2 ******");
tiles.Clear();
instrucciones = GenerarInstrucciones(inputs);
//format tiles
for (int i = 0; i < instrucciones.Count; i++)
{
(decimal x, decimal y) currentPos = (0, 0);
for (int j = 0; j < instrucciones[i].Length; j++)
{
var deltaCurrentPos = deltaPositions[instrucciones[i][j]];
currentPos.x += deltaCurrentPos.x;
currentPos.y += deltaCurrentPos.y;
}
if (tiles.ContainsKey(currentPos) == true)
{
// true <-> false
tiles[currentPos] = !tiles[currentPos];
}
else
{
tiles[currentPos] = false;
}
}
for (ushort i = 0; i < 100; i++)
{
var formatedTile = new Dictionary<(decimal x, decimal y), (bool coloreado, int numVecinosColoreados)>();
foreach (var (posicionTile, tileColoreado) in tiles)
{
foreach (var (deltaPosicionNombre, (deltaPosicionX, deltaPosicionY)) in deltaPositions)
{
var currentPositionTile = posicionTile;
currentPositionTile.x += deltaPosicionX;
currentPositionTile.y += deltaPosicionY;
//añadir
if (formatedTile.ContainsKey(currentPositionTile) == false)
{
bool isColoreado = true;
if (tiles.ContainsKey(currentPositionTile) == true)
{
isColoreado = tiles[currentPositionTile];
}
formatedTile[currentPositionTile] = (isColoreado, 0);
}
(bool coloreado, int numeroVecinosColoreados) valorTile = formatedTile[currentPositionTile];
if (tileColoreado == false)
{
valorTile.numeroVecinosColoreados++;
}
formatedTile[currentPositionTile] = valorTile;
}
}
Dictionary<(decimal x, decimal y), bool> nuevoTile = new Dictionary<(decimal x, decimal y), bool>();
foreach (var (positionTile, (isColoreado, numVecinosColoreados)) in formatedTile)
{
if (isColoreado == false )
{
if (numVecinosColoreados == 0 || numVecinosColoreados > 2)
{
}
else
{
nuevoTile.Add(positionTile, false);
}
}
else
{
if (numVecinosColoreados == 2)
{
nuevoTile.Add(positionTile, false);
}
}
}
tiles = nuevoTile;
}
contador = 0;
foreach (var valor in tiles)
{
if (valor.Value == false)
{
contador++;
}
}
Console.WriteLine("Resultado=" + contador);
}
private List<string[]> GenerarInstrucciones(string[] inputs)
{
List<string[]> instrucciones = new List<string[]>();
Regex regex = new Regex(@"e|se|sw|w|nw|ne");
//format instrucciones
for (ushort i = 0; i < inputs.Length; i++)
{
MatchCollection matches = regex.Matches(inputs[i]);
List<string> templist = new List<string>();
for (int j = 0; j < matches.Count; j++)
{
templist.Add(matches[j].Value);
}
instrucciones.Add(templist.ToArray());
}
return instrucciones;
}
}