Example #1
0
        public Solution(string input)
        {
            var blocks = Input.GetBlockLines(input);

            var pieces = blocks.Select(block => PuzzlePiece.Parse(block, true)).ToArray();

            this.puzzle      = new Puzzle().Lay(pieces);
            this.fusedPuzzle = Puzzle.Fuse(puzzle);
        }
Example #2
0
        public void CanParseImages(string stringData, int roughness)
        {
            var lines = stringData.Split(Environment.NewLine);

            var piece = PuzzlePiece.Parse(lines, false);

            var solve = new ImageParser().Solve(piece);

            Assert.AreEqual(roughness, solve);
        }
Example #3
0
        public void CanGetEdges()
        {
            var pieceStr = @"Tile 123:
...#
#..#
...#
#...";

            var piece = PuzzlePiece.Parse(pieceStr.Split(Environment.NewLine), true);
            var edges = piece.GetEdges();

            Assert.AreEqual(123, piece.Id);
            Assert.AreEqual(1, edges[PuzzlePiece.UP]);
            Assert.AreEqual(14, edges[PuzzlePiece.RIGHT]);
            Assert.AreEqual(8, edges[PuzzlePiece.DOWN]);
            Assert.AreEqual(5, edges[PuzzlePiece.LEFT]);
        }
Example #4
0
        public static PuzzlePiece Fuse(PuzzlePiece[][] laidPuzzle)
        {
            long id = new PuzzlePiece[]
            {
                laidPuzzle[0][0],
                laidPuzzle[0][laidPuzzle[0].Length - 1],
                laidPuzzle[laidPuzzle.Length - 1][0],
                laidPuzzle[laidPuzzle.Length - 1][laidPuzzle[0].Length - 1]
            }.Select(piece => piece.Id)
            .Aggregate((a, b) => a * b);

            List <string> stringData = new List <string>();

            for (int puzzleRow = 0; puzzleRow < laidPuzzle.Length; puzzleRow++)
            {
                string[] accumulators = new string[laidPuzzle[puzzleRow][0].GetStrippedStringData().Length];
                for (int puzzlePiece = 0; puzzlePiece < laidPuzzle[puzzleRow].Length; puzzlePiece++)
                {
                    var strippedData = laidPuzzle[puzzleRow][puzzlePiece].GetStrippedStringData();
                    for (int puzzleLine = 0; puzzleLine < strippedData.Length; puzzleLine++)
                    {
                        accumulators[puzzleLine] += strippedData[puzzleLine];
                    }
                }

                for (int n = 0; n < accumulators.Length; n++)
                {
                    stringData.Add(accumulators[n]);
                }
            }

            var pieceString =
                new string[] { $"Tile {id}:" }
            .Concat(stringData)
            .ToArray();

            return(PuzzlePiece.Parse(pieceString, false));
        }
Example #5
0
        [TestCase(8, 1, 14, 8, 5)] // modular
        public void CanRotatePiece(int rotation, int up, int right, int down, int left)
        {
            var pieceStr = @"Tile 123:
...#
#..#
...#
#...";

/* rot1:    rot2:   rot3:   flip:   rot5:   rot6:   rot7:
 #.#.        ...#    ###.    #...    .###    #...    .#.#
 * ....        #...    ....    #..#    ....    ...#    ....
 * ....        #..#    ....    #...    ....    #..#    ....
 * .###        #...    .#.#    ...#    #.#.    ...#    ###.
 */
            var piece = PuzzlePiece.Parse(pieceStr.Split(Environment.NewLine), true);

            piece.Rotation = rotation;
            var edges = piece.GetEdges();

            Assert.AreEqual(up, edges[PuzzlePiece.UP]);
            Assert.AreEqual(right, edges[PuzzlePiece.RIGHT]);
            Assert.AreEqual(down, edges[PuzzlePiece.DOWN]);
            Assert.AreEqual(left, edges[PuzzlePiece.LEFT]);
        }
Example #6
0
        public void FuseIsCorrect(string input, string output)
        {
            var blocks = input.Split(Environment.NewLine + Environment.NewLine);

            var pieces = blocks.Select(block => PuzzlePiece.Parse(block.Split(Environment.NewLine), true)).ToArray();

            var fullPuzzle = new Puzzle().Lay(pieces);

            var fused = Puzzle.Fuse(fullPuzzle);

            var expected = output.Split(Environment.NewLine).Skip(1).ToArray();

            bool match = false;

            for (int rot = 0; rot < 8; rot++)
            {
                fused.Rotation = rot;
                var data = fused.GetStringData();
                Assert.AreEqual(expected.Length, data.Length);

                match = true;
                for (int n = 0; n < expected.Length; n++)
                {
                    if (expected[n] != data[n])
                    {
                        match = false;
                    }
                }
                if (match)
                {
                    break;
                }
            }

            Assert.That(match);
        }