Skip to main content
added 200 characters in body
Source Link
Xwtek
  • 2k
  • 11
  • 28

Haskell, 173 166 bytes, 159 bytes in GHCi

Here is the normal version:

import Data.List

t=length
j[_]=0
j l=y[t f|a<f|f<-Data.Listfst.permutations[0span(>0)<$>permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Here is the GHCi answer (put the line one at a time):

t=length
y[]=0-1;y l=minimum l+1
j[_]=0;j l=y[t f|f<-fst.span(>0)<$>Data.List.permutations[0..t l-1],u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Haskell, 173 166 bytes

t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Haskell, 173 166 bytes, 159 bytes in GHCi

Here is the normal version:

import Data.List

t=length
j[_]=0
j l=y[t f|f<-fst.span(>0)<$>permutations[0..t l-1],u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Here is the GHCi answer (put the line one at a time):

t=length
y[]=0-1;y l=minimum l+1
j[_]=0;j l=y[t f|f<-fst.span(>0)<$>Data.List.permutations[0..t l-1],u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

added 86 characters in body
Source Link
Xwtek
  • 2k
  • 11
  • 28

Haskell, 173 165166 bytes

t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Haskell, 173 165 bytes

t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Haskell, 173 166 bytes

t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

added 86 characters in body
Source Link
Xwtek
  • 2k
  • 11
  • 28

Haskell, 173173 165 bytes

import Data.List
t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Haskell, 173 bytes

import Data.List
t=length
j[_]=0
j l=y[t f|a<-permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

Haskell, 173 165 bytes

t=length
j[_]=0
j l=y[t f|a<-Data.List.permutations[0..t l-1],let f=takeWhile(/=0)a,u<-f,u==t l-1,all(\(a,b)->abs(b-a)<=l!!a)$zip(0:f)$f++[0]]
y[]=0-1
y l=minimum l+1

Just a bruteforce. Generate the possible answer. (i.e. permutation of [0..n-1] with zero and following element dropped. Then check if the answer is correct. Get the minimum length and add by one. (Since the leading and trailing zeroes is delete).

How to use: j[3,4,0,0,6] -> 3

It turns out that I don't have to import Data.List if I just want to use 1 function

Fixed, the original byte length as it reflects old and longer one. and added explanation
Source Link
Xwtek
  • 2k
  • 11
  • 28
Loading
Source Link
Xwtek
  • 2k
  • 11
  • 28
Loading