public static Ferry NewFerryWithSwitchedPlaces(Ferry oldFerry) { var newFerry = new Ferry(); newFerry.TileMap = new Dictionary <Point, FloorTile>(); foreach (var tile in oldFerry.TileMap) { var newSeat = new Seat { IsOccupied = true }; if (tile.Value.Seat is null) { newSeat = null; } else if (tile.Value.Seat.IsOccupied && tile.Value.AreMyNeighboursOccupied(tile.Key, 4)) { newSeat.IsOccupied = false; } else if (!tile.Value.Seat.IsOccupied && tile.Value.AreMyNeighboursOccupied(tile.Key, 1)) { newSeat.IsOccupied = false; } newFerry.TileMap.Add(tile.Key, new FloorTile { Coordinate = tile.Key, Ferry = newFerry, Seat = newSeat }); } return(newFerry); }
public bool AreEqual(Ferry ferry2) { foreach (var tile in TileMap) { var tileToCompare = ferry2.TileMap[tile.Key]; if (tile.Value.Seat is null && tileToCompare.Seat is null) { continue; } if (tile.Value.IsOccupied == tileToCompare.IsOccupied) { continue; } return(false); } return(true); }
private static int First(List <string> input) { var ferry = new Ferry(input); bool differentFromLast; var sw = new Stopwatch(); do { sw.Start(); var newFerry = NewFerryWithSwitchedPlaces(ferry); differentFromLast = !ferry.AreEqual(newFerry); ferry = newFerry; sw.Stop(); Debug.WriteLine(sw.Elapsed); sw.Reset(); } while (differentFromLast); return(ferry.OccupiedTiles); }
public static Ferry NewFerryWithSwitchedPlaces2(Ferry oldFerry) { var newFerry = new Ferry(); newFerry.TileMap = new Dictionary <Point, FloorTile>(); foreach (var tile in oldFerry.TileMap) { var newSeat = new Seat(); if (tile.Value.Seat is null) { newSeat = null; } else if (tile.Value.Seat.IsOccupied && tile.Value.NumberOfTilesInLineOfSight() >= 5) { newSeat.IsOccupied = false; } else if (!tile.Value.Seat.IsOccupied && tile.Value.NumberOfTilesInLineOfSight() == 0) { newSeat.IsOccupied = true; } else { newSeat.IsOccupied = tile.Value.Seat.IsOccupied; } var ft = new FloorTile { Coordinate = tile.Key, Ferry = newFerry, Seat = newSeat }; newFerry.TileMap.Add(tile.Key, ft); } return(newFerry); }