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

saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (V8), 7773 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||i=j=0;~j||(j=0,++i<w+hj=++i);j++<w+h;j--)i-(x=i&1?j:i-j:j)<h&x<w&&print(x,i-x)}

Try it online! Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

NB: Processing the longest diagonal never brings any new cell. It's just golfier to include it.

JavaScript (V8), 77 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)i-(x=i&1?j:i-j)<h&x<w&&print(x,i-x)}

Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

NB: Processing the longest diagonal never brings any new cell. It's just golfier to include it.

JavaScript (V8), 73 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;~j||(j=++i)<w+h;j--)i-(x=i&1?i-j:j)<h&x<w&&print(x,i-x)}

Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

NB: Processing the longest diagonal never brings any new cell. It's just golfier to include it.

saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (V8), 7977 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)i-(x=i&1?j:i-j,y=i-x)<h&x<w&&print(x,yi-x)}

Try it online! Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

NB: Processing the longest diagonal never brings any new cell. It's just golfier to include it.

JavaScript (V8), 79 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)(x=i&1?j:i-j,y=i-x)<h&x<w&&print(x,y)}

Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

JavaScript (V8), 77 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)i-(x=i&1?j:i-j)<h&x<w&&print(x,i-x)}

Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

NB: Processing the longest diagonal never brings any new cell. It's just golfier to include it.

minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (V8), 79 bytes

Expects (w)(h). Prints allthe coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)(x=i&1?j:i-j,y=i-x)<h&x<w&&print(x,y)}

Try it online!

How?

We walk through allthe upper-left diagonals of length \1ドル\$ toa square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around, printing. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

JavaScript (V8), 79 bytes

Expects (w)(h). Prints all coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)(x=i&1?j:i-j,y=i-x)<h&x<w&&print(x,y)}

Try it online!

How?

We walk through all diagonals of length \1ドル\$ to \$w+h\$, going alternately from top-right to bottom-left and the other way around, printing only coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

JavaScript (V8), 79 bytes

Expects (w)(h). Prints the coordinates.

w=>h=>{for(i=j=0;j<=i||(j=0,++i<w+h);j++)(x=i&1?j:i-j,y=i-x)<h&x<w&&print(x,y)}

Try it online!

How?

We walk through the upper-left diagonals of a square of width \$w+h\$, going alternately from top-right to bottom-left and the other way around. We print only the coordinates that are within the target rectangle.

Below is an example for \$w=3\$ and \$h=2\$:

example

added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

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