15
\$\begingroup\$

Inspired by this. No avocados were harmed in the making of this challenge.

Hello I have challenge I need help juic an avocado so I need program to tell me how long to juic avocad for

Observe this ASCII art avocado:

    ###### 
   #      #
   # #### #
  #  # p# #
  ## #### #
   #      #
    ######

This avocado consists of an exterior of #s (specifically the first and last sequences of #s on each line) and a pit (a shape of #s in the avocado that does not touch the avocado exterior).

Through rigorous experiments on these ASCII art avocados I have discovered the following:

avocado juice in fluid ounces = number of spaces inside avocado but outside pit (the pit is marked with a p in the example) + 2 * number of spaces inside pit

time to juice avocado in minutes = 13 * number of spaces inside pit

For example, this avocado will take 26 (2 spaces inside pit * 13) minutes to juice and will give 23 (19 spaces inside avocado but outside pit + 2 * 2 spaces inside pit) fl oz of juice.

Challenge

Given an input of exactly one ASCII art avocado such as the one above that consists of only # and whitespace, output the amount of time in minutes it will take to juice it and the amount of juice it will produce in any order.

You may assume that the input avocado will always have exactly one pit and both the avocado and the pit will always be closed. The pit and avocado will always be connected, and any subset of the pit will be connected as well. The avocado and the pit will always be convex. Note that the avocado exterior may be arbitrarily thick.

Sample Inputs and Outputs

    ###### 
   #      #
   # #### #
  #  #  # # -> 26 23
  ## #### #
   #      #
    ######


   #######
  #       #
  #  ###   ##
  #  #  #   # -> 26 35
  #   ##   #
  #        #
  ##########

This is , so shortest code in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Possible duplicate of Are you in the biggest room? \$\endgroup\$ – Mego Feb 9 '16 at 22:31
  • 3
    \$\begingroup\$ @Mego I talked with people in chat and we decided it was sufficiently different due to the avocado pit. \$\endgroup\$ – a spaghetto Feb 9 '16 at 22:32
  • 3
    \$\begingroup\$ I still think it's a dupe. \$\endgroup\$ – Mego Feb 9 '16 at 22:33
  • 1
    \$\begingroup\$ @DigitalTrauma Fixed. \$\endgroup\$ – a spaghetto Feb 9 '16 at 23:09
  • 1
    \$\begingroup\$ Still seems quite unclear what the set of valid inputs is. \$\endgroup\$ – feersum Feb 10 '16 at 11:35
6
\$\begingroup\$

Pyth, 59 51 bytes

*Ksm/.s.s.sd\ \#\ \ fq4l:T"#+"4.z13+-/s.sR\ .zdK*2K

Try it here!

Outputs the time to juic the advacado (totally correct english) first and on the next line the amount of juic.

Explanation

Code - Overview

*Ksm/.s.s.sd\ \#\ \ fq4l:T"#+"4.z13+-/s.sR\ .zdK*2K # .z=list of all input lines

                    fq4l:T"#+"4.z                   # Get the pit-lines
   m/.s.s.sd\ \#\ \                                 # Map the pit-lines to the whitespace amount
 Ks                                                 # Sum the amount of pit-spaces and assign to K
*                                13                 # Print the juic time
                                     /s.sR\ .zd     # Count all whitespaces in the advacado
                                    -          K    # Subtract the pit size from it
                                   +            *2K # Do the Rest of the amount calcluation and print it


Detailed explanations of the size calculation parts see below.

Getting the advacado size

Let's look at this one:

    ###### 
   #      #
   # #### #
  #  #  # #
  ## #### #
   #      #
    ######

First the leading and trailing whitespaces get removed. After that we wrap everything in one line, which results in this string:

#######      ## #### ##  #  # ### #### ##      #######

This contains all whitespaces in the advacado, so we just have to count them (the advacado will always be convex, so this works for all valid inputs). This number still contains the spaces in the pit, but for the juic amount calculation we only need the spaces in the fruit without the pit-spaces. So we need to calculate them too.

The code for that explained in detail:

/s.sR\ .zd   # .z=list of all input lines

  .sR\ .z    # strip spaces from every input line
 s           # Concatenate all lines
/        d   # Count all spaces

Getting the pit size

This is a bit trickier. First we remove the lines which don't contribute to the pit size. This is done by filtering out all lines that have less than 4 groups of hashes (using the regex #+ and counting its matches). In the example above only one line will survive this process:

  #  #--# #

The spaces I marked with a - here are the ones we need to count. So we just strip spaces, then hashes and then spaces again which leaves us with this:

#  #

There we just have to count the spaces. We do all that for every line which survived the filtering process, sum everything and we are done. The rest is trivial math.

The code for that explained in detail:

sm/.s.s.sd\ \#\ \ fq4l:T"#+"4.z   # .z=list of all input lines

                  f          .z   # filter the input
                     l:T"#+"4     # length of the matches for the regex `#+`
                   q4             # if there are 4 groups of hashes its a pit line
 m                                # map the pit lines to...
  /             \                 # The occurences of spaces in..
   .s.s.sd\ \#\                   # ...the stripped pit line (see explanation above)
s                                 # Sum all amounts of spaces in the pits

\$\endgroup\$
5
\$\begingroup\$

Retina, 70

  • 25 bytes saved thanks to @FryAmTheEggman and @randomra
T` `i`(?<=# +#+) *(?=#+ +#)
T` `f`# +#
i
13$*iff
((i)|(f)|\W)+
$#2 $#3

Try it online.

\$\endgroup\$
  • 1
    \$\begingroup\$ Dunno if it'll help but I got 90 bytes using $*... still feels really golfable though... \$\endgroup\$ – FryAmTheEggman Feb 10 '16 at 14:52
  • 2
    \$\begingroup\$ @FryAmTheEggman Ohh, you can use literals with $*_? That's nice. I managed to get 70 bytes. \$\endgroup\$ – randomra Feb 10 '16 at 15:09
  • 1
    \$\begingroup\$ @randomra yeah, it will actually use any "token", and very nice! I had tried to come up with a similar scheme, but I kept getting stuck with having to do additional parsing, reusing f is very clever! It's too bad though that the right "argument" of $* can only be a character and not a token... maybe another kind of replacement for the future? :0 \$\endgroup\$ – FryAmTheEggman Feb 10 '16 at 15:15
  • \$\begingroup\$ @randomra very cool - thanks! \$\endgroup\$ – Digital Trauma Feb 11 '16 at 16:21
3
\$\begingroup\$

Python, 141 119 bytes

import sys
s=str.strip;l=len;o=i=0
for x in sys.stdin:x=s(s(x),'#');y=s(x);o+=l(x)-l(y);i+=l(s(y,'#'))
print o+2*i,13*i
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! If you define s with s=str.strip, the loop body can become x=s(s(x),'#');y=s(x);o+=l(x)-l(y);i+=l(s(y,'#')). Also, there's a non-functional space on the last line. \$\endgroup\$ – Dennis Feb 11 '16 at 17:40
  • \$\begingroup\$ ahh i didn't even know you could do that, thanks :) \$\endgroup\$ – mtp Feb 11 '16 at 17:43

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.