Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 35 bytes

JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

###Commented

Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()

#JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()

JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()
update tio link
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

#JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()

#JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()

#JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()
saved 1 byte
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

#JavaScript (ES6), 3635 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|a[v=>v<0|v!=i&&v]==i=i&a[v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    a[              //   test the tunnel:
      v != i &&     //     if v is equal to i, we test a[false] which is undefined
            &        //    make andsure thereforethat guaranteedthis notcell tois benot equalpointing to iitself
      v    a[v] == i       //     otherwise, we test a[v]check (the other end of the tunnel)
    ] == i          //   check that the above result is equal to i
  )                 // end of every()

#JavaScript (ES6), 36 bytes

a=>a.every((v,i)=>v<0|a[v!=i&&v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    a[              //   test the tunnel:
      v != i &&     //     if v is equal to i, we test a[false] which is undefined
                    //     and therefore guaranteed not to be equal to i
      v             //     otherwise, we test a[v] (the other end of the tunnel)
    ] == i          //   check that the above result is equal to i
  )                 // end of every()

#JavaScript (ES6), 35 bytes

Saved 1 byte thanks to @Shaggy

a=>a.every((v,i)=>v<0|v!=i&a[v]==i)

Try it online!

###Commented

a =>                // a[] = input array
  a.every((v, i) => // for each value v at position i in a[]:
    v < 0 |         //   force the test to succeed if v is negative (non-tunnel position)
    v != i &        //   make sure that this cell is not pointing to itself
    a[v] == i       //   check the other end of the tunnel
  )                 // end of every()
added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading