5 kyu

Jigsaw Puzzle

Description:

You are given a jigsaw puzzle as an array of pieces. Each piece has four sides (left, right, top, and bottom). Each side tells an ID of the matching side of another piece. Two pieces connect if they each have a side (piece1.left === piece2.right OR piece1.top === piece2.bottom) with the same ID.

For Example:

const puzzlePiece1 = {
    left: '1',
    right: false,
    top: '3',
    bottom: '4',
};
const puzzlePiece2 = {
    left: '5',
    right: false,
    top: '7',
    bottom: '3',
};

In the above example, puzzlePiece1.top === puzzlePiece2.bottom. These two pieces connect! puzzlePiece1 is directly below puzzlePiece2. Also note that right is false so these pieces are on the right side of the puzzle and do not have a right side piece.

Your function should return back a 2-dimensional array of the rows of the pieces of the puzzle laid out in the order of the assembled puzzle.

Notes:

  • Side IDs are always a string, or false for when they are an edge piece
  • The tests will check for objects' exact equality so if you return copies of objects your test results will fail
  • All puzzles are complete with no missing nor rotated pieces
  • All puzzles will have between 1-50 rows and 1-50 columns
  • All IDs are unique; you don't have to worry that an ID of 3 for a left side can't match an ID of 3 for a top side

Example completed reassembled 5X5 puzzle:

const puzzle = [
  [
    {top: false, left: false, right: '1', bottom: 'a'},
    {top: false, left: '1', right: '2', bottom: 'b'},
    {top: false, left: '2', right: '3', bottom: 'c'},
    {top: false, left: '3', right: '4', bottom: 'd'},
    {top: false, left: '4', right: false, bottom: 'e'},
  ], [
    {top: 'a', left: false, right: '5', bottom: 'f'},
    {top: 'b', left: '5', right: '6', bottom: 'g'},
    {top: 'c', left: '6', right: '7', bottom: 'h'},
    {top: 'd', left: '7', right: '8', bottom: 'i'},
    {top: 'e', left: '8', right: false, bottom: 'j'},
  ], [
    {top: 'f', left: false, right: '9', bottom: 'k'},
    {top: 'g', left: '9', right: '10', bottom: 'l'},
    {top: 'h', left: '10', right: '11', bottom: 'm'},
    {top: 'i', left: '11', right: '12', bottom: 'n'},
    {top: 'j', left: '12', right: false, bottom: 'o'},
  ], [
    {top: 'k', left: false, right: '13', bottom: 'p'},
    {top: 'l', left: '13', right: '14', bottom: 'q'},
    {top: 'm', left: '14', right: '15', bottom: 'r'},
    {top: 'n', left: '15', right: '16', bottom: 's'},
    {top: 'o', left: '16', right: false, bottom: 't'},
  ], [
    {top: 'p', left: false, right: '17', bottom: false},
    {top: 'q', left: '17', right: '18', bottom: false},
    {top: 'r', left: '18', right: '19', bottom: false},
    {top: 's', left: '19', right: '20', bottom: false},
    {top: 't', left: '20', right: false, bottom: false},
  ]
];

Example shuffled pieces of the above 5X5 complete puzzle sent to your assemblePuzzle function:

[ { top: false, left: '4', right: false, bottom: 'e' },
  { top: 'q', left: '17', right: '18', bottom: false },
  { top: 'c', left: '6', right: '7', bottom: 'h' },
  { top: false, left: false, right: '1', bottom: 'a' },
  { top: 'j', left: '12', right: false, bottom: 'o' },
  { top: 'k', left: false, right: '13', bottom: 'p' },
  { top: 'o', left: '16', right: false, bottom: 't' },
  { top: 'm', left: '14', right: '15', bottom: 'r' },
  { top: false, left: '3', right: '4', bottom: 'd' },
  { top: 'f', left: false, right: '9', bottom: 'k' },
  { top: 'd', left: '7', right: '8', bottom: 'i' },
  { top: 'r', left: '18', right: '19', bottom: false },
  { top: 'b', left: '5', right: '6', bottom: 'g' },
  { top: 'g', left: '9', right: '10', bottom: 'l' },
  { top: false, left: '2', right: '3', bottom: 'c' },
  { top: false, left: '1', right: '2', bottom: 'b' },
  { top: 's', left: '19', right: '20', bottom: false },
  { top: 't', left: '20', right: false, bottom: false },
  { top: 'e', left: '8', right: false, bottom: 'j' },
  { top: 'i', left: '11', right: '12', bottom: 'n' },
  { top: 'l', left: '13', right: '14', bottom: 'q' },
  { top: 'p', left: false, right: '17', bottom: false },
  { top: 'a', left: false, right: '5', bottom: 'f' },
  { top: 'h', left: '10', right: '11', bottom: 'm' },
  { top: 'n', left: '15', right: '16', bottom: 's' } ]
Puzzles

Similar Kata:

More By Author:

Check out these other kata created by Austin Haws

Stats:

CreatedJun 7, 2018
PublishedJun 21, 2018
Warriors Trained189
Total Skips7
Total Code Submissions465
Total Times Completed58
JavaScript Completions58
Total Stars8
% of votes with a positive feedback rating98% of 24
Total "Very Satisfied" Votes23
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments15
Average Assessed Rank
5 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • Austin Haws Avatar
  • dfhwze Avatar
Ad