public PixelSet Expand(Dictionary <PixelSet, PixelSet> rules) { if (Grid.Size % 2 == 0) { var pixelSets = new List <PixelSet>(); int squareY = 0; for (int squareX = 0; squareX < Grid.Size / 2 && squareY < Grid.Size / 2;) { bool[,] square = new bool[2, 2]; for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { square[x, y] = Grid.Squares[x + 2 * squareX, y + 2 * squareY]; } } var expandedSquare = new PixelSet(new Grid(square)).ApplyRules(rules); pixelSets.Add(expandedSquare); squareX++; if (squareX % (Grid.Size / 2) == 0) { squareX = 0; squareY += 1; } } return(new PixelSet(pixelSets)); } else { var pixelSets = new List <PixelSet>(); int squareY = 0; for (int squareX = 0; squareX < Grid.Size / 3 && squareY < Grid.Size / 3;) { bool[,] square = new bool[3, 3]; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { square[x, y] = Grid.Squares[x + 3 * squareX, y + 3 * squareY]; } } var expandedSquare = new PixelSet(new Grid(square)).ApplyRules(rules); pixelSets.Add(expandedSquare); squareX++; if (squareX % (Grid.Size / 3) == 0) { squareX = 0; squareY += 1; } } return(new PixelSet(pixelSets)); } }
public static int CountPixelsOnAfterExpansion(IList <string> input, int iterations) { var initialPixelSet = new PixelSet(new Grid(".#./..#/###")); var rules = ReadRules(input); var expanded = initialPixelSet; for (int i = 0; i < iterations; i++) { expanded = expanded.Expand(rules); } return(expanded.OnCount); }
public void Expand() { var pixelSet = new PixelSet(new Grid(".#./..#/###")); string[] input = { "../.# => ##./#../...", ".#./..#/### => #..#/..../..../#..#", }; var rules = PixelArt.ReadRules(input); var rotated = pixelSet.Expand(rules); var expected = new PixelSet(new Grid("#..#/..../..../#..#")); Assert.Equal(expected, rotated); }
public static Dictionary <PixelSet, PixelSet> ReadRules(IList <string> input) { var rules = new Dictionary <PixelSet, PixelSet>(); foreach (var rule in input) { var ruleSplit = Regex.Split(rule, " => "); var pixelInputDescription = ruleSplit[0]; var pixelOutputDescription = ruleSplit[1]; var pixelInput = new PixelSet(new Grid(pixelInputDescription)); var pixelOutput = new PixelSet(new Grid(pixelOutputDescription)); rules.Add(pixelInput, pixelOutput); } return(rules); }
public void ConstructorList() { var pixelSet1 = new PixelSet(new Grid("##./#../...")); var pixelSet2 = new PixelSet(new Grid("##./#../...")); var pixelSet3 = new PixelSet(new Grid("##./#../...")); var pixelSet4 = new PixelSet(new Grid("##./#../...")); var pixelSet = new PixelSet(new List <PixelSet> { pixelSet1, pixelSet2, pixelSet3, pixelSet4, }); var expectedPixelSet = new PixelSet(new Grid("##.##./#..#../....../##.##./#..#../......")); Assert.Equal(expectedPixelSet, pixelSet); }
protected bool Equals(PixelSet other) { return(Equals(Grid, other.Grid)); }