This commit is contained in:
2025-04-13 23:05:53 +03:00
parent 03db246d88
commit 5d4c34be39
21 changed files with 566 additions and 121 deletions

View File

@ -1,13 +1,47 @@
namespace omp
{
public class Configurator
class UndergroundException : Exception;
public interface IConfigurator
{
public int xsize;
public int ysize;
public int zCoordinate;
public List<Point> stations = [];
Type PointType { get; }
public int XSize { get; }
public int YSize { get; }
public int ZCoordinate { get; }
public List<Point> Stations { get; }
}
public class Configurator : IConfigurator
{
public Type PointType { get; }
public int XSize { get; }
public int YSize { get; }
public int ZCoordinate { get; }
public List<Point> Stations { get; } = [];
public Configurator(string[] args)
{
List<string> lines = ReadAllLines(args);
bool hasZ = CheckHasZ(lines[1]);
if (hasZ)
{
PointType = typeof(Point3D);
}
else
{
PointType = typeof(Point2D);
}
(XSize, YSize) = ParseSize(lines[0]);
ZCoordinate = ParseZCoord(lines[1], hasZ);
ParseStations(lines, hasZ);
}
private static List<string> ReadAllLines(string[] args)
{
List<string> lines;
@ -32,16 +66,7 @@ namespace omp
throw new ArgumentException("You must either provide path to parameters file as an only cli argument, or insert it into STDIN");
}
ParseParameters(lines);
}
private void ParseParameters(IReadOnlyList<string> lines)
{
bool hasZ = CheckHasZ(lines[1]);
ParseSize(lines[0]);
ParseZCoord(lines[1], hasZ);
ParseStations(lines, hasZ);
return lines;
}
private static bool CheckHasZ(string line)
@ -49,48 +74,65 @@ namespace omp
return line.Split(' ').Length == 1; // line contains a single number, z coordinate
}
private void ParseSize(string line)
private static (int, int) ParseSize(string line)
{
string[] size_parts = line.Split(' ', 2);
xsize = int.Parse(size_parts[0]);
ysize = int.Parse(size_parts[1]);
int xSize = int.Parse(size_parts[0]);
int ySize = int.Parse(size_parts[1]);
return (xSize, ySize);
}
private void ParseZCoord(string line, bool hasZ)
private int ParseZCoord(string line, bool hasZ)
{
int zCoordinate;
if (hasZ)
{
zCoordinate = int.Parse(line);
if (ZCoordinate < 0)
{
throw new UndergroundException();
}
}
else
{
zCoordinate = 0;
zCoordinate = -1;
}
return zCoordinate;
}
private void ParseStations(IReadOnlyList<string> lines, bool hasZ)
{
int firstIndex = 1 + (hasZ ? 1 : 0);
for (int i = firstIndex; i < lines.Count; ++i)
if (hasZ) Parse3DStations(lines);
else Parse2DStations(lines);
}
private void Parse2DStations(IReadOnlyList<string> lines)
{
for (int i = 1; i < lines.Count; ++i)
{
string[] point_parts = lines[i].Split(' ', 2 + (hasZ ? 1 : 0));
string[] point_parts = lines[i].Split(' ', 2);
int x, y, z;
int x = int.Parse(point_parts[0]);
int y = int.Parse(point_parts[1]);
x = int.Parse(point_parts[0]) - 1;
y = int.Parse(point_parts[1]) - 1;
Stations.Add(new Point2D(x, y));
}
}
if (hasZ)
{
z = int.Parse(point_parts[2]);
}
else
{
z = 0;
}
private void Parse3DStations(IReadOnlyList<string> lines)
{
for (int i = 2; i < lines.Count; ++i)
{
string[] point_parts = lines[i].Split(' ', 3);
stations.Add(new Point(x, y, z));
int x = int.Parse(point_parts[0]);
int y = int.Parse(point_parts[1]);
int z = int.Parse(point_parts[2]);
Stations.Add(new Point3D(x, y, z));
}
}
}
}
}