| Bytes | Lang | Time | Link |
|---|---|---|---|
| 271 | Java 10 | 170626T130538Z | Kevin Cr |
| 118 | Röda | 170628T110052Z | fergusq |
| 224 | JavaScript ES6 | 170627T171948Z | Ian |
| 003 | 176 bytes | 170619T140138Z | Cameron |
Java 10, 307 298 288 280 273 271 bytes
s->{String[]a=s.split("\\d"),o=a[1].split("\n"),f=a[2].split("\n");int r[]=new int[f.length-1],q=-2,d,i,j,z;for(var p:f)if(++q>=0)for(z=d=o.length,i=0;++i<d;)for(j=0;o[i].contains((f=p.split(" "))[0])&++j<z;)r[q]=d=o[j].contains(f[1])?Math.min(i>j?i-j:j-i,d):d;return r;}
-19 bytes thanks to @ceilingcat.
Explanation:
s->{ // Method with String parameter & integer-array return
String[]a=s.split("\\d"), // Split the input on numbers
o=a[1].split("\n"), // List of orchards
f=a[2].split("\n"); // List of pairs (later on reused for individual pair of fruits)
int r[]=new int[f.length-1], // Result integer-array
q=-2,d,i,j,z; // Some temp integers
for(var p:f) // Loop `p` over the pairs:
if(++q>=0){ // If this isn't the first iteration of the pair-loop
// (the first item is always empty after `.split("\\d")`)
for(z=d=o.length,i=0;++i<d;)
// Inner loop `i` over the orchards:
for(j=0;o[i].contains((f=p.split(" "))[0])
// If the current orchard contains the first fruit of the pair:
&++j<z;) // Inner loop `j` over the orchards again:
r[q]= // Add the following to the resulting array:
d=o[j].contains(f[1])?
// If the current orchard contains the second fruit of the pair:
Math.min( // Take the minimum of:
i>j?i-j:j-i,// The absolute difference between `i` and `j`
d) // And the current integer of the array
: // Else:
d; // Leave the current integer unchanged
return r;} // Return the result array
Röda, 118 bytes
{A=[head(parseInteger(pull()))|splitMany|enum]pull n;split|{f={|x|A|[_2]if[x in _1]}f a|{|i|f b|abs i-_}_|min}for a,b}
Explanation:
{
A=[
/* Pull line, convert to integer, and read that many lines */
head(parseInteger(pull()))|
/* Split all lines to arrays */
splitMany|
/* Enumerate, ie. give each line a number */
enum
]
/* Discard one line */
pull n;
/* Split each following line and push words to the stream */
split|
/* For each word a, b in the stream: */
{
/* Helper function to find lines that contain x */
f={|x|A|[_2]if[x in _1]}
/* Push numbers of lines that contain the first word to the stream */
f a|
/* For each line number i */
{|i|
/* Push numbers of lines that contain the second word to the stream */
f b|
/* Push the difference of two line numbers to the stream */
abs i-_
}_|
/* Push the smallest difference to the stream */
min
}for a,b
}
No TIO link yet, as the TIO version of Röda is outdated.
JavaScript (ES6), 224 bytes
a=>(d=a.split('\n')).slice(e=+d[0]+2).map(f=>{p=e;for(g=(q=(t,s=0)=>d.slice(1,e-1).map(b=c=>c.split(' ')).findIndex((x,y)=>y>=t&x.includes(b(f)[s])))(0);++g;g=q(g))for(l=q(0,1);++l;l=q(l,1))p=p<(u=g>l?g-l:l-g)?p:u;return p})
Call with "6\nnone\napple\norange pear pear\nnone\norange lemon pumpkin\npumpkin lettuce flowers peas\n4\npeas lettuce\napple orange\napple pumpkin\nflowers orange"
176 bytes, Python 3
def p(a):l=[b.split()for b in a.split('\n')];X=int(l[0][0]);return[min(y-x for y in range(X)for x in range(y+1)if z[0]in l[x+1]+l[y+1]and z[1]in l[x+1]+l[y+1])for z in l[X+2:]]
This can be called by passing in the entire content as a string
p("""6
none
apple
orange pear pear
none
orange lemon pumpkin
pumpkin lettuce flowers peas
4
peas lettuce
apple orange
apple pumpkin
flowers orange""")
Ungolfed solution
def problem(a):
# this will split on new line, and split each individual line into it's own list
lines=[b.split()for b in a.split('\n')]
# get the number of orchards
X=int(lines[0][0])
return[
# get the minimum of all possible differences
min(y-x # subtract y and x to get the difference
for y in range(X) # for each orchard, y
for x in range(y+1) # and each orchard up to y inclusive, x
if z[0] in lines[x+1]+lines[y+1] and z[1] in lines[x+1]+lines[y+1]) # if both fruits exist the x and y orchards
# for every day (z is a list containing 2 fruits as string)
for z in lines[X+2:]
]