-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.cs
More file actions
168 lines (130 loc) · 3.91 KB
/
day23.cs
File metadata and controls
168 lines (130 loc) · 3.91 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
class Day23Class
{
public class Nodo
{
public int value;
public Nodo Next;
public Nodo(int value)
{
this.value = value;
}
}
public void Start()
{
// https://adventofcode.com/2020/day/23
Console.WriteLine("****** DIA 23 ******");
Console.WriteLine("****** FASE 1 ******");
string lines = "198753462";
string resultado = Jugar(lines, 0, 100, false);
Console.WriteLine("El resultado es:" + resultado);
Console.WriteLine("****** FASE 2 ******");
resultado = Jugar(lines, 1_000_000, 10_000_000, true);
Console.WriteLine("El resultado es=" + resultado);
}
private string Jugar(string cadena, int max, int rondas, bool isParte2)
{
List<int> inputs = new List<int>();
for (ushort i = 0; i < cadena.Length; i++)
{
inputs.Add(int.Parse(cadena[i].ToString()));
}
int valorMaximo = inputs.Max();
if (max > 0)
{
for (int j = valorMaximo + 1; j <= max ; j++)
{
inputs.Add(j);
}
}
int indice = max + 1;
if (max == 0)
{
indice = 10;
}
Nodo[] nodos = new Nodo[indice];
Nodo nodoStart = new Nodo(inputs[0]);
nodos[inputs[0]] = nodoStart;
Nodo nodoAnterior = nodoStart;
for (int i = 1; i < inputs.Count; i++)
{
Nodo nodoActual = new Nodo(inputs[i]);
nodos[inputs[i]] = nodoActual;
nodoAnterior.Next = nodoActual;
nodoAnterior = nodoActual;
if (inputs[i] > valorMaximo)
{
valorMaximo = inputs[i];
}
}
nodoAnterior.Next = nodoStart;
Nodo nodoCurrent = nodoStart;
for (int j = 0; j < rondas; j++)
{
Nodo nodoSiguiente = nodoCurrent.Next;
nodoCurrent.Next = nodoSiguiente.Next.Next.Next;
int destVal = FindPositionDestination(nodoCurrent.value, nodoSiguiente, valorMaximo);
Nodo ip = nodos[destVal];
Nodo ipn = ip.Next;
Nodo tail = nodoSiguiente.Next.Next;
tail.Next = ipn;
ip.Next = nodoSiguiente;
nodoCurrent = nodoCurrent.Next;
}
string resultado = "";
if (isParte2 == false)
{
resultado = nodosToString(nodoStart, 1);
}
else
{
Nodo nodoActual = nodos[1];
ulong res = (ulong)nodoActual.Next.value * (ulong)nodoActual.Next.Next.value;
resultado = res.ToString();
}
return resultado;
}
private int FindPositionDestination(int valorInicio, Nodo nodoCurrent, int valormasalto)
{
int destino = 0;
if (valorInicio == 1)
{
destino = valormasalto;
}
else
{
destino = valorInicio - 1;
}
int valorCurrentNodo = nodoCurrent.value;
int valorNextNodo = nodoCurrent.Next.value;
int valorNodoLejano = nodoCurrent.Next.Next.value;
while (destino == valorCurrentNodo || destino == valorNextNodo || destino == valorNodoLejano)
{
destino--;
if (destino <= 0)
{
destino = valormasalto;
}
}
return destino;
}
private string nodosToString(Nodo currentNodo, int valorInicial)
{
while (currentNodo.value != valorInicial)
{
currentNodo = currentNodo.Next;
}
currentNodo = currentNodo.Next;
string str = "";
do
{
str += currentNodo.value.ToString();
currentNodo = currentNodo.Next;
}
while (currentNodo.value != valorInicial);
return str;
}
}