7
\$\begingroup\$

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.

Rules

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.

\$\endgroup\$
  • \$\begingroup\$ -1 No objective winning criteria \$\endgroup\$ – dkudriavtsev Jun 19 '17 at 2:53
  • 14
    \$\begingroup\$ Good thing this went through the sandbox so this user could be informed about this before they lost rep for it. /S \$\endgroup\$ – ATaco Jun 19 '17 at 2:54
  • 2
    \$\begingroup\$ Sorry about the confusion. This is now code-golf \$\endgroup\$ – zoecarver Jun 19 '17 at 3:00
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Cameron Aavik Jun 19 '17 at 4:48
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Sriotchilism O'Zaic Jun 26 '17 at 3:10
2
\$\begingroup\$

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:]
    ]
\$\endgroup\$
0
\$\begingroup\$

Java 8, 307 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=0,d,i,j;for(String p:f)if(!p.isEmpty()){for(d=o.length,i=1;i<d;i++)if(o[i].contains(p.split(" ")[0]))for(j=1;j<o.length;j++)if(o[j].contains(p.split(" ")[1]))d=j-i>0?j-i<d?j-i:d:i-j<d?i-j:d;r[q++]=d;}return r;}

I'm almost ashamed to post this, because I have the feeling this can be golfed by a lot considering the three nested for-loops, but this will do for now..

Explanation:

Try it here.

s->{                               // Method with String parameter and integer-array return-type
  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
  int r[]=new int[f.length-1],     //  Result integer-array
      q=0,d,i,j;                   //  Some temp integers
  for(String p:f)                  //  Loop (1) over the pairs:
    if(!p.isEmpty()){              //   If the pair isn't empty (first item is empty after `.split("\\d")`)
      for(d=o.length,i=1;i<d;i++)  //    Inner loop (2) over the orchards:
        if(o[i].contains(p.split(" ")[0]))
                                   //     If the current orchard contains the first fruit of the pair:
          for(j=1;j<o.length;j++)  //      Inner loop (3) over the orchards again:
            if(o[j].contains(p.split(" ")[1]))
                                   //       If the current orchard contains the second fruit of the pair:
              d=j-i>0?             //        If j-i is larger than 0:
                 j-i<d?            //         If j-i is smaller than `d`:
                  j-i              //          Set `d` to j-i
                 :                 //         Else:
                  d                //          `d` remains the same
                :                  //        Else (i-j is larger than or equal to 0):
                 i-j<d?            //         If i-j is smaller than `d`:
                  i-j              //          Set `d` to i-j
                 :                 //         Else:
                  d;               //          `d` remains the same
                                   //      End of inner loop (3) (implicit / single-line body)
                                   //    End of inner loop (2) (implicit / single-line body)
      r[q++]=d;                    //    Add `d` to the result array
    }
                                   //  End of loop (1) (implicit / single-line body)
  return r;                        //  Return the result array
}                                  // End of method
\$\endgroup\$
0
\$\begingroup\$

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"

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.