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 15 characters in body
Source Link
ngm
  • 4.4k
  • 9
  • 29

R, (削除) 125 111 (削除ここまで) 111108 bytes

function(x,i,m=array(seq(1:prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1)o[p[1]+0:2,p[2]+0:2][-5]

Try it online! Try it online!

14 and 8 bytes golfed by @JayCe and @Mark.

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

R, (削除) 125 (削除ここまで) 111 bytes

function(x,i,m=array(seq(prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1)o[p[1]+0:2,p[2]+0:2][-5]

Try it online!

14 bytes golfed by @JayCe

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

R, (削除) 125 111 (削除ここまで) 108 bytes

function(x,i,m=array(1:prod(x),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1)o[p[1]+0:2,p[2]+0:2][-5]

Try it online!

14 and 8 bytes golfed by @JayCe and @Mark.

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

added 5 characters in body
Source Link
ngm
  • 4.4k
  • 9
  • 29

R, 125(削除) 125 (削除ここまで) 111 bytes

function(x,i,m=array(seq(prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1,r=o[p[1]:(p[1]+2)o[p[1]+0:2,p[2]p[2]+0:(p[2]+2)])r[2][-5]

Try it online! Try it online!

1-based.14 bytes golfed by @JayCe

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

R, 125 bytes

function(x,i,m=array(seq(prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1,r=o[p[1]:(p[1]+2),p[2]:(p[2]+2)])r[-5]

Try it online!

1-based.

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

R, (削除) 125 (削除ここまで) 111 bytes

function(x,i,m=array(seq(prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1)o[p[1]+0:2,p[2]+0:2][-5]

Try it online!

14 bytes golfed by @JayCe

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

Source Link
ngm
  • 4.4k
  • 9
  • 29

R, 125 bytes

function(x,i,m=array(seq(prod(x)),x),n=rbind(m,m,m),o=cbind(n,n,n),p=which(m==i,T)+x-1,r=o[p[1]:(p[1]+2),p[2]:(p[2]+2)])r[-5]

Try it online!

1-based.

Input is [w, h], i because R populates arrays column first.

Makes the array and then "triples" it row- and column-wise. Then locate i in the original array and find it's neighborhood. Output without i.

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