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..


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.


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!

  • 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\$ Jul 15, 2017 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\$ Jul 15, 2017 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, 2017 at 4:38

11 Answers 11


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!


A full program printing the result*.

Try it online!


‘: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!

  • \$\begingroup\$ Yep, there is. :-) \$\endgroup\$ Jul 16, 2017 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\$ Jul 16, 2017 at 12:42
  • \$\begingroup\$ ...mind you I was thinking 25-30 bytes :) \$\endgroup\$ Jul 16, 2017 at 12:48

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.


05AB1E, 38 35 32 bytes

Input order: length of hair, height of tower


Try it online!


‹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 "!"
  • \$\begingroup\$ You seem to have forgotten a 3 which is shown on TIO. \$\endgroup\$ Jul 16, 2017 at 12:29
  • \$\begingroup\$ @EriktheOutgolfer: Thanks for notifying. I've fixed the explanation to conform with the code :) \$\endgroup\$
    – Emigna
    Jul 16, 2017 at 12:53
  • \$\begingroup\$ Oh, and you have spacing problems with your explanation. \$\endgroup\$ Jul 16, 2017 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, 2017 at 13:09
  • \$\begingroup\$ Oh I was confused with the extraneous whitespace in it... \$\endgroup\$ Jul 16, 2017 at 13:12

Javascript, 105 97 bytes

Thanks to Oki for helping save 7 bytes!


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

Try it online!

  • \$\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, 2017 at 10:56
  • \$\begingroup\$ @Oki thanks for pointing that error out and showing me the shorter rounding! \$\endgroup\$
    – DanTheMan
    Jul 16, 2017 at 14:58
  • \$\begingroup\$ p=(t,s='o')=>s.repeat(t) \$\endgroup\$
    – tsh
    Jul 17, 2017 at 8:11

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

  • \$\begingroup\$ Argument order 1. hair 2. tower? \$\endgroup\$
    – racer290
    Jul 16, 2017 at 13:25
  • \$\begingroup\$ @racer290 Yes $h hair $t tower \$\endgroup\$ Jul 16, 2017 at 13:45

Jelly, 32 bytes


Try it online!

-1 thanks to Jonathan Allan.

Only works as full program.

Arguments are in order: hair, tower

  • \$\begingroup\$ Save a byte by replacing ær0 with +. \$\endgroup\$ Jul 16, 2017 at 23:04
  • \$\begingroup\$ @JonathanAllan Ooh it does indeed work. (damn two-byte builtin) \$\endgroup\$ Jul 17, 2017 at 6:41

Julia, 101 bytes


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

  • \$\begingroup\$ Typo: heigth of Hower \$\endgroup\$
    – racer290
    Jul 16, 2017 at 13:46

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.


Try it online!

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

R, 117 bytes


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


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].


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))}).'!'}
  • \$\begingroup\$ You can get rid of the ``` and intend your code by 4 spaces to make it look nicer.. \$\endgroup\$ Jul 17, 2017 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.