Traveling Pumpkin Problem
Jelly, 30 29 27 25 bytes
_AṢæ..
0,0ṭṚç2\+\<S
Œ!ç€Ṁ
Try it online!
Apparently Jelly's dot product just ignores a list size mismatch and doesn't multiply the extra elements of the other array, just adds them. Shaves off 2 bytes.
Explanation
_AṢæ.. Helper link to calculate distance. Arguments: a, b
_ subtract the vertices from each other
A take absolute values of axes
Ṣ sort the axes
æ.. dot product with [0.5]
0,0ṭṚç2\+\<S Helper link to calculate max cities. Arguments: perm, max
0,0 create pair [0,0]
ṭ append that to the permutation
Ṛ reverse the permutation (gets the [0,0] to the beginning)
ç2\ find distances of each pair using the previous link
+\ find all partial sums
< see if each sum was less than the max
S sum to count cases where it was
Œ!ç€Ṁ Main link. Arguments: cities, max
Œ! get permutations of cities
ç€ find max cities for each permutation using the previous link
Ṁ take the maximum
Java 7, 206 201 bytes
Thanks to @KevinCruijssen for saving 5 bytes
int f(float e,int[]a,int[]b){int x=0,y=0,c=0,d=0,t;float s;for(int i:a){s=(i!=x&b[c]==y)|(i==x&b[c]!=y)?Math.sqrt((t=i-x)*t+(t=b[c]-y)*t)*1:Math.abs(i-x)*1.5;d+=e-s>=0?1:0;e-=s;x=i;y=b[c++];}return d;}
Ungolfed
class Travellingpumpkin {
public static void main(String[] args) {
System.out.println(f( 5 ,new int[] { 1,2,3,4,5,5 } , new int[] { 1,1,1,1,0,1 } ));
}
static int f( double e , int[]a , int[]b ) {
int x = 0 , y = 0 , c = 0 , d = 0 , t;
double s ;
for ( int i : a ) {
s = ( i != x & b[c] == y )|( i == x & b[c] != y )
? Math.sqrt( ( t = i - x ) * t + ( t = b[c] - y ) * t ) * 1
: Math.abs( i - x ) * 1.5 ;
d += e-s >= 0 ? 1 : 0 ;
e -= s ;
x = i ; y = b [ c++ ] ;
}
return d ;
}
}
Scala, 196 bytes
def f(l:Int,c:(Int,Int)*)=c.permutations.map(x=>((0,0)+:x sliding 2 map{p=>val Seq(c,d)=Seq((p(0)._1-p(1)._1)abs,(p(0)._2-p(1)._2)abs).sorted
c*1.5+(d-c)}scanLeft 0d)(_+_)takeWhile(_<l)size).max-1
Ungolfed:
def g (l: Int, c: (Int, Int)*) = {
c.permutations
.map { x =>
((0, 0) +: x).sliding(2).map({ p =>
val Seq(c, d) = Seq((p(0)._1 - p(1)._1) abs, (p(0)._2 - p(1)._2) abs).sorted
c * 1.5 + (d - c)
}).scanLeft(0d)(_ + _).takeWhile(_ < l).size
}.max - 1
}
Explanantion:
def f(l:Int,c:(Int,Int)*)= //defien a function with an int and a vararg-int-pait parameter
c.permutations //get the permutations of c, that is all possible routes
.map(x=> //map each of them to...
((0,0)+:x //prepend (0,0)
sliding 2 //convert to a sequence of consecutive elemtens
map{p=> //and map each of them to their distance:
val Seq(c,d)=Seq( //create a sequence of
(p(0)._1-p(1)._1)abs, //of the absolute distance between the x points
(p(0)._2-p(1)._2)abs //and he absolute distance between the y coordinates
).sorted //sort them and assign the smaller one to c and the larger one to d
c*1.5+(d-c) //we do the minimum difference diagonally
} //we now have a sequence of sequence of the distances for each route
scanLeft 0d)(_+_) //calculate the cumulative sum
takeWhile(_<l) //and drop all elements that are larger than the candle lifespan
size //take the size
).max-1 //take the maximum, taht is the size of the largest route and subtract 1 because we added (0,0) at the beginning