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

Fix input `//x` old code has extra space
Source Link
l4m2
  • 32.4k
  • 2
  • 26
  • 115
f=(s,x=X=W=H='',y=Y=0,d=1,D=0D=q=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>c=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&q&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][[q=o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online! Try it online!

// Somehow outdated
/// followed by manual comment, // by AI comment, // <s> AI's incorrect comment
f = (
 s, // `s` is the input string or array to process.
 x = X = W = H = '', // `x` and `X` track horizontal positions, initialized as empty strings.
 /// which is treated as zero in `x` and `X`, string for counter in `W` and `H`
 y = Y = 0, // `y` and `Y` track vertical positions, while `H` acts as the first "row" with a default 0.
 d = 1, // `d` represents horizontal direction (1 for right, -1 for left).
 D = 0, // `D` represents vertical direction (1 for down, -1 for up).
 o = [...H] // `o` is a 2D array derived from `H`. Mapping the string `H` into an array with sub-arrays.
 .map(_ => [...W]) // For every element `H`, create rows initialized with the current width `W`.
 /// `W` would contain only spaces, so `o` is full of spaces, as empty canvas.
) => 
 s.some((c, i) => // Begin processing the input array `s` via `.some` for iteration with conditions.
 ( // For each character `c` in `s`, compute its movement based on the character and update variables.
 [d, D] = // Update direction or positions based on the character.
 c == '\\' ? [-D, -d] // A backslash `\` inverts directions.
 /// also swaps, so it works as mirror. Same below.
 : c == '/' ? [D, d] // A forward slash `/` swaps directions.
 : [d, D, // Otherwise, maintain position and attempt movement.
 i && (x -= d, y -= D) // For all iterations (except the first), decrement by `d` and `D`.
 /// Use `-=` to handle empty string(initial value) well
 ],
 // Handle boundaries and updates based on row and column positions.
 y < 0 ? ++Y // If beyond the top, <s> extend a new row at the top.
 /// then would restart but initial Y increases
 : H[y] /// If inside
 ? x > 0 // If within a row:
 ? --X // <s> Shift left in the current row.
 /// then would restart but initial X increases
 : W[-x] // <s> If at/near a boundary:
 /// If inside
 ? ![o[y][-x] = c] // Place character `c` into the 2D grid `o`.
 /// and return false (other operations modifying initial condition return true)
 : W += ' ' // Otherwise, grow the row's width by one.
 : H += 0 // If beyond the existing rows, <s> add a new "0" row.
 /// add a row, content irrelevant
 )[3] // Process the last element of the computed array.
 ) /// aka. is it a restart request
 ? f(s, X, Y) // If `.some` returns `true` (i.e., a condition matches), recurse with updated values.
 : o // When all input has been processed, return the final 2D array `o`.
f=(s,x=X=W=H='',y=Y=0,d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

/// followed by manual comment, // by AI comment, // <s> AI's incorrect comment
f = (
 s, // `s` is the input string or array to process.
 x = X = W = H = '', // `x` and `X` track horizontal positions, initialized as empty strings.
 /// which is treated as zero in `x` and `X`, string for counter in `W` and `H`
 y = Y = 0, // `y` and `Y` track vertical positions, while `H` acts as the first "row" with a default 0.
 d = 1, // `d` represents horizontal direction (1 for right, -1 for left).
 D = 0, // `D` represents vertical direction (1 for down, -1 for up).
 o = [...H] // `o` is a 2D array derived from `H`. Mapping the string `H` into an array with sub-arrays.
 .map(_ => [...W]) // For every element `H`, create rows initialized with the current width `W`.
 /// `W` would contain only spaces, so `o` is full of spaces, as empty canvas.
) => 
 s.some((c, i) => // Begin processing the input array `s` via `.some` for iteration with conditions.
 ( // For each character `c` in `s`, compute its movement based on the character and update variables.
 [d, D] = // Update direction or positions based on the character.
 c == '\\' ? [-D, -d] // A backslash `\` inverts directions.
 /// also swaps, so it works as mirror. Same below.
 : c == '/' ? [D, d] // A forward slash `/` swaps directions.
 : [d, D, // Otherwise, maintain position and attempt movement.
 i && (x -= d, y -= D) // For all iterations (except the first), decrement by `d` and `D`.
 /// Use `-=` to handle empty string(initial value) well
 ],
 // Handle boundaries and updates based on row and column positions.
 y < 0 ? ++Y // If beyond the top, <s> extend a new row at the top.
 /// then would restart but initial Y increases
 : H[y] /// If inside
 ? x > 0 // If within a row:
 ? --X // <s> Shift left in the current row.
 /// then would restart but initial X increases
 : W[-x] // <s> If at/near a boundary:
 /// If inside
 ? ![o[y][-x] = c] // Place character `c` into the 2D grid `o`.
 /// and return false (other operations modifying initial condition return true)
 : W += ' ' // Otherwise, grow the row's width by one.
 : H += 0 // If beyond the existing rows, <s> add a new "0" row.
 /// add a row, content irrelevant
 )[3] // Process the last element of the computed array.
 ) /// aka. is it a restart request
 ? f(s, X, Y) // If `.some` returns `true` (i.e., a condition matches), recurse with updated values.
 : o // When all input has been processed, return the final 2D array `o`.
f=(s,x=X=W=H='',y=Y=0,d=1,D=q=0,o=[...H].map(_=>[...W]))=>s.some(c=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,q&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![q=o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

// Somehow outdated
/// followed by manual comment, // by AI comment, // <s> AI's incorrect comment
f = (
 s, // `s` is the input string or array to process.
 x = X = W = H = '', // `x` and `X` track horizontal positions, initialized as empty strings.
 /// which is treated as zero in `x` and `X`, string for counter in `W` and `H`
 y = Y = 0, // `y` and `Y` track vertical positions, while `H` acts as the first "row" with a default 0.
 d = 1, // `d` represents horizontal direction (1 for right, -1 for left).
 D = 0, // `D` represents vertical direction (1 for down, -1 for up).
 o = [...H] // `o` is a 2D array derived from `H`. Mapping the string `H` into an array with sub-arrays.
 .map(_ => [...W]) // For every element `H`, create rows initialized with the current width `W`.
 /// `W` would contain only spaces, so `o` is full of spaces, as empty canvas.
) => 
 s.some((c, i) => // Begin processing the input array `s` via `.some` for iteration with conditions.
 ( // For each character `c` in `s`, compute its movement based on the character and update variables.
 [d, D] = // Update direction or positions based on the character.
 c == '\\' ? [-D, -d] // A backslash `\` inverts directions.
 /// also swaps, so it works as mirror. Same below.
 : c == '/' ? [D, d] // A forward slash `/` swaps directions.
 : [d, D, // Otherwise, maintain position and attempt movement.
 i && (x -= d, y -= D) // For all iterations (except the first), decrement by `d` and `D`.
 /// Use `-=` to handle empty string(initial value) well
 ],
 // Handle boundaries and updates based on row and column positions.
 y < 0 ? ++Y // If beyond the top, <s> extend a new row at the top.
 /// then would restart but initial Y increases
 : H[y] /// If inside
 ? x > 0 // If within a row:
 ? --X // <s> Shift left in the current row.
 /// then would restart but initial X increases
 : W[-x] // <s> If at/near a boundary:
 /// If inside
 ? ![o[y][-x] = c] // Place character `c` into the 2D grid `o`.
 /// and return false (other operations modifying initial condition return true)
 : W += ' ' // Otherwise, grow the row's width by one.
 : H += 0 // If beyond the existing rows, <s> add a new "0" row.
 /// add a row, content irrelevant
 )[3] // Process the last element of the computed array.
 ) /// aka. is it a restart request
 ? f(s, X, Y) // If `.some` returns `true` (i.e., a condition matches), recurse with updated values.
 : o // When all input has been processed, return the final 2D array `o`.
added 3563 characters in body
Source Link
l4m2
  • 32.4k
  • 2
  • 26
  • 115

JavaScript (Node.js), 197195 bytes

f=(s,x=X=W=''x=X=W=H='',y=Y=H='0'y=Y=0,d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online! Try it online!

/// followed by manual comment, // by AI comment, // <s> AI's incorrect comment
f = (
 s, // `s` is the input string or array to process.
 x = X = W = H = '', // `x` and `X` track horizontal positions, initialized as empty strings.
 /// which is treated as zero in `x` and `X`, string for counter in `W` and `H`
 y = Y = 0, // `y` and `Y` track vertical positions, while `H` acts as the first "row" with a default 0.
 d = 1, // `d` represents horizontal direction (1 for right, -1 for left).
 D = 0, // `D` represents vertical direction (1 for down, -1 for up).
 o = [...H] // `o` is a 2D array derived from `H`. Mapping the string `H` into an array with sub-arrays.
 .map(_ => [...W]) // For every element `H`, create rows initialized with the current width `W`.
 /// `W` would contain only spaces, so `o` is full of spaces, as empty canvas.
) => 
 s.some((c, i) => // Begin processing the input array `s` via `.some` for iteration with conditions.
 ( // For each character `c` in `s`, compute its movement based on the character and update variables.
 [d, D] = // Update direction or positions based on the character.
 c == '\\' ? [-D, -d] // A backslash `\` inverts directions.
 /// also swaps, so it works as mirror. Same below.
 : c == '/' ? [D, d] // A forward slash `/` swaps directions.
 : [d, D, // Otherwise, maintain position and attempt movement.
 i && (x -= d, y -= D) // For all iterations (except the first), decrement by `d` and `D`.
 /// Use `-=` to handle empty string(initial value) well
 ],
 // Handle boundaries and updates based on row and column positions.
 y < 0 ? ++Y // If beyond the top, <s> extend a new row at the top.
 /// then would restart but initial Y increases
 : H[y] /// If inside
 ? x > 0 // If within a row:
 ? --X // <s> Shift left in the current row.
 /// then would restart but initial X increases
 : W[-x] // <s> If at/near a boundary:
 /// If inside
 ? ![o[y][-x] = c] // Place character `c` into the 2D grid `o`.
 /// and return false (other operations modifying initial condition return true)
 : W += ' ' // Otherwise, grow the row's width by one.
 : H += 0 // If beyond the existing rows, <s> add a new "0" row.
 /// add a row, content irrelevant
 )[3] // Process the last element of the computed array.
 ) /// aka. is it a restart request
 ? f(s, X, Y) // If `.some` returns `true` (i.e., a condition matches), recurse with updated values.
 : o // When all input has been processed, return the final 2D array `o`.

JavaScript (Node.js), 197 bytes

f=(s,x=X=W='',y=Y=H='0',d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

JavaScript (Node.js), 195 bytes

f=(s,x=X=W=H='',y=Y=0,d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

/// followed by manual comment, // by AI comment, // <s> AI's incorrect comment
f = (
 s, // `s` is the input string or array to process.
 x = X = W = H = '', // `x` and `X` track horizontal positions, initialized as empty strings.
 /// which is treated as zero in `x` and `X`, string for counter in `W` and `H`
 y = Y = 0, // `y` and `Y` track vertical positions, while `H` acts as the first "row" with a default 0.
 d = 1, // `d` represents horizontal direction (1 for right, -1 for left).
 D = 0, // `D` represents vertical direction (1 for down, -1 for up).
 o = [...H] // `o` is a 2D array derived from `H`. Mapping the string `H` into an array with sub-arrays.
 .map(_ => [...W]) // For every element `H`, create rows initialized with the current width `W`.
 /// `W` would contain only spaces, so `o` is full of spaces, as empty canvas.
) => 
 s.some((c, i) => // Begin processing the input array `s` via `.some` for iteration with conditions.
 ( // For each character `c` in `s`, compute its movement based on the character and update variables.
 [d, D] = // Update direction or positions based on the character.
 c == '\\' ? [-D, -d] // A backslash `\` inverts directions.
 /// also swaps, so it works as mirror. Same below.
 : c == '/' ? [D, d] // A forward slash `/` swaps directions.
 : [d, D, // Otherwise, maintain position and attempt movement.
 i && (x -= d, y -= D) // For all iterations (except the first), decrement by `d` and `D`.
 /// Use `-=` to handle empty string(initial value) well
 ],
 // Handle boundaries and updates based on row and column positions.
 y < 0 ? ++Y // If beyond the top, <s> extend a new row at the top.
 /// then would restart but initial Y increases
 : H[y] /// If inside
 ? x > 0 // If within a row:
 ? --X // <s> Shift left in the current row.
 /// then would restart but initial X increases
 : W[-x] // <s> If at/near a boundary:
 /// If inside
 ? ![o[y][-x] = c] // Place character `c` into the 2D grid `o`.
 /// and return false (other operations modifying initial condition return true)
 : W += ' ' // Otherwise, grow the row's width by one.
 : H += 0 // If beyond the existing rows, <s> add a new "0" row.
 /// add a row, content irrelevant
 )[3] // Process the last element of the computed array.
 ) /// aka. is it a restart request
 ? f(s, X, Y) // If `.some` returns `true` (i.e., a condition matches), recurse with updated values.
 : o // When all input has been processed, return the final 2D array `o`.
added 115 characters in body
Source Link
l4m2
  • 32.4k
  • 2
  • 26
  • 115

JavaScript (Node.js), 198197 bytes

f=(s,x=X=W='',y=Y=H='0',d=-1d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='/'D]=c=='\\'?[-D,-d]:c=='\\'c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x<0x>0?++X--X:W[+x]W[-x]?![o[y][+x]=c][o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online! Try it online!

Lots of work on exacting boundary

Note that x is negated, as it shortens 1 bytes for d, and all use of x need explicit convert into number

JavaScript (Node.js), 198 bytes

f=(s,x=X=W='',y=Y=H='0',d=-1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='/'?[-D,-d]:c=='\\'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x<0?++X:W[+x]?![o[y][+x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

Lots of work on exacting boundary

JavaScript (Node.js), 197 bytes

f=(s,x=X=W='',y=Y=H='0',d=1,D=0,o=[...H].map(_=>[...W]))=>s.some((c,i)=>([d,D]=c=='\\'?[-D,-d]:c=='/'?[D,d]:[d,D,i&&(x-=d,y-=D),y<0?++Y:H[y]?x>0?--X:W[-x]?![o[y][-x]=c]:W+=' ':H+=0])[3])?f(s,X,Y):o

Try it online!

Lots of work on exacting boundary

Note that x is negated, as it shortens 1 bytes for d, and all use of x need explicit convert into number

Source Link
l4m2
  • 32.4k
  • 2
  • 26
  • 115
Loading

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