17
\$\begingroup\$

It seems as of recent, there have been a lot a Jimmys falling to their death, as can be seen here, and here where you were asked to determine if Jimmy would fall. It is time we put a stop to this madness and try to save Jimmy.

Jimmy has three body parts /, o, and \ arranged like this

/o\

Platforms are represented with -. Jimmy will fall off their platform iff they have two or more body parts that are not directly above a platform.

Some examples:

   /o\
- -------

Jimmy will balance since all their body parts are above a -.

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

Jimmy will balanced since two body parts are above -s.

 /o\
-- ----  --

Jimmy will balance even though they are split between two platforms

  /o\
   -

Jimmy will not balanced since two body parts are not above a platform.


Since my platform supply is running low, I only have platforms with a length of 5, and it is important we use as few as possible.

Your task is to take an input of Jimmys and output a string of platforms which will save all of the Jimmys in the input. Your output must use as few platforms as possible, but each platform must be 5 -'s wide.

Note that the rule of the platform being 5 - wide, means that each platform must have a space between them. ---------- is not valid in the output since it will be considered a platform of length 10 and not two platforms of length 5.

Test Case's

/o\  /o\/o\    // input
----- -----    // output
/o\           /o\
-----       -----
/o\           /o\      // same input as above
-----          -----   // this is also valid output
    /o\ /o\
     -----

Standard rules apply.

This is code-golf, may the shortest answer win!

\$\endgroup\$
  • 2
    \$\begingroup\$ @Veskah as long as all Jimmys are saved using the fewest possible platforms, position does not matter, so there will be multiple valid outputs for each input. There is no condition on how many Jimmys there will be in the input, an empty string would be a valid input. \$\endgroup\$ – Quinn Jul 5 at 17:45
  • 3
    \$\begingroup\$ Test case 4 looks like a scary face \$\endgroup\$ – Expired Data Jul 5 at 17:48
  • 2
    \$\begingroup\$ Follow-up: place as many Jimmys as possible on platforms in such a way that they will not fall. \$\endgroup\$ – val Jul 5 at 19:06
  • 2
    \$\begingroup\$ @val that is actually one of the linked questions - I followed up after that one :P \$\endgroup\$ – Quinn Jul 5 at 19:07
  • 7
    \$\begingroup\$ I can see so many Jimmy follow-ups in the future. \$\endgroup\$ – connectyourcharger Jul 5 at 19:30
2
\$\begingroup\$

Charcoal, 17 15 bytes

W‹ⅈLθ«×⁵№o\§θⅈ→

Try it online! Link is to verbose version of code. Explanation:

W‹ⅈLθ«

Repeat while the cursor position is less than the length of the input.

×⁵№o\§θⅈ

If the character at that position in the input is an o or a \ then print 5 -s.

Move to the next character, thus guaranteeing at least one space between platforms.

Previous 17-byte solution is IMHO more "Charcoal-y".

θ⸿Fθ«×⁵¬№ /⊟KD²↑→

Try it online! Link is to verbose version of code. Output includes input, thus demonstrating correctness of solution. Explanation:

θ⸿

Print the input and move to the start of the next line.

Fθ«

Loop over every character of the input to ensure that no Jimmy gets missed.

×⁵¬№ /⊟KD²↑

Look at the character above the cursor. If there is none, or if it is space or /, then do nothing, otherwise print 5 -s.

Move to the next character, thus guaranteeing at least one space between platforms.

\$\endgroup\$
7
\$\begingroup\$

Python 2, 70 67 bytes

lambda s:S('/',' ',S("\S.{5}","----- ",s+' '*5))
import re;S=re.sub

Try it online!

-3 bytes thanks to Kevin Cruijssen & Neil

Not the prettiest, not sure how to better handle those leftover slashes...

Unfortunately, we can't replace both ends of each platform with spaces using a single re.sub call, because in the case of 2 platforms being one space apart, the gap between them can't be matched more than once. A lookahead/lookbehind assertion won't help, because anything matched within those assertions doesn't get replaced.

Using a single re.sub reference:

Python 3.8 (pre-release), 78 bytes

lambda s:[s:=re.sub(".[^/ -].{5}"," ----- ",s+"  ",1)for c in s][-1]
import re

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 69 bytes by first replacing the / and then the [^ ]. \$\endgroup\$ – Kevin Cruijssen Jul 8 at 12:50
  • \$\begingroup\$ @Neil's suggested golf on my Retina answer also works for you: 67 bytes. \$\endgroup\$ – Kevin Cruijssen Jul 8 at 15:57
4
\$\begingroup\$

JavaScript (ES6),  56 55  54 bytes

s=>[...s+1e4].map(c=>(s--?s:s=c>{}&&5)?'-':' ').join``

Try it online!

Or 47 bytes if returning an array of characters is acceptable.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 158 164 160 bytes

a=input();r=""
for i,c in enumerate(a):
 try:r[i]
 except:
  if c in"/o":
   r+=(a[i+5<len(a)and i+5or len(a)-1]=="o"and" "or"")+"----- "
  else:r+=" "
print(r)

Try it online!

This is my first code golf answer, and I'm happy it's on a Jimmy question!

Explanation:

  • a=input();r="": Take in input and initialize a new string r.

  • for i,c in enumerate(a):: Enumerate over the input.

  • try:r[i] ... except:: See if r[i] exists - if not, process the except block.

  • if c in"/o":: Check if the current character is in Jimmy's first two body parts.

  • r+=(a[i+5<len(a)and i+5or len(a)-1]=="o"and" "or"")+"----- ": If so, add a new segment. Add a space before our new segment if another Jimmy head is present in five characters.

  • else:r+=" ": Otherwise, just add a space.

  • print(r): Print our final result.

\$\endgroup\$
  • \$\begingroup\$ Pretty close, but I think this actually doesn't works for all test cases. It should use as few platforms as possible and your last example uses 2 when it could be done with 1 \$\endgroup\$ – Quinn Jul 5 at 21:54
  • \$\begingroup\$ @Quinn Shoot. Err, non-competing until I fix that. \$\endgroup\$ – connectyourcharger Jul 5 at 22:06
  • \$\begingroup\$ @connectyourcharger "non-competing" doesn't validate an invalid solution. I recommend deleting it if you're not going to fix it for some time (e.g. the next few minutes). \$\endgroup\$ – Erik the Outgolfer Jul 5 at 22:53
  • \$\begingroup\$ @Quinn I fixed it now. \$\endgroup\$ – connectyourcharger Jul 5 at 23:14
  • \$\begingroup\$ @connectyourcharger Nice! \$\endgroup\$ – Quinn Jul 6 at 0:55
2
\$\begingroup\$

Retina, 23 21 bytes

/

$
5* 
\S.{5}
5*- 

-2 bytes thanks to @Neil.

Contains a single trailing space on the second, fourth, and sixth lines.

Port of @negativeSeven's Python 2 answer, so make sure to upvote him!

Try it online.

Explanation:

Replace all "/" with a " ":

/
 

Append 5 trailing spaces:

$
5* 

Replace all substrings of size six that do not start with a space by "----- ":

\S.{5}
5*- 
\$\endgroup\$
  • \$\begingroup\$ Can you use \S instead of [^ ]? \$\endgroup\$ – Neil Jul 8 at 14:08
  • \$\begingroup\$ @Neil Ah, of course. Thanks! \$\endgroup\$ – Kevin Cruijssen Jul 8 at 15:56
1
\$\begingroup\$

Jelly, 35 bytes

;3x5¤;0;ṛṫ6ɗ
;⁶x5¤e€⁾o\œṡ1ç/$ÐLị⁾- 

Try it online!

A monadic link that takes the input as a string and returns a Jelly string with the platforms.

Takes inspiration from @negativeseven’s answer.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 25 24 bytes

ð5׫'/ð:DŒ6ùʒнðÊ}'-5×ð«:

Port of @negativeSeven's Python 2 answer, so make sure to upvote him!

Try it online or verify all test cases.

Explanation:

ð5׫                      # Append 5 trailing spaces to the (implicit) input-string
    '/ð:                 '# Replace all "/" with a space
        D                 # Duplicate the string
         Π               # Get all substrings of this
          6ù              # Only leave those of length 6
            ʒ   }         # Filter it further by:
             нðÊ          #  Only keep those which do NOT start with a space
                 '-5×ð«  '# Push a string of 5 "-" appended with a space: "----- "
                       :  # Replace in the initially duplicated string all substrings 
                          # remaining in the list with this "---- "
\$\endgroup\$
1
\$\begingroup\$

Japt, 23 22 bytes

A port of Arnauld's JS solution that I'm just too exhausted to fully test. If it's invalid can a Diamond please delete?

+L² £=U´?U:X>M©5)?'-:S

Try it

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