Skip to main content
deleted 902 characters in body
Source Link
ngm
  • 4.3k
  • 7
  • 29

R, 117 63 6359 bytes

@Giuseppe with most of the bytes saved:

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5<2)<2)

Try it online!Try it online!

dist computes distances (default is Euclidean) among rows of a matrix. which with second argument TRUE returns the coordinates where the predicate is true. 

Coordinates are neighbours if the distance between them is not more than the square root of 2, but the inner <2 is good enough because the possible distance jumps from sqrt(2) ro 2.

Original:

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

R, 117 63 bytes

@Giuseppe with most of the bytes saved:

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5)<2)

Try it online!

dist computes distances (default is Euclidean) among rows of a matrix. which with second argument TRUE returns the coordinates where the predicate is true. Coordinates are neighbours if the distance between them is not more than the square root of 2.

Original:

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

R, 117 63 59 bytes

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<2)<2)

Try it online!

dist computes distances (default is Euclidean) among rows of a matrix. which with second argument TRUE returns the coordinates where the predicate is true. 

Coordinates are neighbours if the distance between them is not more than the square root of 2, but the inner <2 is good enough because the possible distance jumps from sqrt(2) ro 2.

added 316 characters in body
Source Link
ngm
  • 4.3k
  • 7
  • 29

R, 117 63 bytes

@Giuseppe with most of the bytes saved:

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5)<2)

Try it online!

dist computes distances (default is Euclidean) among rows of a matrix. which with second argument TRUE returns the coordinates where the predicate is true. Coordinates are neighbours if the distance between them is not more than the square root of 2.

Original:

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

R, 117 63 bytes

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5)<2)

Try it online!

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

R, 117 63 bytes

@Giuseppe with most of the bytes saved:

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5)<2)

Try it online!

dist computes distances (default is Euclidean) among rows of a matrix. which with second argument TRUE returns the coordinates where the predicate is true. Coordinates are neighbours if the distance between them is not more than the square root of 2.

Original:

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

added 402 characters in body
Source Link
ngm
  • 4.3k
  • 7
  • 29

R, 117117 63 bytes

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrowcolSums(p)),functionas.matrix(y)sumdist(applywhich(p,1m==8,function(x)all((c(y-x)^2<2)T)))<=2^.5)==1<2)

Try it online!

A not-that-great explanation:Try it online!

f <- function(m) {
  p <- which,p=which(m == 8m==8, T)    # Matrix with rows of indices where 8 appears
  )sum(
    sapply(
      split(p, 1:nrow(p)), # Split p into a list of its rows
      function(y) {
        # 1 if no neighboring 8s, 0 otherwise
        sum(apply(p, 1, function(x) all((c(y - x)^2 < 2^2<2)))) == 1
      }
    )
  ==1)    
}

Try it online!

R, 117 bytes

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

A not-that-great explanation:

f <- function(m) {
  p <- which(m == 8, T)    # Matrix with rows of indices where 8 appears
  sum(
    sapply(
      split(p, 1:nrow(p)), # Split p into a list of its rows
      function(y) {
        # 1 if no neighboring 8s, 0 otherwise
        sum(apply(p, 1, function(x) all((c(y - x)^2 < 2)))) == 1
      }
    )
  )    
}

R, 117 63 bytes

function(m)sum(colSums(as.matrix(dist(which(m==8,T)))<=2^.5)<2)

Try it online!

function(m,p=which(m==8,T))sum(sapply(split(p,1:nrow(p)),function(y)sum(apply(p,1,function(x)all((c(y-x)^2<2)))))==1)

Try it online!

Source Link
ngm
  • 4.3k
  • 7
  • 29
Loading