My little kid has a toy like this:
This toy consists of 10 stackable little buckets, that we are going to number from 1 (the smallest one) to 10 (the biggest one). Sometimes he makes small piles and the toy ends up like this:
We can represent schematically the piles like this:
1 6
4 9 2 7
5 10 3 8
---------- <-- Floor
1 2 3 4 <-- Pile #
Or, put it another way:
[[4,5],[9,10],[1,2,3],[6,7,8]]
This set of bucket piles is easily restackable to rebuild the original set (the first image) just by consecutively placing piles of smaller buckets inside piles of bigger buckets:
1 1 6
2 2 7
1 6 3 6 3 8
4 9 2 7 4 9 7 4 9
5 10 3 8 5 10 8 5 10
---------- > [Pile 3 to 1] > ---------- > [Pile 4 to 2] > ---------- > [Pile 1 to 2] > Done!
1 2 3 4 1 2 3 4 1 2 3 4
Nonetheless, sometimes my kid tries to build towers, or throws buckets away, and the piles end up being inconsistent and the original set cannot be rebuild just by placing one pile inside another. Examples of this:
[[1,3,2],[4]] (the kid tried to build a tower by placing a bigger bucket
over a smaller one, we would need to reorder the buckets
first)
[[1,3,4],[2]] (the kid left aside an unordered bucket, we would need to remove
bucket #1 from pile #1 before restacking)
[[1,2,3],[5]] (the kid lost a bucket, we need to find it first)
Challenge
Given a list of lists of integers representing a set of bucket piles, return a truthy value if the lists represent an easily restackable set of piles, or falsey in any other case.
- Input will be given as a list of lists of integers, representing the buckets from top to bottom for each stack.
- There won't be empty starting piles (you won't get
[[1,2,3],[],[4,5]]
as input). - The total number of buckets can be any within a reasonable integer range.
- My kid only has one set of buckets so there won't be duplicate elements.
- You can select any two consistent (and coherent) values for truthy or falsey.
- The buckets will be labelled from #1 to #N, being
N
the largest integer in the lists of integers. My kid still does not know the concept of zero. - You may receive the input in any reasonable format as long as it represents a set of piles of buckets. Just specify it in your answer if you change the way you receive the input.
- This is code-golf, so may the shortest program/function for each language win!
Examples
Input: [[4,5],[9,10],[1,2,3],[6,7,8]]
Output: Truthy
Input: [[6,7,8,9,10],[1],[2],[3,4,5],[11,12,13]]
Output: Truthy
Input: [[2,3,4],[1],[5,6,7]]
Output: Truthy
Input: [[1,2],[5,6],[7,8,9]]
Output: Falsey (buckets #3 and #4 are missing)
Input: [[2,3,4],[5,6,7]]
Output: Falsey (bucket #1 is missing)
Input: [[1,3,4],[5,7],[2,6]]
Output: Falsey (non-restackable piles)
Input: [[1,4,3],[2],[5,6]]
Output: Falsey (one of the piles is a tower)