-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemA.cs
More file actions
156 lines (131 loc) · 4.01 KB
/
ProblemA.cs
File metadata and controls
156 lines (131 loc) · 4.01 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
using System;
using System.IO;
/// <summary>
/// @author: jaracoder
/// @date: 08/04/2017
///
/// Oversized Pancake Flipper.
/// Problem A - Qualification Round 2017 - Google Code Jam
/// </summary>
///
namespace GoogleCodeJam17
{
public class ProblemA
{
static int totalCases;
static string inputFile = "test.txt";
static string[] inputLines = null;
static string outputFile = "solvedA.in";
static string[] outputLines = null;
static int TotalVueltas = 0;
static int PosicionInicial = 1;
static void Main()
{
try
{
inputLines = GetLinesFromFile();
totalCases = Int32.Parse(inputLines[0]);
outputLines = new string[totalCases];
string pancakes;
int flipperSize;
for (int i = 1; i < inputLines.Length; i++)
{
pancakes = inputLines[i].Split(' ')[0];
flipperSize = Convert.ToInt32(inputLines[i].Split(' ')[1]);
string number = GetNumber(pancakes, flipperSize);
outputLines[i - 1] = "Case #" + i + ": " + number;
}
SaveLinesOnFile(outputLines);
}
catch
{
}
}
// Pega la vuelta a un número determinado de galletas,
// dependiendo del tamaño de la espátula.
// Continua desde la posición anterior.
// Contempla todas las posibilidades recorriendo de derecha a izquierda indistintamente.
static string GetNumber(string pancakes, int flipperSize)
{
if (pancakes.Contains("-"))
{
int total;
// Recorre la fila desde la posición inicial hacia la derecha
string volteo = "";
for (int i = 0; i < pancakes.Length; i++)
{
volteo += pancakes[i];
if (volteo.Length.Equals(flipperSize))
{
if (volteo.Contains("-"))
{
TotalVueltas++;
pancakes = pancakes.Substring(i - flipperSize);
}
volteo = "";
}
}
}
// Comprueba si todas se encuentran en su cara feliz
// Retorna el número total acumulado de vueltas realizadas.
if (!pancakes.Contains("-"))
{
return TotalVueltas.ToString();
}
return "IMPOSSIBLE";
}
/// <summary>
/// Check if it's a tidy number
/// </summary>
static bool IsTidy(string number)
{
if (number.Length == 1)
return true;
for (int i = 0; i < number.Length; i++)
{
if (i == number.Length - 1)
{
if (number[i] < number[i - 1])
{
return false;
}
}
else
{
if (number[i] > number[i + 1])
{
return false;
}
}
}
return true;
}
/// <summary>
/// Gets all lines of a text file.
/// </summary>
static string[] GetLinesFromFile()
{
string[] lines;
try
{
lines = File.ReadAllLines(inputFile);
}
catch
{
lines = null;
}
return lines;
}
/// <summary>
/// Gets all lines of a text file.
/// </summary>
static void SaveLinesOnFile(string[] lines)
{
try
{
File.WriteAllLines(outputFile, lines);
}
catch { }
}
}
}