|
@@ -7,9 +7,9 @@ public class Grid
|
|
|
private readonly int _width;
|
|
private readonly int _width;
|
|
|
private readonly int _height;
|
|
private readonly int _height;
|
|
|
private Tile? _start;
|
|
private Tile? _start;
|
|
|
- public Tile?[,] Tiles { get; }
|
|
|
|
|
|
|
+ public Tile[,] Tiles { get; }
|
|
|
|
|
|
|
|
- public Grid(Tile?[,] tiles, int width, int height)
|
|
|
|
|
|
|
+ public Grid(Tile[,] tiles, int width, int height)
|
|
|
{
|
|
{
|
|
|
_width = width;
|
|
_width = width;
|
|
|
_height = height;
|
|
_height = height;
|
|
@@ -23,7 +23,7 @@ public class Grid
|
|
|
var lineBuilder = new StringBuilder();
|
|
var lineBuilder = new StringBuilder();
|
|
|
for (var x = 0; x < _width; x++)
|
|
for (var x = 0; x < _width; x++)
|
|
|
{
|
|
{
|
|
|
- lineBuilder.Append(Tiles[x, y]?.Display ?? " ");
|
|
|
|
|
|
|
+ lineBuilder.Append(Tiles[x, y].Display ?? " ");
|
|
|
}
|
|
}
|
|
|
Console.WriteLine(lineBuilder);
|
|
Console.WriteLine(lineBuilder);
|
|
|
}
|
|
}
|
|
@@ -33,7 +33,7 @@ public class Grid
|
|
|
{
|
|
{
|
|
|
foreach (var t in Tiles)
|
|
foreach (var t in Tiles)
|
|
|
{
|
|
{
|
|
|
- if (t?.Pipe == 'S')
|
|
|
|
|
|
|
+ if (t.Pipe == 'S')
|
|
|
{
|
|
{
|
|
|
_start = t;
|
|
_start = t;
|
|
|
break;
|
|
break;
|
|
@@ -44,7 +44,6 @@ public class Grid
|
|
|
throw new Exception("Start is not set yet");
|
|
throw new Exception("Start is not set yet");
|
|
|
}
|
|
}
|
|
|
var queue = new Queue<(Tile Tile, GridPoint Source, int Index)>();
|
|
var queue = new Queue<(Tile Tile, GridPoint Source, int Index)>();
|
|
|
- var num = 0;
|
|
|
|
|
queue.Enqueue((_start, _start.Position, 0));
|
|
queue.Enqueue((_start, _start.Position, 0));
|
|
|
|
|
|
|
|
while (queue.Count > 0)
|
|
while (queue.Count > 0)
|
|
@@ -53,6 +52,7 @@ public class Grid
|
|
|
|
|
|
|
|
if (tile.HasDisplay)
|
|
if (tile.HasDisplay)
|
|
|
{
|
|
{
|
|
|
|
|
+ UpdateStart();
|
|
|
return index;
|
|
return index;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -65,16 +65,61 @@ public class Grid
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
tile.SetDisplay(index);
|
|
tile.SetDisplay(index);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ throw new Exception("No cycle found");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- num++;
|
|
|
|
|
- if (num % 100 == 0)
|
|
|
|
|
|
|
+ public int Enclosed()
|
|
|
|
|
+ {
|
|
|
|
|
+ var enclosed = 0;
|
|
|
|
|
+ for (var y = 0; y < _height; y++)
|
|
|
|
|
+ {
|
|
|
|
|
+ var verticals = 0;
|
|
|
|
|
+ var half = 0;
|
|
|
|
|
+ for (var x = 0; x < _width; x++)
|
|
|
{
|
|
{
|
|
|
- Print();
|
|
|
|
|
- Console.WriteLine();
|
|
|
|
|
|
|
+ var tile = Tiles[x, y];
|
|
|
|
|
+ if (!tile.HasDisplay)
|
|
|
|
|
+ {
|
|
|
|
|
+ enclosed += (verticals % 2 == 1) ? 1 : 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ switch (tile.Pipe)
|
|
|
|
|
+ {
|
|
|
|
|
+ case '|':
|
|
|
|
|
+ verticals++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'F':
|
|
|
|
|
+ case '7':
|
|
|
|
|
+ half += 1;
|
|
|
|
|
+ if (half == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ verticals++;
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'J':
|
|
|
|
|
+ case 'L':
|
|
|
|
|
+ half -= 1;
|
|
|
|
|
+ if (half == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ verticals++;
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (Math.Abs(half) > 1)
|
|
|
|
|
+ {
|
|
|
|
|
+ half = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- throw new Exception("No cycle found");
|
|
|
|
|
|
|
+ return enclosed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Tile? this[GridPoint point]
|
|
public Tile? this[GridPoint point]
|
|
@@ -89,4 +134,54 @@ public class Grid
|
|
|
return Tiles[point.X, point.Y];
|
|
return Tiles[point.X, point.Y];
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private void UpdateStart()
|
|
|
|
|
+ {
|
|
|
|
|
+ var outlets = _start!
|
|
|
|
|
+ .GetConnections()
|
|
|
|
|
+ .Select(p => this[p])
|
|
|
|
|
+ .Where(t => (t?.HasDisplay ?? false) && (t.ConnectsTo(_start.Position)))
|
|
|
|
|
+ .Cast<Tile>()
|
|
|
|
|
+ .ToList();
|
|
|
|
|
+
|
|
|
|
|
+ if (outlets.Count != 2)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new Exception("Only 2 outlets for S expected");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var east = outlets.Any(t => t.Position.X > _start.Position.X);
|
|
|
|
|
+ var west = outlets.Any(t => t.Position.X < _start.Position.X);
|
|
|
|
|
+ var north = outlets.Any(t => t.Position.Y < _start.Position.Y);
|
|
|
|
|
+ var south = outlets.Any(t => t.Position.Y > _start.Position.Y);
|
|
|
|
|
+
|
|
|
|
|
+ var pipe = ' ';
|
|
|
|
|
+ if (north && south)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = '|';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (north && east)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = 'L';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (north && west)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = 'J';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (east && west)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = '-';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (south && east)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = 'F';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (south && west)
|
|
|
|
|
+ {
|
|
|
|
|
+ pipe = '7';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Tiles[_start.Position.X, _start.Position.Y] = new Tile(pipe, _start.Position);
|
|
|
|
|
+ _start = Tiles[_start.Position.X, _start.Position.Y];
|
|
|
|
|
+ _start.SetDisplay(0);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|