# Introduction

A farmer needs help calculating the least time it will take him to pick his fruit each day.

# Challenge

• This farmer has X orchards.
• Each orchard has Y fruits in it. If the orchard has no fruits, then it will contain the string "none".
• The farmer has a list, this list contains the fruit he must pick.
• The farmer will only go down the list in order
• You must calculate how long it will take the farmer to pick his fruit on each day.

### More on the orchards

• All of the orchards are in a line.
• Each orchard is exactly 1 unit away from the next and previous one.
• The farmer can go up and down the line, but may not jump from one orchard to another

# Input and Output

You will receive an input in the following format:

X
*string*
*string*
*string* *string* *string* *string*
*string*
//ect.
Y
*string* *string*
*string* *string*
*string* *string*
*string* *string*
//ect.


X is the number of orchards

• Everything after X and before Y is an orchard containing a/some string(s), each string is a different fruit in that orchard.

Y is the number of days that the farmer must gather fruit.

• Each day consists of two strings that are different fruits.
• You must find what orchard these strings are in and calculate the difference.

### Input Rules:

1. Each fruit name string will be one word with no spaces

### Real Example

Still confused? Maybe this will clear it up:

Input

6

none

apple

orange pear pear

none

orange lemon pumpkin

pumpkin lettuce flowers peas

4

peas lettuce

apple orange

apple pumpkin

flowers orange


output: [ 0, 1, 3, 1 ]

### Explanation

Input:

• 6 the number of orchards
• A set of 6 orchards containing fruit, each orchard on a new line.
• 4 the number of days on the farmers list.
• A set of 4 fruits to compare, each pair of fruits is on a new line.

Output:

• Output an array of the differences between each set of fruits.
• The difference between peas and lettuce is 0, because they are in the same orchard.
• The difference between apples and oranges is 1 because they are one orchard apart.
• The difference between apples and pumpkins is 3 Because they are three orchards apart.
• The difference between flowers and oranges is 1 because they are one orchard apart.

Annotated input/output

6 orchards

a none

b apple

c orange pear pear

d none

e orange lemon pumpkin

f pumpkin lettuce flowers peas

--

4 fruits

peas lettuce 0

apple orange 1

apple pumpkin 3

flower orange 1

--

output: [ 0, 1, 3, 1 ]


# How To Win

Shortest code in bytes wins.

# Disclamer

I submitted this question to the sandbox, but I did not get any feedback on it, so feel free to edit as you see needed.

• -1 No objective winning criteria – anna328p Jun 19 '17 at 2:53
• Good thing this went through the sandbox so this user could be informed about this before they lost rep for it. /S – ATaco Jun 19 '17 at 2:54
• Sorry about the confusion. This is now code-golf – zoecarver Jun 19 '17 at 3:00
• If I was to go the function approach, would the input be one big string which has lines separated by newlines? or would the input be the lines as a string array/list? Also, can a fruit name contain numbers? or just letters? – Cameron Aavik Jun 19 '17 at 4:48
• Your explanation of the challenge is really confusing. I was able to piece together what was being asked by the example test case, but users should be clear on the challenge just from the spec. I would recommend rewriting the task. I think one of the more confusing parts is the use of X and Y to refer to multiple different things, however I'm not entirely sure if this is even the case. – Wheat Wizard Jun 26 '17 at 3:10

# 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:]
]


# 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"

# 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 */
/* Split all lines to arrays */
splitMany|
/* Enumerate, ie. give each line a number */
enum
]
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.

# Java 10, 307298288280 273 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(Math.abs(i-j),d):d;return r;}


-17 bytes thanks to @ceilingcat.

Explanation:

Try it here.

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:
Math.abs(i-j),
//         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