Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 10 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite (converts the output to 0-indexing).

How?

Each anti-diagonal consists of the maximal subset of coordinates that have the same sum. The path traverses these anti-diagonals in this coordinate-sum order. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w[1..w]) Cartesian product (h[1..h]) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum ([r, c]) -> r+c ->= #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 -> r if #diagonal is odd; c otherwise
 ; - (#diagonal) concatenate (that)

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite (converts the output to 0-indexing).

How?

Each anti-diagonal consists of the maximal subset of coordinates that have the same sum. The path traverses these anti-diagonals in this coordinate-sum order. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite (converts the output to 0-indexing).

How?

Each anti-diagonal consists of the maximal subset of coordinates that have the same sum. The path traverses these anti-diagonals in this coordinate-sum order. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - ([1..w]) Cartesian product ([1..h]) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum ([r, c]) -> r+c = #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 -> r if #diagonal is odd; c otherwise
 ; - (#diagonal) concatenate (that)
added 679 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite (converts the output to 0-indexing).

How?

Each anti-diagonal consists of the maximal subset of coordinates that have the same sum. The path traverses the diagonalsthese anti-diagonals in order of theirthis coordinate sum-sum order. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite.

How?

The path traverses the diagonals in order of their coordinate sum. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite (converts the output to 0-indexing).

How?

Each anti-diagonal consists of the maximal subset of coordinates that have the same sum. The path traverses these anti-diagonals in this coordinate-sum order. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)
added 679 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite.

How?

The path traverses the diagonals in order of their coordinate sum. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite.

Jelly, 7 bytes

pS;ị\$Þ

A dyadic Link that accepts \$w\$ on the left and \$h\$ on the right and yields the path as a list of 1-indexed coordinates.

Try it online! Or see the test-suite.

How?

The path traverses the diagonals in order of their coordinate sum. Those with odd sums are traversed in row order, while those with even sums are traversed in column order. (This is all true regardless of whether 0 or 1 indexing is employed.)

pS;ị\$Þ - Link: integer, w; integer h
p - (w) Cartesian product (h) -> coordinates
 Þ - sort by:
 $ - last two links as a monad - f([r, c]):
 S - sum -> r+c -> #diagonal
 \ - last two links as a dyad -f (#diagonal, [r, c]):
 ị - (#diagonal) index into ([r, c]) - 1-based and modular
 ; - (#diagonal) concatenate (that)
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading

AltStyle によって変換されたページ (->オリジナル) /