29
\$\begingroup\$

Backstory

Meet my friend Jimmy:

/o\

Jimmy is a little character who likes to stand on platforms. Here's Jimmy safely standing on a platform:

         /o\
  -------------

Now, Jimmy has a good sense of balance, so he can safely stand with one leg off of the platform, like so:

   /o\
    -------------------

Although if he stands with two or more body parts off of the platform, he will fall. Both of these are examples where Jimmy will fall:

/o\                                       /o\
  ----------        ----------------------   

The challenge

Your challenge is to write a program to determine, given a string with Jimmy's platform and position, if Jimmy can stand on the platform without falling off.

  • Input: Two lines showing Jimmy's position and the position of the platform under him. This can be from two separate inputs, a single input, or an array of some sort.

    1. You may take input through any reasonable form, includings functions and standard input. Only resort to hard-coding if your language does not support the other input methods.
  • Output: The boolean values true and false, or the integers 1 or 0 to represent true/false respectively.

    1. The boolean value is based off of whether Jimmy can stay on the platform or not - true if Jimmy can stay on the platform, or false if he will fall off.
  • The platform size is arbitrary and can be changed at will. Your program should account for that.

    1. The platform cannot be a length of zero, and the platform must be complete (no holes in the platform).

    2. Remember that Jimmy falls off when two of his body parts are hanging off the platform. A body part is one ASCII character of his body.

    3. Trailing whitespace at the end of the platform is not required, but your program should account for both situations, where there is whitespace after the platform and where there is not.

  • Be mindful of the standard loopholes that are forbidden.

Test cases

         /o\               ✔️ TRUE
  -------------

/o\                        ✔️ TRUE
 ----------

                    /o\    ❌ FALSE
  ------------------

               /o\         ❌ FALSE
  -------

    /o\                    ❌ FALSE
     -

Scoring

This is , so lowest byte count wins.

Leaderboard

You can view the leaderboard for this post by expanding the widget/snippet below. In order for your post to be included in the rankings, you need a header (# header text) with the following info:

  • The name of the language (end it with a comma , or dash -), followed by...

  • The byte count, as the last number to appear in your header.

For example, JavaScript (ES6), 72 bytes is valid, but Fortran, 143 bytes (8-bit) is invalid because the byte count is not the last number in the header (your answer will be recognized as 8 bytes - don't take advantage of this).

<!-- Run the snippet to see the leaderboard. Report any bugs to @xMikee1 on Github -->    <iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=187586" width="100%" height="100%" style="border:none;">Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.</iframe><style>html,body{margin:0;padding:0;height:100%;overflow:hidden}</style>

\$\endgroup\$
  • \$\begingroup\$ Can we assume trailing whitespace after Jimmy? Also if you’re allowing arrays of characters, in some languages those will have to be padded with whitespace. \$\endgroup\$ – Nick Kennedy Jul 2 at 23:07
  • \$\begingroup\$ @NickKennedy You need to account for trailing whitespace or no trailing whitespace. I didn't set strict rules on that. \$\endgroup\$ – connectyourcharger Jul 2 at 23:09
  • 12
    \$\begingroup\$ Title is "will jimmy fall of the platform" and you required to output "will jimmy stay on the platform". Is this expected behavior? \$\endgroup\$ – tsh Jul 3 at 3:29
  • 6
    \$\begingroup\$ Can you reformat your test cases to make them easier to copy & paste, please? \$\endgroup\$ – Shaggy Jul 3 at 6:53
  • 2
    \$\begingroup\$ Is swapping truthy-falsy values allowed? (i.e. output true when Jimmy falls and false when that doesn't happen?) \$\endgroup\$ – Mr. Xcoder Jul 3 at 9:48

41 Answers 41

0
\$\begingroup\$

Japt -!, 7 bytes

ÕèSi.)z

Try it

ÕèSi.)z     :Implicit input of multi-line string
Õ           :Transpose
 è          :Count the occurrences of
  S         :  Space
   i.       :  Prepended with ".", giving the regex /. /g
     )      :End count
      z     :Floor divide by 2
            :Implicit output as Boolean
\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 bytes

!!tt#rL6C

Try it here! (Note: This is a temporary interpreter, hosted on pythtemp.herokuapp.com; the official pyth.herokuapp.com can't be used for now)

If swapping truthy-falsy values were allowed, this would become 8 bytes by dropping one of the !s (awaiting confirmation). Moreover, if outputting an empty list for the case when Jimmy falls and a non-empty list otherwise would be an option, both !s could be dropped.

How it works

!!tt#rL6C – Full program. Receives a list of two strings through STDIN.
        C – Transpose / Zip (switch from rows to columns)
     rL6  – Strip the whitespace on both sides of each column.
   t#     – Filter-keep those that have a tail (i.e. that are length-2).
  t       – Tail. The result will be non-empty iff Jimmy doesn't fall.
!!        – Double negation (to output as a boolean...).
\$\endgroup\$
0
\$\begingroup\$

Retina, 31 29 bytes

^(.)*/.*¶(?<-1>.)*(?(1)!) ?--

Try it online!

Saved 2 bytes thanks to Neil!

A simple regex that determines whether at least two pieces of the platform are directly beneath Jimmy. Assumes that the input will contain no ! characters.

\$\endgroup\$
  • \$\begingroup\$ I don't think you need the trailing -?. \$\endgroup\$ – Neil Jul 3 at 10:30
  • \$\begingroup\$ @Neil Quite right, I don't know what case I thought I was covering with that. \$\endgroup\$ – FryAmTheEggman Jul 3 at 15:50
0
\$\begingroup\$

Attache, 38 bytes

{2>Sum!`~&Sum[Ord@Grid@_-39]=>1'46'65}

Try it online!

Explanation

We convert the input string to a grid of ASCII codepoints, subtract 39 from each, then take the Sum, columnwise. (Sum[Ord@Grid@_-32]). Then, we bind this array to the ~ operator, which in this case counts the number of times the argument occurs in the bound array. We apply this function to each member of the array 1'46'65 with =>, which correspond to various body parts not above a platform. We then take the sum of this resulting array with Sum!. This is the count of body parts not above the platform. Finally, we check to see that 2 exceeds this sum; if it does, then we return false, for Jimmy has fallen. If it does not, we return true, for Jimmy is quite safe on his perch.

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

40 column Commodore BASIC (C64, C16/+4, C128, PET, C64Mini) - non-competing* - 98 tokenized and BASIC bytes used

 0a=rN(.)*36:b=rN(.)*39:c=-rN(.)*(b-39):fOi=0tob:a$=a$+"-":nE:?tAa)"/o\
 1?tAc)a$:?-(a>c-2anda<c+b+2)

Here is the unobfuscated listing for explanation purposes

 0 let a=rnd(0) * 36
 1 let b=rnd(0) * 39
 2 let c=-rnd(0) * (b - 39)
 3 for i=0 to b
 4  let a$=a$+"-"
 5 next i
 6 print tab(a);"/o\"
 7 print tab(c);a$
 8 print -(a>c - 2 and a<c + b + 2)

The variable a is used for positioning Jimmy (using PRINT TAB(A) which is zero-indexed).

The varaible b is used for the length of the platform.

And finally the variable c is used for the position of the platform.

The loop in lines 3 through to 5 inclusive builds the platform for Jimmy, with line 6 printing Jimmy at his pre-determined position, and 7 for the pre-determined position of the platform. Finally, 1 or 0 is outputted to represent our TRUE or FALSE value.

Note, the listing includes keyword abbreviations to ensure we break the 80 character limit per line on standard Commodore C64 BASIC.

enter image description here

* Assumed non-competing because PETSCII is not ASCII. Jimmy looks better in PETSCII though, of course.

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

Charcoal, 19 bytes

η⸿Pθ…θ⌕θo≔№KM-ζ⎚›ζ¹

Try it online! Link is to verbose version of code. Outputs - if the Jimmy will balance, nothing if he will fall off. Explanation:

η

Print the platform.

⸿

Move down a line. (Reversed gravity turned out to be golfier.)

Pθ

Print Jimmy without moving the cursor.

…θ⌕θo

Print Jimmy's leg leaving the cursor over his body.

≔№KM-ζ

Count the number of adjacent -s from the platform.

Clear the canvas.

›ζ¹

Output a - if the platform was large enough.

\$\endgroup\$
  • \$\begingroup\$ The challenge asks for boolean output rather than the two distinct value sort of output that tends to be common. \$\endgroup\$ – Wheat Wizard Jul 6 at 2:03
  • \$\begingroup\$ @SriotchilismO'Zaic That's just the default output of a boolean expression in Charcoal. \$\endgroup\$ – Neil Jul 6 at 9:36
0
\$\begingroup\$

Brain-Flak, 210 bytes

{({}<>)<>}<>{({}[(()()()()()){}]<>)<>}<>({<(<({}[((()()()()()){}()){}])>{()(<{}>)}{}<{({}<>)<>}{}({}[((((()()()()()))){}{}){}{}])>[()()]{()(<{}>)}<<>{({}<>)<>}<>>)>[()]{()(<{}>)}{}}<>)(()[{<({}())>{()<{}>}}]{})

Try it online!

String parsing is hard in Brain-Flak. It considers anything on the first line other than space to be Jimmy and anything on the second line other than - to be open air. The reason we test for - instead of space is that Brain-Flak pads the input indefinitely to the right with nulls (ASCII codepoint 0) which we need to consider open air. This luckily means we don't have to worry about needing trailing whitespace in the input.

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

Elm 0.19, 99 bytes

f j p=List.sum(List.map2(\a b->if a/=' '&&b/=' 'then 1 else 0)(String.toList j)(String.toList p))>1

Port of Maxwell's Python 3 answer. Verify all test cases here.

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

PHP, 90 80 bytes

Code

<?=substr_count(substr(($v=explode("
",$argv[1]))[1],strpos($v[0],"/"),3),"--");

Try it online!

Mistakenly coded allowwing jimmy with just a leg on the edge

<?=count($v=explode("
",$argv[1]))>1&&substr_count(substr($v[1],strpos($v[0],"/"),3),"-");

Try it online!

Explanation

<?=
substr_count(
substr(($v=explode("\n",$argv[1]))[1],// explode and get Jimmy body
        strpos($v[0],"/"),  // find "/" position (position start)
        3),                 // substring of the platform length
        "--");              // platform has to contain "--" somewhere
// 
//
\$\endgroup\$
  • \$\begingroup\$ The puzzle states that the input is two lines, and the second one is not empty, so you can remove that check \$\endgroup\$ – Einacio Jul 8 at 11:05
  • \$\begingroup\$ I tested the code on the TIO, and it fails with two spaces before the platform, or if jimmy is only with it's left leg (test case 3). it also fails the last test case \$\endgroup\$ – Einacio Jul 8 at 11:09
  • \$\begingroup\$ Thank you @Einacio, i mistakenly coded allowing jimmy to have 1 leg and nothing else on the platform, also ty for the advice on removing the condition for 2 lines as input. \$\endgroup\$ – Francisco Hahn Jul 8 at 14:17
0
\$\begingroup\$

Bash, 49 bytes

[[ "${1:$((`expr index "$0" /`-1)):3}" =~ "--" ]]

Jimmy input as command line parameter 0, the platform as parameter 1; Must be aware of Bash's separating and eating whitespace (use single quotes):

bash -c '[[ "${1:$((`expr index "$0" /`-1)):3}" =~ "--" ]]' '   /o\' '  -------'
\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 42 40 bytes

y(a,b)=strip(b[findfirst("/o\\",a)])>"-"

Try it online!

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