Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
203 lines (181 loc) · 6.52 KB
/
Copy pathProgram.cs
File metadata and controls
203 lines (181 loc) · 6.52 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
namespace HedgehokTest
{
internal class Program
{
static void Main(string[] args)
{
string[] colors = { "red", "green", "blue" };
int[] population = new int[3];
bool isValidCombination = false;
int neededColony, smallerColony, largerColony;
int roundCounter = 0;
int total = 0;
total = GetPopulationInput(colors, population, total);
if (!IsValidTotalPopulation(total))
{
return;
};
Display(population);
neededColony = GetValidColorInput();
(smallerColony, largerColony) = GetSmallerAndLargerColonies(neededColony, population);
while (population[smallerColony] + population[largerColony] != 0)
{
if (IsImpossibleToContinue(population, smallerColony, largerColony))
{
isValidCombination = false;
break;
};
if (population[smallerColony] == 0 && population[smallerColony] != population[largerColony])
{
population[neededColony] -= 1;
population[largerColony] -= 1;
population[smallerColony] += 2;
roundCounter++;
}
else
{
population[neededColony] += 2;
population[smallerColony] -= 1;
population[largerColony] -= 1;
roundCounter++;
};
isValidCombination = true;
};
Display(population);
int result = isValidCombination ? roundCounter : -1;
Console.WriteLine($"result:\t\t\t{result}");
}
static int GetPopulationInput(string[] colors, int[] population, int total)
{
for (int i = 0; i < population.Length; i++)
{
bool isValidInput = false;
while (!isValidInput)
{
try
{
Console.Write($"{colors[i]}({i}) hedgehogs:\t");
int input = int.Parse(Console.ReadLine());
if (input >= 0)
{
population[i] = input;
total += input;
isValidInput = true;
}
else
{
Console.WriteLine("Invalid input.\nPlease enter a non-negative integer.");
}
}
catch (FormatException)
{
Console.WriteLine("Invalid input.\nPlease enter a valid integer.");
}
}
}
return total;
}
static bool IsValidTotalPopulation(int total)
{
if (total < 1 || total > int.MaxValue)
{
Console.WriteLine($"Invalid total population.\nThe sum must be between 1 and {int.MaxValue}.");
return false;
}
return true;
}
// check for impossibility of fulfilling the condition
static bool IsImpossibleToContinue(int[] population, int smallerColony, int largerColony)
{
return population[smallerColony] + population[largerColony] == 1;
}
static int GetValidColorInput()
{
int neededColony = -1;
bool isValidColor = false;
while (!isValidColor)
{
Console.Write($"needed color:\t\t");
try
{
neededColony = int.Parse(Console.ReadLine());
if (neededColony >= 0 && neededColony <= 2)
{
isValidColor = true;
}
else
{
Console.WriteLine("Invalid input.\nPlease enter a number between 0 and 2.");
}
}
catch (FormatException)
{
Console.WriteLine("Invalid input.\nPlease enter a valid integer.");
}
}
return neededColony;
}
static (int, int) GetSmallerAndLargerColonies(int neededColony, int[] population)
{
int smallerColony, largerColony;
switch (neededColony)
{
case 0:
if (population[1] != population[2])
{
smallerColony = population[1] < population[2] ? 1 : 2;
largerColony = population[1] > population[2] ? 1 : 2;
}
else
{
smallerColony = 1;
largerColony = 2;
}
break;
case 1:
if (population[0] != population[2])
{
smallerColony = population[0] < population[2] ? 0 : 2;
largerColony = population[0] > population[2] ? 0 : 2;
}
else
{
smallerColony = 0;
largerColony = 2;
}
break;
case 2:
if (population[0] != population[1])
{
smallerColony = population[0] < population[1] ? 0 : 1;
largerColony = population[0] > population[1] ? 0 : 1;
}
else
{
smallerColony = 0;
largerColony = 1;
}
break;
default:
throw new ArgumentException("invalid colony index");
}
return (smallerColony, largerColony);
}
static void Display(int[] values)
{
Console.Write($"hedgehog population:\t");
foreach (int value in values)
{
Console.Write($"{value}; ");
}
Console.WriteLine();
}
}
}