23
\$\begingroup\$

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 0s (.s) are present in spots left by moved 1s.

This becomes the following:

....
....
.##.
####

Which translates back to:

0000
0000
0110
1111

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

Another test case

[10, 17, 19, 23]

This becomes:

01010
10001
10011
10111

which becomes:

00000
10011
10011
11111

translating back to:

[0, 19, 19, 31]

Challenge

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 , so the shortest answer in bytes wins!

\$\endgroup\$
6
  • \$\begingroup\$ Sandbox Post \$\endgroup\$
    – hyperneutrino
    Commented Jul 29, 2017 at 19:36
  • 1
    \$\begingroup\$ More testcases? You know, non-square testcases would be good. \$\endgroup\$
    – Leaky Nun
    Commented Jul 29, 2017 at 19:44
  • \$\begingroup\$ @LeakyNun Sure. I'll do that. \$\endgroup\$
    – hyperneutrino
    Commented Jul 29, 2017 at 19:44
  • \$\begingroup\$ That's just a sorting problem for bit arrays. \$\endgroup\$ Commented Jul 30, 2017 at 19:50
  • \$\begingroup\$ @MarcusMüller You're right - I realized that after the MATL answer :P \$\endgroup\$
    – hyperneutrino
    Commented Jul 30, 2017 at 20:14

16 Answers 16

29
\$\begingroup\$

MATL, 4 bytes

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
\$\endgroup\$
8
  • \$\begingroup\$ o_O How does this work :o \$\endgroup\$
    – hyperneutrino
    Commented Jul 29, 2017 at 19:48
  • 1
    \$\begingroup\$ Did MATL just out-golf Jelly by 4 bytes? o_O \$\endgroup\$ Commented Jul 29, 2017 at 19:49
  • \$\begingroup\$ 5 bytes now :-p \$\endgroup\$
    – Leaky Nun
    Commented Jul 29, 2017 at 19:52
  • \$\begingroup\$ I never thought there'd be a built-in to move the ones to the bottom xD +1 \$\endgroup\$
    – hyperneutrino
    Commented Jul 29, 2017 at 19:52
  • 1
    \$\begingroup\$ @totallyhuman well, wait till Dennis comes \$\endgroup\$ Commented Jul 29, 2017 at 23:58
6
\$\begingroup\$

Python, 68 bytes

f=lambda a:a and[x|y&a[0]for x,y in zip([0]+f(a[1:]),f(a[1:])+[-1])]

Try it online!

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 50 bytes

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.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 9 bytes

BUz0Ṣ€ZUḄ

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python 3.5, 60 bytes

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

Try it online!

Takes input like f(2, 6, 9, 4). Assumes input is non-empty. Uses a lot of tuple unpacking.

\$\endgroup\$
3
\$\begingroup\$

Japt, 16 bytes

m¤z3 ®¬n qÃz mn2

Try it online! using the -Q flag to format the array result.

Explanation

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
\$\endgroup\$
2
  • \$\begingroup\$ I think you can save a byte with mì2 z3 mn z mì2 \$\endgroup\$ Commented Jul 30, 2017 at 1:10
  • \$\begingroup\$ @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 1s, unlike spaces, which are sorted to the left. \$\endgroup\$ Commented Jul 30, 2017 at 1:15
3
\$\begingroup\$

Mathematica, 64 bytes

#~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.

\$\endgroup\$
3
\$\begingroup\$

Octave, 29 25 bytes

4 bytes saved thanks to @Stewie

@(x)bi2de(sort(de2bi(x)))
\$\endgroup\$
2
  • \$\begingroup\$ de2bi/bi2de saves 4 bytes in octave. Works on octave-online.net. \$\endgroup\$ Commented Jul 30, 2017 at 15:27
  • \$\begingroup\$ @StewieGriffin Thanks! \$\endgroup\$
    – Suever
    Commented Jul 30, 2017 at 18:02
2
\$\begingroup\$

J, 13 bytes

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

Try it online!

Explanation

/:~"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
\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 15:32
  • \$\begingroup\$ @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. \$\endgroup\$
    – miles
    Commented Aug 1, 2017 at 2:58
2
\$\begingroup\$

05AB1E, 9 bytes

bí0ζR€{øC

Try it online!

Kinda different algorithm from Magic's.

\$\endgroup\$
3
  • \$\begingroup\$ ζ, damnit. Deleted mine, take my +1. \$\endgroup\$ Commented Jul 31, 2017 at 13:28
  • \$\begingroup\$ @MagicOctopusUrn Why did you delete yours? No need to. \$\endgroup\$ Commented Jul 31, 2017 at 13:29
  • \$\begingroup\$ It's not really much different (in terms of algorithm) and this was 25% better. \$\endgroup\$ Commented Jul 31, 2017 at 13:31
2
\$\begingroup\$

Dyalog APL, 24 21 19 bytes

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

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

How?

  • 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)
\$\endgroup\$
2
\$\begingroup\$

Dyalog APL (23 characters)

{2⊥¨↓⍉↑{⍵[⍋⍵]}¨↓2⊥⍣¯1⊢⍵}
  1. Convert the input arguments into a binary matrix
  2. Split the matrix into columns
  3. Sort the columns into ascending order
  4. Convert the sorted rows back into decimal

Example

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

Thanks to Zacharý for correcting me on this one.

\$\endgroup\$
5
  • \$\begingroup\$ You can replace with (⊥⍣¯1)⍵ with ⊥⍣¯1⊢⍵. Also, I don't think you need the axis specification on split (↓[1]=>). \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 14:33
  • \$\begingroup\$ Oh, and you're supposed to convert it back to a list! \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 14:46
  • \$\begingroup\$ This is invalid. \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 17:28
  • \$\begingroup\$ Thank you, Zacharý, I was working on this late last night and I think I misread the problem. I've modified my solution now. \$\endgroup\$ Commented Jul 30, 2017 at 18:59
  • 1
    \$\begingroup\$ Well, good job! (⊥⍣¯1 really needs to be a builtin). And thank you for actually getting my username right. \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 19:32
1
\$\begingroup\$

JavaScript, 127 125 bytes

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()

Try it online

-2 bytes thanks to Cows quack

\$\endgroup\$
1
  • \$\begingroup\$ (1<<c)&e can become 2**c&e \$\endgroup\$
    – user41805
    Commented Jul 29, 2017 at 20:30
1
\$\begingroup\$

Python 2, 142 bytes

... 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.

\$\endgroup\$
2
  • \$\begingroup\$ 142 bytes, you have some redundant parenthesis. \$\endgroup\$
    – Mr. Xcoder
    Commented Jul 30, 2017 at 5:39
  • \$\begingroup\$ @Mr.Xcoder , oh yes that was silly \$\endgroup\$
    – Daniel
    Commented Jul 30, 2017 at 16:24
1
\$\begingroup\$

Japt, 9 bytes

m¤ù yñ mÍ

Try it

m¤ù yñ mÍ     :Implicit input of array
m             :Map
 ¤            :  To binary strings
  ù           :Left pad each with spaces to the length of the longest
    y         :Transpose
     ñ        :Sort each and transpose back
       m      :Map
        Í     :  To decimal
\$\endgroup\$
1
\$\begingroup\$

Husk, 11 bytes

mḋTmOT0mo↔ḋ

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.