-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.cs
More file actions
184 lines (126 loc) · 4.69 KB
/
day18.cs
File metadata and controls
184 lines (126 loc) · 4.69 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
using System;
using System.IO;
using System.Collections.Generic;
class Day18Class
{
public class Operacion
{
public List<long> numeros = new List<long>();
public List<char> opers = new List<char>();
public long CalcularOperacionFase1()
{
long subtotal = numeros[0];
for (short i = 1; i < numeros.Count; i++)
{
if (opers[i - 1] == '+')
{
subtotal += numeros[i];
}
else if (opers[i - 1] == '*')
{
subtotal *= numeros[i];
}
}
return subtotal;
}
public long CalcularOperacionFase2()
{
long sumaoperacion = numeros[0];
List<long> additionsFirstResult = new List<long>();
for (ushort i = 1; i < numeros.Count; i++)
{
if (opers[i - 1] == '+')
{
sumaoperacion += numeros[i];
}
else if (opers[i - 1] == '*')
{
additionsFirstResult.Add(sumaoperacion);
sumaoperacion = 0;
sumaoperacion += numeros[i];
}
}
additionsFirstResult.Add(sumaoperacion);
long subtotal = 1;
for (ushort i = 0; i < additionsFirstResult.Count; i++)
{
subtotal *= additionsFirstResult[i];
}
return subtotal;
}
}
public void Start()
{
// https://adventofcode.com/2020/day/18
Console.WriteLine("****** DIA 18 ******");
var lines = File.ReadAllLines(@"./inputs/inputs_dia18.txt");
Console.WriteLine("****** FASE 1 ******");
List<Operacion> operaciones = new List<Operacion>();
long total = 0;
for (ushort i = 0; i < lines.Length; i++)
{
char[] fila = lines[i].Replace(" ", "").ToCharArray();
operaciones.Add( new Operacion());
for (ushort p = 0; p < fila.Length; p++)
{
switch(fila[p])
{
case '+':
operaciones[operaciones.Count - 1].opers.Add('+');
break;
case '*':
operaciones[operaciones.Count - 1].opers.Add('*');
break;
case '(':
operaciones.Add(new Operacion());
break;
case ')':
long t = operaciones[operaciones.Count - 1].CalcularOperacionFase1();
operaciones.RemoveAt(operaciones.Count - 1);
operaciones[operaciones.Count - 1].numeros.Add(t);
break;
default:
operaciones[operaciones.Count - 1].numeros.Add(long.Parse(fila[p].ToString()));
break;
}
}
total += operaciones[operaciones.Count - 1].CalcularOperacionFase1();
operaciones.RemoveAt(operaciones.Count - 1);
}
Console.WriteLine("La suma es=" + total);
Console.WriteLine("****** FASE 2 ******");
operaciones.Clear();
total = 0;
for (ushort i = 0; i < lines.Length; i++)
{
char[] fila = lines[i].Replace(" ", "").ToCharArray();
operaciones.Add(new Operacion());
for (ushort p = 0; p < fila.Length; p++)
{
switch (fila[p])
{
case '+':
operaciones[operaciones.Count - 1].opers.Add('+');
break;
case '*':
operaciones[operaciones.Count - 1].opers.Add('*');
break;
case '(':
operaciones.Add(new Operacion());
break;
case ')':
long t = operaciones[operaciones.Count - 1].CalcularOperacionFase2();
operaciones.RemoveAt(operaciones.Count - 1);
operaciones[operaciones.Count - 1].numeros.Add(t);
break;
default:
operaciones[operaciones.Count - 1].numeros.Add(long.Parse(fila[p].ToString()));
break;
}
}
total += operaciones[operaciones.Count - 1].CalcularOperacionFase2();
operaciones.RemoveAt(operaciones.Count - 1);
}
Console.WriteLine("El resultado es=" + total);
}
}