Inspired by Create a binary wall

Given a list of positive integers, we can write them out all above each other like so, for `[2, 6, 9, 4]`

as an example:

```
0010
0110
1001
0100
```

We can imagine this as a wall:

```
..#.
.##.
#..#
.#..
```

However, this is a very weak wall, and it has collapsed! Each `1`

(`#`

) falls down until it hits the "ground" or another `1`

(`#`

). The `0`

s (`.`

s) are present in spots left by moved `1`

s.

This becomes the following:

```
....
....
.##.
####
```

Which translates back to:

```
0000
0000
0110
1111
```

Which, as a list of numbers, is `[0, 0, 6, 15]`

.

`[10, 17, 19, 23]`

This becomes:

```
01010
10001
10011
10111
```

which becomes:

```
00000
10011
10011
11111
```

translating back to:

`[0, 19, 19, 31]`

Given a list of positive integers, apply this transformation to the list. Input/Output as lists of positive integers in any reasonable format. Standard loopholes apply.

This is a code-golf, so the shortest answer in bytes wins!

HyperNeutrino 07/29/2017

Sandbox Post

1 Leaky Nun 07/29/2017

More testcases? You know, non-square testcases would be good.

HyperNeutrino 07/29/2017

@LeakyNun Sure. I'll do that.

Marcus Müller 07/30/2017

That's just a sorting problem for bit arrays.

HyperNeutrino 07/30/2017

@MarcusMüller You're right - I realized that after the MATL answer :P

Suever 07/29/2017.

`BSXB`

Try it at MATL Online

**Explanation**

```
% Implicitly grab input as an array
% STACK: [10, 17, 19, 23]
B % Convert each element to binary where each decimal number results in a row
% STACK: [0 1 0 1 0;
% 1 0 0 0 1;
% 1 0 0 1 1;
% 1 0 1 1 1]
S % Sort each column, placing all of the 1's at the bottom of each column
% STACK: [0 0 0 0 0;
% 1 0 0 1 1;
% 1 0 0 1 1;
% 1 1 1 1 1]
XB % Convert each row from its binary representation to its decimal number
% STACK: [0, 19, 19, 31]
% Implicitly display the result
```

Anders Kaseorg 07/29/2017.

Neil 07/29/2017.

`f=a=>a.map(_=>a.map((e,i)=>a[a[i]|=a[--i],i]&=e))&&a`

Explanation: Suppose two rows of the wall were like this:

```
0011
0101
```

The result needs to be this:

```
0001
0111
```

In other words, the first row becomes the AND of the two rows and the second row becomes the OR of the two rows. This just needs to be repeated enough times for all the bits to fall to the bottom.

Justin Mariner 07/29/2017.

`m¤z3 ®¬n qÃz mn2`

Try it online! using the `-Q`

flag to format the array result.

```
m¤z3 ®¬n qÃz mn2 Implicit: U = input array.
[10, 17, 19, 23]
m¤z3 Map U to binary strings and rotate the array left 90°
1010 0111
10001 -> 1011
10011 0001
10111 1000
111
®¬n qÃ Sort each binary string, putting 0s and spaces at the start
0111
0111
0001
0001
111
z mn2 Rotate right 90° and convert each back to a number
0000 0
10011 -> 19
10011 19
11111 31
Implicit output of resulting array
```

@ETHproductions It seems rotating the 2D array, instead of rotating the array of strings, pads each inner array with

`null`

instead of spaces. So that doesn't seem to work. And `null`

is sorted to the right of the `1`

s, unlike spaces, which are sorted to the left. DanTheMan 07/30/2017.

`#~FromDigits~2&/@(Sort/@(PadLeft[#~IntegerDigits~2&/@#]))&`

is `\[Transpose]`

This converts the input (a list of numbers) to a list of lists of digits, pads it to be a square matrix, transposes it, sorts the rows so the 1's "fall" to the bottom, transposes back, then converts back into numbers.

xnor 07/30/2017.

```
def f(a,*t):
if t:b,*r=f(*t);t=f(a|b,*r);a&=b
return(a,*t)
```

Takes input like `f(2, 6, 9, 4)`

. Assumes input is non-empty. Uses a lot of tuple unpacking.

Suever 07/30/2017.

*4 bytes saved thanks to @Stewie*

`@(x)bi2de(sort(de2bi(x)))`

miles 07/29/2017.

`/:~"1&.|:&.#:`

```
/:~"1&.|:&.#: Input: array M
#: Convert each in M to binary with left-padding
|:& Transpose
/:~"1& Sort each row
&.|: Inverse of transpose (which is just transpose)
&.#: Inverse of converting to binary
```

Zacharý 07/30/2017

There's that binary left-padding again, +1. And also, can you explain why you would need to use the inverse of transpose, since it is just transpose?

miles 08/01/2017

@Zacharý The inverses are used to undo the operations used before sorting each row. It's true that the inverse of transpose is just transpose, but another way to see this is as

`<convert from binary> <transpose> <sort each row> <transpose> <convert to binary> M`

, where the first two functions are just the inverses of the last two. Erik the Outgolfer 07/30/2017.

It's not really much different (in terms of algorithm) and this was 25% better.

Zacharý 07/30/2017.

`2⊥↑{⍵[⍋⍵]}¨↓2⊥⍣¯1⊢⎕`

Try it online! (modified so TryAPL accepts it as valid)

`⎕`

evaluated input (arrays are space separated)`2⊥⍣¯1⊢`

converts each each of the arguments to binary (transposed of what is in the question)`↓`

turns a 2D array into a vector of vectors`{⍵[⍋⍵]}¨`

sorts each of the elements of the vector`↑`

turns the vector of vectors into a 2D array again`2⊥`

convert from binary (since it sort of transposes it, we arrive at the correct result)

James Heslip 07/30/2017.

`{2⊥¨↓⍉↑{⍵[⍋⍵]}¨↓2⊥⍣¯1⊢⍵}`

- Convert the input arguments into a binary matrix
- Split the matrix into columns
- Sort the columns into ascending order
- Convert the sorted rows back into decimal

```
{2⊥¨↓⍉↑{⍵[⍋⍵]}¨↓2⊥⍣¯1⊢⍵}10 17 19 23
0 19 19 31
```

Thanks to Zacharý for correcting me on this one.

Zacharý 07/30/2017

You can replace with

`(⊥⍣¯1)⍵`

with `⊥⍣¯1⊢⍵`

. Also, I don't think you need the axis specification on split (`↓[1]`

=>`↓`

). James Heslip 07/30/2017

Thank you, Zacharý, I was working on this late last night and I think I misread the problem. I've modified my solution now.

1 Zacharý 07/30/2017

Well, good job! (

`⊥⍣¯1`

really needs to be a builtin). And thank you for actually getting my username right. ThePirateBay 07/29/2017.

`a=>a[m='map'](_=>b[m]((n,i)=>n&&(b[i]--,d|=1<<i),d=0)&&d,b=[...Array(32)][m]((_,c)=>a[m](e=>d+=!!(2**c&e),d=0)&&d)).reverse()`

*-2 bytes thanks to Cows quack*

Dopapp 07/30/2017.

... and still golfing... hopefully –– Any help appreciated!

`def c(l):b=[bin(n)[2:]for n in l];print[int(n,2)for n in map(''.join,zip(*map(sorted,zip(*['0'*(len(max(b,key=len))-len(x))+x for x in b]))))]`

A big chunk of this is for padding the numbers with zeroes.

More readable:

```
def collapse(nums):
bins = [bin(n)[2:] for n in nums]
bins = [('0'*(len(max(bins, key = len)) - len(x))) + x for x in bins]
print [int(n, 2) for n in map(''.join, zip(*map(sorted, zip(*bins))))]
```

This creates an array of the binary string representations, pads it, rotates it 90º clockwise, sorts each row, rotates it back 90º, and then creates integers out of each row.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

- Print the amount of ones in a binary number without using bitwise operators
- Implementing Binary Arithmetic
- Compute the Binary Sierpinski Triangle Sequence
- Collide against walls
- What's the Binary Time?
- Generate binary matrices which are distinct up to reflections
- What is the reverse (binary) bit number?
- Binary Puzzle Solver
- Is the number binary-heavy?
- Create a binary wall
- Create a binary wall

- Should you play games with a toddler that they're very good at and always win?
- What does AoS mean?
- Are effect sizes really superior to p-values?
- How to introduce a nameless, mysterious character in limited third person?
- Would a castle built at the top of a mesa have unusual weaknesses?
- What seemingly innocuous results in mathematics require advanced proofs?
- What does the Night King really want?
- Is it unprofessional to have a quiet alarm for your own finishing time?
- Why are people unwilling to share information about their salary?
- My first luaLaTeX example
- Reduce string to a snippet of the alphabet
- Understanding "Conformity is a virtue, creativity suspect, humor forbidden, and voice mute"
- A verb for when we actively extract information of others?
- Couldn't one or two or three dragons take care of the Army of the Dead in like a couple hours?
- Internal network address contains my name - problematic?
- Is President Trump right that there was violence on “both sides” in Charlottesville?
- Is there a name for this drop-down anti-pattern?
- Why might a valued mineral be only found on one planet?
- How can a race of eldritch abominations help humanity?
- How do commas in the initialization and increment parts of a for-loop work?
- How big of a deal is blocking copy-pasting on a website?
- What is this weird plug on the other end of my USB 3.0 cord?
- How to poison a creature that measures more than a dozen meters?
- How can I prove that this is a rational number?