20
\$\begingroup\$

Description

I guess everyone knows the fairy tale of Rapunzel and the prince. For those who do not: read it here. However, Rapunzel had just had her hair cut, so it might not be long enough to let her prince climb up! She may get very sad..

Challenge

Your task is to write a function that evaluates what Rapunzel will be saying when the prince calls for her to let her hair down: When her hair is longer than or equal to the tower is tall plus an extra meter (safety guidelines), she becomes veeeery happy and says Aaaah!, with the number of as being the same as length of her hair - height of the tower. Otherwise, her hair does not have sufficient length, and she starts crying: Booho!, where the os before the h equal two thirds of height of the tower - length of her hair, and the os after the h being the rest. The number of os after the B must be rounded, so if you get 2.6, there will be 3 os, and the others must be after the h.

I/O

You are given positive integers (including null) as arguments, as a list or as two single numbers, in the order you find it the most convenient, but you must state in which order you take them. As the output, print what Rapunzel will be saying.

Test cases

In the test cases, the first number will be the hair length.

0, 0 -> 'Bh!' (probably a dry sob..)
2, 1 -> 'Aah!'
1, 2 -> 'Boh!'
1, 4 -> 'Booho!'
4, 1 -> 'Aaaah!'
2, 4 -> 'Boho!'

This is , so the shortest answer in bytes wins!

\$\endgroup\$
  • 5
    \$\begingroup\$ So she can't get down if the height of the tower is 0? BTW, positive integers (including null) == _non-negative integers :) \$\endgroup\$ – Stewie Griffin Jul 15 '17 at 19:20
  • 2
    \$\begingroup\$ I meant to do this on your first post, but I would like to tell you about the Sandbox, where you can post challenges to get feedback before posting them to main. This seems like quite an interesting challenge but people can get carried away. \$\endgroup\$ – caird coinheringaahing Jul 15 '17 at 20:14
  • 4
    \$\begingroup\$ @StewieGriffin Yep, assuming that her hair length is 0 too. Still, she has to get out of the window, and the restrictions for safety apply here too! There's no way she can marry him if she's got herself a broken neck. \$\endgroup\$ – racer290 Jul 16 '17 at 4:38

11 Answers 11

5
\$\begingroup\$

Jelly,  43 41 40 38 34 33 32  31 bytes

There's probably a much Is there a shorter way though! ? ...this was quite some golfing!

‘:3;ạ¥⁸4ẋ+;€⁹¦7Ṛṭ;8ị“øŻPLC»
ạç>

A full program printing the result*.

Try it online!

How?

‘:3;ạ¥⁸4ẋ+;€⁹¦7Ṛṭ;8ị“øŻPLC» - Link 1: number, abs(hair-tower); number, (hair > tower)?
‘                           - increment -> abs(hair-tower)+1
 :3                         - integer divide by 3 -> (abs(hair-tower)+1)//3
                            -   ...the remainder amount after removing 2/3 rounded
      ⁸                     - chain's left argument, abs(hair-tower)
     ¥                      - last two links as a dyad:
    _                       -   subtract (yields the 2/3 rounded amount)
   ;                        - concatenate
       4ẋ                   - repeat 4 (vectorises) (i.e. [[4,4,...],[4,...]])
         +                  - add (hair > tower)? (vectorises) (i.e. 4s->5s if so)
             ¦              - sparse application:
          ;€  7             - of:  concatenate €ach with a 7
            ⁹               - to indexes: chain's right argument, (hair-tower)?
               Ṛ            - reverse the list
                ṭ           - tack (hair-tower)?
                 ;8         - concatenate an 8
                    “øŻPLC» - compression of the word "Abroach" & the string "!B"
                   ị        - index into "Abroach!B" (1-indexed & modular, so 0->B)
                            - implicit (smashed) print

ạç> - Main link: number, hair; number, tower
ạ   - absolute difference -> abs(hair-tower)
  > - greater than? -> (hair > tower)? (1 if so, else 0)
 ç  - call the last link (1) as a dyad

* As a monadic link it returns a list of characters and lists of characters e.g. ['B', [['o', 'o', 'h'], ['o']], '!'], as a full program the implicit print smashes this e.g. Booho!

\$\endgroup\$
  • \$\begingroup\$ Yep, there is. :-) \$\endgroup\$ – Erik the Outgolfer Jul 16 '17 at 11:05
  • \$\begingroup\$ Yeah I started with the idea of using AL€œs3 and found div by zero errors and then after I addressed that with the much longer code above found I still needed to special case the equal length case. I thought I'd try and implement a different way today, but looks like you did already. \$\endgroup\$ – Jonathan Allan Jul 16 '17 at 12:42
  • \$\begingroup\$ ...mind you I was thinking 25-30 bytes :) \$\endgroup\$ – Jonathan Allan Jul 16 '17 at 12:48
4
\$\begingroup\$

Python 3, 87 bytes

lambda l,h:["B"+"o"*round((h-l)*2/3)+"h"+"o"*round((h-l)/3),"A"+"a"*(l-h)+"h"][l>h]+"!"

Try it online!

Arguments to the function are taken in the order length of hair, height of tower.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 38 35 32 bytes

Input order: length of hair, height of tower

‹i¹α'a×"Aÿh!"ë¹-x‚>3÷R'o×'hý…Bÿ!

Try it online!

Explanation

‹i                                  # if h < l
  ¹α                                # push absolute difference of h and l
    'a×                             # repeat "a" that many times
       "Aÿh!"                       # interpolate between "A" and "h!"
 ë                                  # else 
  ¹-                                # push h-l
    x‚                              # pair with its double
      >3÷                           # increment and integer divide by 3
         R                          # reverse the list
          'o×                       # for each, repeat "o" that many times
             'hý                    # merge the o's on "h"
                …Bÿ!                # interpolate between "B" and "!"
\$\endgroup\$
  • \$\begingroup\$ You seem to have forgotten a 3 which is shown on TIO. \$\endgroup\$ – Erik the Outgolfer Jul 16 '17 at 12:29
  • \$\begingroup\$ @EriktheOutgolfer: Thanks for notifying. I've fixed the explanation to conform with the code :) \$\endgroup\$ – Emigna Jul 16 '17 at 12:53
  • \$\begingroup\$ Oh, and you have spacing problems with your explanation. \$\endgroup\$ – Erik the Outgolfer Jul 16 '17 at 12:54
  • \$\begingroup\$ @EriktheOutgolfer: I don't see it. If you mean the fact that they are in 2 diagonal lines, that's an intentional separation of if-else to decrease horizontal space used. If there's something else I must be blind. \$\endgroup\$ – Emigna Jul 16 '17 at 13:09
  • \$\begingroup\$ Oh I was confused with the extraneous whitespace in it... \$\endgroup\$ – Erik the Outgolfer Jul 16 '17 at 13:12
4
\$\begingroup\$

Javascript, 105 97 bytes

Thanks to Oki for helping save 7 bytes!

p=(t,s)=>(s||"o").repeat(t)
l=>h=>(d=l-h,o=d/3-.5|0,l>h?`A${p(d,"a")}h`:`B${p(o-d)}h`+p(-o))+"!"

Defines an anonymous currying function. Use like f(length)(height)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to be working for f(1)(4). Replacing p(o+d) with p(-o-d) could fix this. Also you can use o=-d*2/3+.5|0 to save 7 bytes. \$\endgroup\$ – Oki Jul 16 '17 at 10:56
  • \$\begingroup\$ @Oki thanks for pointing that error out and showing me the shorter rounding! \$\endgroup\$ – DanTheMan Jul 16 '17 at 14:58
  • \$\begingroup\$ p=(t,s='o')=>s.repeat(t) \$\endgroup\$ – tsh Jul 17 '17 at 8:11
2
\$\begingroup\$

PHP>=7.1, 111 bytes

[,$h,$t]=$argv;echo BA[$b=$h>$t],($r=str_repeat)(oa[$b],$c=round(($a=abs($h-$t))*($b?:2/3))),h,$r(o,$a-$c),"!";

PHP Sandbox Online

\$\endgroup\$
  • \$\begingroup\$ Argument order 1. hair 2. tower? \$\endgroup\$ – racer290 Jul 16 '17 at 13:25
  • \$\begingroup\$ @racer290 Yes $h hair $t tower \$\endgroup\$ – Jörg Hülsermann Jul 16 '17 at 13:45
2
\$\begingroup\$

Jelly, 32 bytes

_‘”aẋŒt;⁾h!ɓ_÷1.,3+.”oẋ“Bh!”żð>?

Try it online!

-1 thanks to Jonathan Allan.

Only works as full program.

Arguments are in order: hair, tower

\$\endgroup\$
  • \$\begingroup\$ Save a byte by replacing ær0 with +. \$\endgroup\$ – Jonathan Allan Jul 16 '17 at 23:04
  • \$\begingroup\$ @JonathanAllan Ooh it does indeed work. (damn two-byte builtin) \$\endgroup\$ – Erik the Outgolfer Jul 17 '17 at 6:41
0
\$\begingroup\$

Julia, 101 bytes

g(h,t)=h>t?string("A","a"^(h-t),"h!"):string("B","o"^round(Int,(t-h)*2/3),"h","o"^round(Int,(t-h)/3))

Arguments to the function are taken in the order length of Hair, height of Tower.

\$\endgroup\$
  • \$\begingroup\$ Typo: heigth of Hower \$\endgroup\$ – racer290 Jul 16 '17 at 13:46
0
\$\begingroup\$

Jelly, 84 76 bytes

It's really rather long, but I spent too much time on this not to post it. Takes two integer arguments:

  1. length of hair
  2. length of tower

Any tips on golfing this would be appreciated.

³>⁴×111ð;ø³>⁴¬×97
97ðxø³ạ⁴
111ðxø¢L÷3×⁸ær0:1
2Çṭ2£“h”ṭµ1ÇṭµFḟ1£
>¬+65¥;¢;33Ọ

Try it online!

\$\endgroup\$
  • \$\begingroup\$ well, there is already a much shorter Jelly answer \$\endgroup\$ – Cœur Jul 17 '17 at 5:07
0
\$\begingroup\$

R, 117 bytes

x=diff(scan());cat(`if`(x>0,paste0("A",rep("a",x),"h!"),paste0("B",rep("o",F<-round(-2*x/3)),"h",rep("o",-x-F),"!")))

A bit long, pretty sure this can be golfed down. Takes input from STDIN in the order Tower, Hair.

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

Python 2, 77 bytes

lambda h,t:("BA"+(abs(h-t)*2+1)/3*"oa"+"h"+(abs(h-t)+1)/3*"ao"+"h!!")[h>t::2]

An unnamed function taking hair length, h, and tower height, t, and returning a string.

Try it online!

Builds a string starting with BA, followed by two thirds of the difference rounded of the string oa repeated, followed by a single h, then the remainder amount of ao repeated, and finally h!!. The return value is then every second character starting with either the B or A via the slice notation [h>t::2].

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

Perl, 107 bytes

Takes hair length first, tower length second.

sub b{(($c=$_[0]-$_[1])>0?'A'.'a'x--$c.'h':do{$o=int(2/3*($c*=-1)+.5);'B'.('o'x$o).'h'.('o'x($c-$o))}).'!'}
\$\endgroup\$
  • \$\begingroup\$ You can get rid of the ``` and intend your code by 4 spaces to make it look nicer.. \$\endgroup\$ – Roman Gräf Jul 17 '17 at 17:27

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.