Skip to main content
added 2 characters in body
Source Link
0xff
  • 829
  • 3
  • 6
deleted 1150 characters in body
Source Link
0xff
  • 829
  • 3
  • 6

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity.

For example

[[1,0,1,1,0,1,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,1,1,1]
 [0,1,1,0,1,1,0]
 [1,1,0,1,0,0,1]]

Should result in

[[0,0,0,0,0,0,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,0,1,0]
 [1,1,1,1,1,1,1]
 [1,1,1,1,1,1,1]]

As all \$1\$'s have been dropped down.

input

Input will be atleast \$1\times 1\$. You may take input in all reasonable forms (Bitsets, arrays, Lists) and either output the result, return a new Bitset, array or list or simply modify the input.

This is code golf, so the answer with the fewest bytes wins!

test-cases

More test-cases:Apply gravity to this matrix

[[1,0]                   [[0,0]
 [0,0]            ->      [1,0]
 [1,0]]                   [1,0]]

[[1,1,1,1,1]            [[1,0,1,1,0]
 [1,0,1,1,0]      ->     [1,1,1,1,0]
 [1,1,1,1,0]]            [1,1,1,1,1]]

[[1]]             ->    [[1]]

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity.

For example

[[1,0,1,1,0,1,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,1,1,1]
 [0,1,1,0,1,1,0]
 [1,1,0,1,0,0,1]]

Should result in

[[0,0,0,0,0,0,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,0,1,0]
 [1,1,1,1,1,1,1]
 [1,1,1,1,1,1,1]]

As all \$1\$'s have been dropped down.

input

Input will be atleast \$1\times 1\$. You may take input in all reasonable forms (Bitsets, arrays, Lists) and either output the result, return a new Bitset, array or list or simply modify the input.

This is code golf, so the answer with the fewest bytes wins!

test-cases

More test-cases:

[[1,0]                   [[0,0]
 [0,0]            ->      [1,0]
 [1,0]]                   [1,0]]

[[1,1,1,1,1]            [[1,0,1,1,0]
 [1,0,1,1,0]      ->     [1,1,1,1,0]
 [1,1,1,1,0]]            [1,1,1,1,1]]

[[1]]             ->    [[1]]
added 27 characters in body
Source Link
0xff
  • 829
  • 3
  • 6

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity.

For example

[[1,0,1,1,0,1,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,1,1,1]
 [0,1,1,0,1,1,0]
 [1,1,0,1,0,0,1]]

Should result in

[[0,0,0,0,0,0,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,0,1,0]
 [1,1,1,1,1,1,1]
 [1,1,1,1,1,1,1]]

As all \$1\$'s have been dropped down.

input

Input will be atleast \$1\times 1\$. You may take input in all reasonable forms (Bitsets, arrays, Lists) and either output the result, return a new Bitset, array or list or simply modify the input.

This is code golf, so the answer with the fewest bytes wins!

test-cases

More test-cases:

[[1,0]                   [[0,0]
 [0,0]            ->      [1,0]
 [1,0]]                   [1,0]]

[[1,1,1,1,1]            [[1,0,1,1,0]
 [1,0,1,1,0]      ->     [1,1,1,1,0]
 [1,1,1,1,0]]            [1,1,1,1,1]]

[[1]]             ->    [[1]]

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity.

For example

[[1,0,1,1,0,1,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,1,1,1]
 [0,1,1,0,1,1,0]
 [1,1,0,1,0,0,1]]

Should result in

[[0,0,0,0,0,0,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,0,1,0]
 [1,1,1,1,1,1,1]
 [1,1,1,1,1,1,1]]

As all \$1\$'s have been dropped down.

Input will be atleast \$1\times 1\$. You may take input in all reasonable forms (Bitsets, arrays, Lists) and either output the result, return a new Bitset, array or list or simply modify the input.

This is code golf, so the answer with the fewest bytes wins!

More test-cases:

[[1,0]                   [[0,0]
 [0,0]            ->      [1,0]
 [1,0]]                   [1,0]]

[[1,1,1,1,1]            [[1,0,1,1,0]
 [1,0,1,1,0]      ->     [1,1,1,1,0]
 [1,1,1,1,0]]            [1,1,1,1,1]]

[[1]]             ->    [[1]]

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity.

For example

[[1,0,1,1,0,1,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,1,1,1]
 [0,1,1,0,1,1,0]
 [1,1,0,1,0,0,1]]

Should result in

[[0,0,0,0,0,0,0]
 [0,0,0,1,0,0,0]
 [1,0,1,1,0,1,0]
 [1,1,1,1,1,1,1]
 [1,1,1,1,1,1,1]]

As all \$1\$'s have been dropped down.

input

Input will be atleast \$1\times 1\$. You may take input in all reasonable forms (Bitsets, arrays, Lists) and either output the result, return a new Bitset, array or list or simply modify the input.

This is code golf, so the answer with the fewest bytes wins!

test-cases

More test-cases:

[[1,0]                   [[0,0]
 [0,0]            ->      [1,0]
 [1,0]]                   [1,0]]

[[1,1,1,1,1]            [[1,0,1,1,0]
 [1,0,1,1,0]      ->     [1,1,1,1,0]
 [1,1,1,1,0]]            [1,1,1,1,1]]

[[1]]             ->    [[1]]
Source Link
0xff
  • 829
  • 3
  • 6
Loading