public TiledDrawing(Point cellsize, SparseGrid<IDrawing>.Constructor newCell = null) { Cells = new SparseGrid<IDrawing>(cellsize.x, cellsize.y); Changed = new SparseGrid<bool>(cellsize.x, cellsize.y); NewCell = newCell ?? NullCell; }
//private readonly IList<Entity> _static; //private readonly IList<Entity> _active; public Map(int width, int height) { _width = width; _height = height; _actualWidth = width * 2 + 1; _actualHeight = height * 2 + 1; _mapEntities = new SparseGrid <IMapEntity>(); //_static = new List<Entity>(); //_active = new List<Entity>(); }
public MapModel(MapTile tile, MapAttributes attrs) { this.tile = new BindingMapTile(tile); this.Attributes = attrs; this.floatingTiles = new BindingList <Positioned <IMapTile> >(); this.voids = new BindingSparseGrid <bool>(new SparseGrid <bool>(this.Tile.HeightGrid.Width, this.Tile.HeightGrid.Height)); this.featureLocationIndex = new SparseGrid <FeatureInstance>(this.Tile.HeightGrid.Width, this.Tile.HeightGrid.Height); this.Minimap = new Bitmap(252, 252); var g = Graphics.FromImage(this.Minimap); g.FillRectangle(Brushes.White, 0, 0, this.Minimap.Width, this.Minimap.Height); this.FloatingTilesChanged += this.FloatingTilesListChanged; }
private SparseGrid <bool> DoFold(SparseGrid <bool> grid, Fold fold) { var nextGrid = new SparseGrid <bool>(); foreach (var(point, _) in grid.AllDefinedCells) { if (fold.Axis == Axis.X) { if (point.X < fold.Coordinate) { nextGrid.Set(point, true); } else if (point.X == fold.Coordinate) { // Do nothing } else { var gridPoint = new GridPoint(fold.Coordinate - Math.Abs(fold.Coordinate - point.X), point.Y); nextGrid.Set(gridPoint, true); } } else { if (point.Y < fold.Coordinate) { nextGrid.Set(point, true); } else if (point.Y == fold.Coordinate) { // Do nothing } else { var gridPoint = new GridPoint(point.X, fold.Coordinate - Math.Abs(fold.Coordinate - point.Y)); nextGrid.Set(gridPoint, true); } } } return(nextGrid); }
private object Solve(string[] input, bool axisOnly) { var lineSegments = ParseInput(input); if (axisOnly) { lineSegments = lineSegments .Where(x => IsAxisAligned(x.Item1, x.Item2)); } var allPoints = lineSegments.SelectMany(x => PointsOnLine(x.Item1, x.Item2)); var grid = new SparseGrid <int>(); foreach (var gridPoint in allPoints) { grid.Update(gridPoint, 0, count => count + 1); } return(grid.AllDefinedCells.Values.Count(x => x != 1)); }
static void Main(string[] args) { string input = "../../../input.txt"; string sample = "../../../sample.txt"; int gridDim = 12; var tiles = File.ReadLines(input).Paragraphs(); var tilesById = tiles.Select(p => new KeyValuePair <int, Dictionary <Point, char> >(int.Parse(p.First().Substring(5, 4)), SparseGrid.Read(p.Skip(1)))).ToDictionary(); var tilesByEdge = new Dictionary <int, HashSet <int> >(); foreach (var tile in tilesById) { foreach (var edge in PotentialEdges(tile.Value)) { if (tilesByEdge.ContainsKey(edge)) { tilesByEdge[edge].Add(tile.Key); } else { tilesByEdge[edge] = new HashSet <int>() { tile.Key }; } } } var tileToTile = new Dictionary <int, HashSet <Connection> >(); foreach (var tile in tilesById) { int tileId = tile.Key; tileToTile.Add(tileId, new HashSet <Connection>()); foreach (var edge in PotentialEdges(tile.Value)) { foreach (var otherTile in tilesByEdge[edge]) { if (otherTile == tile.Key) { continue; } tileToTile[tileId].Add(new Connection(otherTile, edge)); } } } tileToTile.Values.GroupBy(s => s.Count()).Select(g => { Console.WriteLine($"{g.Count()} tiles with {g.Key} possible connections"); return(0); }).ToList(); tilesByEdge.Values.GroupBy(s => s.Count()).Select(g => { Console.WriteLine($"{g.Count()} possible edges with {g.Key} tiles sharing them"); return(0); }).ToList(); Console.WriteLine(tileToTile.Where(kv => kv.Value.Count() == 4).Select(kv => kv.Key).Aggregate(1L, (a, b) => a * b)); Dictionary <Point, KeyValuePair <int, Dictionary <Point, Char> > > tilesByPos = new Dictionary <Point, KeyValuePair <int, Dictionary <Point, char> > >(); int topLeftId = tileToTile.Where(kv => kv.Value.Count() == 4).First().Key; var topLeftTile = tilesById[topLeftId]; int firstEdge = tileToTile[topLeftId].First().EdgeCode; IEnumerable <int> secondEdgeCandidates = tileToTile[topLeftId].Skip(1).Select(x => x.EdgeCode); int RightEdge(Dictionary <Point, Char> tile) => PotentialEdges(tile).Skip(4).First(); int ReverseRightEdge(Dictionary <Point, Char> tile) => PotentialEdges(tile).Skip(5).First(); int BottomEdge(Dictionary <Point, Char> tile) => PotentialEdges(tile).Skip(0).First(); int LeftEdge(Dictionary <Point, Char> tile) => PotentialEdges(tile).Skip(6).First(); int TopEdge(Dictionary <Point, Char> tile) => PotentialEdges(tile).Skip(2).First(); var rightMatches = Orientations(topLeftTile).Where(tile => RightEdge(tile) == firstEdge || ReverseRightEdge(tile) == firstEdge); var bottomMatch = rightMatches.First(tile => secondEdgeCandidates.Any(s => BottomEdge(tile) == s)); Point tilePos = new Point(0, 0); tilesByPos.Add(tilePos, new KeyValuePair <int, Dictionary <Point, Char> >(topLeftId, bottomMatch)); int y = 0; for (int x = 1; x < gridDim; ++x) { var prev = tilesByPos[new Point(x - 1, y)]; int edgeToMatch = RightEdge(prev.Value); int matchingTileId = tilesByEdge[edgeToMatch].First(id => id != prev.Key); var matchingOrientation = Orientations(tilesById[matchingTileId]).First(tile => LeftEdge(tile) == edgeToMatch); tilesByPos.Add(new Point(x, y), new KeyValuePair <int, Dictionary <Point, char> >(matchingTileId, matchingOrientation)); } for (y = 1; y < gridDim; ++y) { for (int x = 0; x < gridDim; ++x) { var above = tilesByPos[new Point(x, y - 1)]; int edgeToMatch = BottomEdge(above.Value); int matchingTileId = tilesByEdge[edgeToMatch].First(id => id != above.Key); var matchingOrientation = Orientations(tilesById[matchingTileId]).First(tile => TopEdge(tile) == edgeToMatch); tilesByPos.Add(new Point(x, y), new KeyValuePair <int, Dictionary <Point, char> >(matchingTileId, matchingOrientation)); } } for (y = 0; y < gridDim; ++y) { for (int x = 0; x < gridDim; ++x) { Console.Write($"{tilesByPos[new Point(x, y)].Key} "); } Console.WriteLine(" "); } Dictionary <Point, char> combined = new Dictionary <Point, char>(); for (int gy = 0; gy < gridDim; gy++) { for (int gx = 0; gx < gridDim; gx++) { var tile = tilesByPos[new Point(gx, gy)].Value; for (y = 0; y < 10; y++) { for (int x = 0; x < 10; ++x) { combined.Add(new Point(10 * gx + x, 10 * gy + y), tile[new Point(x, y)]); } } } } SparseGrid.Print(combined, c => c); combined = new Dictionary <Point, char>(); for (int gy = 0; gy < gridDim; gy++) { for (int gx = 0; gx < gridDim; gx++) { var tile = RemoveEdge(tilesByPos[new Point(gx, gy)].Value); for (y = 0; y < 8; y++) { for (int x = 0; x < 8; ++x) { combined.Add(new Point(8 * gx + x, 8 * gy + y), tile[new Point(x, y)]); } } } } SparseGrid.Print(combined, c => c); var seaMonster = SparseGrid.ReadFromFile("../../../seamonster.txt"); var filtered = combined.ToDictionary(); foreach (var monster in Orientations(seaMonster)) { Console.WriteLine("Checking for Monster:"); Console.WriteLine("------"); SparseGrid.Print(monster, c => c); Console.WriteLine("------"); for (int y0 = 0; y0 < gridDim * 8; ++y0) { for (int x0 = 0; x0 < gridDim * 8; ++x0) { Point pos0 = new Point(x0, y0); var toCheck = monster.Where(kv => kv.Value == '#').Select(kv => kv.Key + pos0); if (toCheck.All(pos => combined.GetOrElse(pos, '.') == '#')) { foreach (var pos in toCheck) { filtered[pos] = 'O'; } } } } } SparseGrid.Print(filtered, c => c); Console.WriteLine(filtered.Count(kv => kv.Value == '#')); // 5328 is too high // 4747 is too high // 2179 is too high }