public Matrix2D <char> GetTransformedMatrix() { bool rotate, hflip, vflip; (rotate, hflip, vflip) = DecodeTransform(CurrentOrientation); List <string> lines = new List <string>(); for (int y = 0; y < (rotate ? Width : Height); y++) { lines.Add(new string(GetTransformedLine(y, true))); } return(CharMatrix.Build(lines)); }
static void Main(string[] args) { int count1 = 0; int iter1 = 0; int count2 = 0; int iter2 = 0; Performance.TimeRun("part1 + 2 (with IO)", () => { var input = TextFile.ReadStringList("input.txt"); var matrix = CharMatrix.Build(input); var original = matrix.Clone(); (count1, iter1) = SimulateWithAdjecency(matrix); (count2, iter2) = SimulateWithAdjecency(original, adjacencyFunc: FindClosestSeats, crowdedThreshold: 5); }, 10, 10); Console.WriteLine($"Part1: {count1} seats end up occupied ({iter1} iterations)"); Console.WriteLine($"Part2: {count2} seats end up occupied ({iter2} iterations)"); }
static (int, Matrix2D <char>) Part2(Tile[] tiledPicture, int dim) { var chart = CombineTiles(tiledPicture, dim); var seaMonsterLines = new List <string>() { " # ", "# ## ## ###", " # # # # # # ", }; var seaMonster = CharMatrix.Build(seaMonsterLines); bool found = false; Matrix2D <char> transformedChart = null; for (int orientation = 0; orientation < 8; orientation++) { chart.CurrentOrientation = orientation; transformedChart = chart.GetTransformedMatrix(); for (int y = 0; y < transformedChart.Height - seaMonster.Height; y++) { for (int x = 0; x < transformedChart.Width - seaMonster.Width; x++) { if (HasAllPixels(transformedChart, seaMonster, x, y)) { found = true; StampValue(transformedChart, seaMonster, x, y, 'O'); } } } if (found) { break; } } if (!found) { throw new ArgumentException("No sea monsters in image!"); } return(transformedChart.Array.Count(c => c == '#'), transformedChart); }
public Tile(int tileNo, List <string> lines, bool calculateEdges = true) { TileNo = tileNo; matrix = CharMatrix.Build(lines); Width = matrix.Width; Height = matrix.Height; if (calculateEdges) { //cache edges for all orientations: for (int i = 0; i < 8; i++) { bool rotate, hflip, vflip; (rotate, hflip, vflip) = DecodeTransform(i); int[] edges = new int[4]; for (int e = 0; e < 4; e++) { Edge edge = (Edge)e; edges[e] = ToInt(GetTransformedEdge(edge, rotate, hflip, vflip)); } orientedEdges[i] = edges; } } }