private static long Run(List <Tile> state, int size, out InfiniteGrid <TileWithOrientation> output) { Console.WriteLine("================================"); var first = state.First(); var rest = state.Skip(1).ToList(); for (var orientation = 0; orientation < 8; orientation++) { var grid = Search(rest, new InfiniteGrid <TileWithOrientation> { [Position.Zero] = new TileWithOrientation(first, orientation) }, size); if (grid == null) { continue; } var(top, right, bottom, left) = (grid.Top, grid.Right, grid.Bottom, grid.Left); output = grid; return(grid[new Position(left, top)].Tile.Id * grid[new Position(left, bottom)].Tile.Id * grid[new Position(right, top)].Tile.Id * grid[new Position(right, bottom)].Tile.Id); } throw new ApplicationException(); }
private static void Visualize(InfiniteGrid <TileWithOrientation> grid) { for (var y = grid.Top; y <= grid.Bottom; ++y) { for (var x = grid.Left; x <= grid.Right; ++x) { Console.Write(grid.TryPosition(new Position(x, y), out var cell) ? cell.Tile.GridId.ToString() : "."); } Console.WriteLine(); } Console.WriteLine(); }
private static long Run2(InfiniteGrid <TileWithOrientation> grid, List <string> seaMonster) { var bitmap = new List <List <char> >(); for (var y = grid.Top; y <= grid.Bottom; y++) { var row = RemoveEdges(grid[new Position(grid.Left, y)].Bitmap); for (var x = grid.Left + 1; x <= grid.Right; x++) { var tile = RemoveEdges(grid[new Position(x, y)].Bitmap); row = AddInColumn(row, tile); } if (bitmap.Count == 0) { bitmap = row; } else { bitmap = AddInRows(bitmap, row); } } for (var orientation = 0; orientation < 8; orientation++) { var count = 0; var newBitmap = TranslateBitmap(bitmap, orientation).Select(line => line.Join("")).ToList(); for (var y = 0; y < newBitmap.Count - seaMonster.Count + 1; y++) { var index1 = SearchLine(newBitmap[y], seaMonster[0]); var index2 = SearchLine(newBitmap[y + 1], seaMonster[1]); var index3 = SearchLine(newBitmap[y + 2], seaMonster[2]); var isect = index1.Intersect(index2).Intersect(index3).ToHashSet(); if (isect.Any()) { count += isect.Count(); } } if (count > 0) { return(newBitmap.Select(it => it.Count(c => c == '#')).Sum() - seaMonster.Select(it => it.Count(c => c == '#')).Sum() * count); } } throw new ApplicationException(); }
public GridCollider(int cellWidth, int cellHeight) { grid = new InfiniteGrid <bool>(); CellWidth = cellWidth; CellHeight = cellHeight; }
private static InfiniteGrid <TileWithOrientation>?Search(List <Tile> tiles, InfiniteGrid <TileWithOrientation> grid, int size) { if (grid.Width > size || grid.Height > size) { return(null); } if (grid.Width == size && grid.Height == size && grid.Positions().Count() == size * size) { return(grid); } var openPositions = grid.Positions() .SelectMany(p => p.Orthogonal()) .Where(p => !grid.Contains(p)); foreach (var position in openPositions) { var constraints = new List <FacingAndValue> (); foreach (var facing in new[] { Vector.North, Vector.East, Vector.South, Vector.West }.Zip(new[] { 0, 1, 2, 3 })) { if (grid.TryPosition(position + facing.First, out var borderingCell)) { var borderingCellsFacing = facing.Second switch { 0 => 2, 1 => 3, 2 => 0, 3 => 1, _ => throw new ApplicationException() }; constraints.Add(new FacingAndValue(facing: facing.Second, borderingCell.Borders[borderingCellsFacing])); } } var validTiles = new List <TileWithOrientation>(); foreach (var tile in tiles) { var c = constraints.First(); if (!tile.Orientations.TryGetValue(c, out var proposed)) { continue; } foreach (var constraint in constraints.Skip(1)) { if (!tile.Orientations.TryGetValue(constraint, out var proposed2) || proposed != proposed2) { proposed = null; break; } } if (proposed == null) { break; } validTiles.Add(proposed); } foreach (var tile in validTiles) { var gridCopy = grid.Clone(); gridCopy.Add(position, tile); var tilesCopy = tiles.ToList(); tilesCopy.Remove(tile.Tile); var newGrid = Search(tilesCopy, gridCopy, size); if (newGrid != null) { return(newGrid); } } } return(null); }
public NeoDemo() { WindowCreateInfo windowCI = new WindowCreateInfo { X = 50, Y = 50, WindowWidth = 960, WindowHeight = 540, WindowInitialState = WindowState.Normal, WindowTitle = "Veldrid NeoDemo" }; RenderContextCreateInfo rcCI = new RenderContextCreateInfo(); rcCI.DebugContext = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); VeldridStartup.CreateWindowAndRenderContext(ref windowCI, ref rcCI, out _window, out _rc); _window.Resized += () => _windowResized = true; _sc.CreateDeviceObjects(_rc); CommonMaterials.CreateAllDeviceObjects(_rc); _scene = new Scene(_window.Width, _window.Height); _sc.SetCurrentScene(_scene); _igRenderable = new ImGuiRenderable(_window.Width, _window.Height); _resizeHandled += (w, h) => _igRenderable.WindowResized(w, h); _igRenderable.CreateDeviceObjects(_rc); _scene.AddRenderable(_igRenderable); _scene.AddUpdateable(_igRenderable); InfiniteGrid grid = new InfiniteGrid(); grid.CreateDeviceObjects(_rc); _scene.AddRenderable(grid); Skybox skybox = Skybox.LoadDefaultSkybox(); skybox.CreateDeviceObjects(_rc); _scene.AddRenderable(skybox); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(10, 10, 10, 10), new Vector3(0, 0, -5), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(5, 5, 5, 5f), new Vector3(-3, -9, 2), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Box(27, 3, 27, 27f), new Vector3(-5, -16, 5), Quaternion.Identity, Vector3.One); AddTexturedMesh( "Textures/spnza_bricks_a_diff.png", PrimitiveShapes.Plane(100, 100, 5), new Vector3(0, -20, 0), Quaternion.Identity, Vector3.One); ShadowmapDrawer texDrawer = new ShadowmapDrawer(_window, () => _sc.NearShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(10, 25); _scene.AddRenderable(texDrawer); texDrawer = new ShadowmapDrawer(_window, () => _sc.MidShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(20 + texDrawer.Size.X, 25); _scene.AddRenderable(texDrawer); texDrawer = new ShadowmapDrawer(_window, () => _sc.FarShadowMapBinding); texDrawer.CreateDeviceObjects(_rc); texDrawer.Position = new Vector2(30 + (texDrawer.Size.X * 2), 25); _scene.AddRenderable(texDrawer); }