Jimmy needs your help!

17

1

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!

Quinn

Posted 2019-07-05T17:29:03.707

Reputation: 1 153

1Does board position matter if it could go multiple places or anywhere is fine as long as all Jimmys are saved? Is there always at least 1 Jimmy? – Veskah – 2019-07-05T17:42:06.493

2@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. – Quinn – 2019-07-05T17:45:17.853

3Test case 4 looks like a scary face – Expired Data – 2019-07-05T17:48:24.600

1Jimmy's not happy with you. – connectyourcharger – 2019-07-05T18:29:47.627

1@connectyourcharger I was just trying to help Jimmy! – Quinn – 2019-07-05T18:31:16.477

1I don't think Jimmy likes all this negative attention, but he appreciates the help. – connectyourcharger – 2019-07-05T18:32:04.373

1All joking aside - can we output empty platforms without any Jimmys on top? – connectyourcharger – 2019-07-05T18:38:13.703

1@connectyourcharger as I mentioned in the question, you must use as few platforms as possible, so outputting platforms without any Jimmys on them would likely break this rule. – Quinn – 2019-07-05T18:41:29.980

1@Quinn Ok, gotcha. – connectyourcharger – 2019-07-05T18:42:00.983

1What are the rules for trailing whitespace in input/output? – negative seven – 2019-07-05T18:57:54.137

1@negativeseven Both inputs and outputs may contain trailing whitespace – Quinn – 2019-07-05T18:59:22.297

2Follow-up: place as many Jimmys as possible on platforms in such a way that they will not fall. – val says Reinstate Monica – 2019-07-05T19:06:48.447

2@val that is actually one of the linked questions - I followed up after that one :P – Quinn – 2019-07-05T19:07:32.457

2@Quinn That one tells to only count them. Why not actually calculate placement string like in this challenge? – val says Reinstate Monica – 2019-07-05T19:09:40.417

1@val ah, you are right - that could be another follow up! – Quinn – 2019-07-05T19:10:47.183

7I can see so many Jimmy follow-ups in the future. – connectyourcharger – 2019-07-05T19:30:22.060

Is my output allowed to contain the input demonstrating that all of the platforms correctly save all of the Jimmies? – Neil – 2019-07-07T10:17:52.207

@Neil Im torn on how to answer you, I keep going back and forth on it - but I think ill allow it – Quinn – 2019-07-08T13:14:17.890

Answers

2

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.

Neil

Posted 2019-07-05T17:29:03.707

Reputation: 95 035

7

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!

negative seven

Posted 2019-07-05T17:29:03.707

Reputation: 1 931

69 bytes by first replacing the / and then the [^ ]. – Kevin Cruijssen – 2019-07-08T12:50:56.813

@Neil's suggested golf on my Retina answer also works for you: 67 bytes.

– Kevin Cruijssen – 2019-07-08T15:57:14.140

4

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.

Arnauld

Posted 2019-07-05T17:29:03.707

Reputation: 111 334

2

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.

connectyourcharger

Posted 2019-07-05T17:29:03.707

Reputation: 2 056

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 – Quinn – 2019-07-05T21:54:52.127

@Quinn Shoot. Err, non-competing until I fix that. – connectyourcharger – 2019-07-05T22:06:38.320

@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). – Erik the Outgolfer – 2019-07-05T22:53:13.917

@Quinn I fixed it now. – connectyourcharger – 2019-07-05T23:14:06.687

@connectyourcharger Nice! – Quinn – 2019-07-06T00:55:05.400

Doesn't quite work for three Jimmies separated by 2 spaces each, though not far off. – negative seven – 2019-07-06T05:32:56.343

Good catch, that's one dead Jimmy – Quinn – 2019-07-06T11:30:18.190

@negativeseven Whoopsies. Ah well, I think this answer was doomed from the start. Sorry, Jimmy! – connectyourcharger – 2019-07-06T12:06:56.917

It should work alright if you always add a space before a platform.

– negative seven – 2019-07-06T21:23:42.217

2

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

Kevin Cruijssen

Posted 2019-07-05T17:29:03.707

Reputation: 67 575

Can you use \S instead of [^ ]? – Neil – 2019-07-08T14:08:33.827

@Neil Ah, of course. Thanks! – Kevin Cruijssen – 2019-07-08T15:56:37.213

1

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.

Nick Kennedy

Posted 2019-07-05T17:29:03.707

Reputation: 11 829

1

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

Shaggy

Posted 2019-07-05T17:29:03.707

Reputation: 24 623

1

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

Kevin Cruijssen

Posted 2019-07-05T17:29:03.707

Reputation: 67 575