How many Jimmys can fit?

29

1

In this simple but fun challenge, you were asked to determine if Jimmy would fall of their platform. 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.


Your task is to write a program that takes a platform as a lengthed container containing only -s and s (e.g. a string) and outputs the number of Jimmys that can be placed on the platform such that none of them will fall and none of them will overlap. A Jimmy may have one of their body parts to the left of the beginning of the string or the right of the end of the string.

This is so answers are scored in bytes with fewer bytes being the aim.

Test cases

Inputs

-  -  -

- -
--
-- --
----
- -- --
------- -

Respective outputs

0
0
1
1
2
2
2
3

Post Rock Garf Hunter

Posted 2019-07-04T18:47:54.880

Reputation: 55 382

Answers

15

JavaScript (ES6),  45 41  40 bytes

Saved 4 bytes thanks to @Shaggy

s=>(0+s+0).split(/.--|-.-|--./).length-1

Try it online!

Arnauld

Posted 2019-07-04T18:47:54.880

Reputation: 111 334

141 bytes – Shaggy – 2019-07-04T19:59:04.720

7@Shaggy Thanks! I knew something was wrong there, but I had to help my wife on some Super Mario Galaxy level in the meantime... and it was also a problem about broken platforms. :p – Arnauld – 2019-07-04T20:12:11.013

2the --- wasn't sitting well with me either, until I was working on my port and realised they weren't needed. Think I'll call it a day here, grab a bag of cans and throw on SMG myself - haven't played it in an age. – Shaggy – 2019-07-04T20:24:32.367

And now all the other answers are using the same regex. – Cœur – 2019-07-07T02:15:12.323

8

Python 2, 53 bytes

lambda s:len(re.findall('.--|-.-|--.',`s`))
import re

Try it online!

Based on Arnauld's regex. Greedily searches for all the non-overlapping length-3 substrings with two or more -. A trick is to do `s` to enclose the input string in quotes as padding to leave room for Jimmys to hang off on either end like

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

Python 2, 57 bytes

f=lambda s:'--'in s[:3]*2and-~f(s[3:])or s>''and f(s[1:])

Try it online!

Requires a cheesy I/O format of the input already in quotes. Outputs False for 0.

A recursive function that places each Jimmy on the leftmost position allowed, by either placing Jimmy over the first three characters if they can hold Jimmy, or otherwise deleting the first character. A cute trick is to check if s[:3] contains two or more - by doing '--'in s[:3]*2, which concatenates two copies of s[:3] and checks for two adjacent -.

xnor

Posted 2019-07-04T18:47:54.880

Reputation: 115 687

3

Japt, 16 bytes

Based on Arnauld's original JS solution. I tried a few different methods to get the necessary padding either side of the input but all came in at the same length - still searching for a shorter way...

ûUÊÄÄ è".--|-."ê

Test it

ûUÊÄÄ è".--|-."ê     :Implicit input of string U
û                    :Centre pad with spaces to length
 UÊ                  :  Length of U
   ÄÄ                :  Add 1, twice
      è              :Count the occurrences of
       ".--|-."ê     :  ".--|-." palindromised, resulting in the RegEx /.--|-.-|--./g

Shaggy

Posted 2019-07-04T18:47:54.880

Reputation: 24 623

3

Perl 5 -p, 28 bytes

Uses the same method as @Arnauld's JavaScript.

$_=@a=" $_ "=~/.--|-.-|--./g

Try it online!

Xcali

Posted 2019-07-04T18:47:54.880

Reputation: 7 671

3

Excel, 96 bytes

A1 = platform. Entered as array Formula Ctrl+Shift+Enter

=SUM(IF(LEN(TRIM(MID(IF(MOD(LEN(A1),3)=1," ","")&A1,3*ROW(INDIRECT("A1:A"&LEN(A1)))-2,3)))>1,1))

remoel

Posted 2019-07-04T18:47:54.880

Reputation: 511

3

05AB1E, 16 bytes

ðì‚ε3ôʒ'-¢2@}g}à

Can definitely be golfed.. Sometimes it's annoying to see all these regex answers in a challenge when using 05AB1E, which is lacking regex of any kind. ;)

Try it online or verify all test cases.

Explanation:

ðì            # Prepend a space before the (implicit) input
  ‚           # Pair it with the unmodified (implicit) input
   ε          # Map both to:
    3ô        #  Split them into parts of size 3
      ʒ       #  Filter these parts by:
       '-¢   '#   Where the amount of "-"
          2@  #   Is larger than or equal to 2
      }g      #  After the filter: take the length to get the amount of items left
   }à         # After the map: get the maximum of the two
              # (which is output implicitly as result)

Kevin Cruijssen

Posted 2019-07-04T18:47:54.880

Reputation: 67 575

3

Ruby, 39 bytes

->s{s.scan(/(^|.)--|-.-|--(.|$)/).size}

Try it online!

G B

Posted 2019-07-04T18:47:54.880

Reputation: 11 099

38 bytes – Value Ink – 2019-07-05T21:52:05.350

2

Stax, 13 bytes

ƒó±KêyG←à╛Ωô∟

Run and debug it

recursive

Posted 2019-07-04T18:47:54.880

Reputation: 8 616

2

Java 8, 41 bytes

s->(0+s+10).split(".--|--.|-.-").length-1

Try it online.

Port of @Arnauld's JavaScript answer, except that +0 is +10 to fix test cases like ----. This is necessary because the String#split builtin in Java will remove trailing empty Strings by default. This can be changed by adding an additional parameter to the split builtin (which is 0 by default in the split-builtin with a single String argument). To quote the usage of this additional parameter from the docs:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
If the limit \$n\$ is greater than zero then the pattern will be applied at most \$n-1\$ times, the array's length will be no greater than \$n\$, and the array's last entry will contain all input beyond the last matched delimiter.
If \$n\$ is non-positive then the pattern will be applied as many times as possible and the array can have any length.
If \$n\$ is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Because of this, usually .split("...",-1) is used to retain ALL trailing empty Strings, and I could have used it for this answer as well (Try it online). In this case changing the +0 to +10 saves two bytes over the ,-1, though. :)

Kevin Cruijssen

Posted 2019-07-04T18:47:54.880

Reputation: 67 575

0

Jelly, 12 bytes

n⁶,Ż$s€3§Ẓ§Ṁ

Try it online!

Based on @KevinCrujissen’s 05AB1E answer so be sure to upvote him too.

Nick Kennedy

Posted 2019-07-04T18:47:54.880

Reputation: 11 829

0

Charcoal, 25 bytes

Pθ↖Fθ¿›№KM-¹«⊞υωM³→»→⎚ILυ

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

Pθ↖

Print the platform without moving the cursor, then move the cursor up and left as that is the first potential Jimmy position.

Fθ

Look for as many Jimmies as there are platform positions.

¿›№KM-¹

Check to see whether there is more than one piece of platform at this position.

«⊞υω

If so then note a valid Jimmy position...

M³→»

... and move three characters to the right so that the Jimmies don't overlap.

Otherwise the next potential Jimmy position is one character to the right.

⎚ILυ

Clear the platform and output the count of discovered positions.

Neil

Posted 2019-07-04T18:47:54.880

Reputation: 95 035

0

PowerShell, 38 bytes

Port of Arnauld's JavaScript answer.

(" $args "-split'.--|-.-|--.').count-1

Try it online!

mazzy

Posted 2019-07-04T18:47:54.880

Reputation: 4 832

0

Elm 0.19, 108 bytes

import Regex as R
f p=List.length<|R.find(Maybe.withDefault R.never<|R.fromString".--|-.-|--.")(" "++p++" ")

Based on the regex in Arnauld's JavaScript answer. Verify all test cases here.

Alternative solution without regex, significantly longer at 171 bytes:

f p=(String.foldl(\z{x,y,s,c}->let(t,d)=if s<1&&List.length(List.filter((==)'-')[x,y,z])>1 then(2,c+1)else(max 0 s-1,c)in{x=y,y=z,s=t,c=d}){x=' ',y=' ',s=0,c=0}(p++" ")).c

Verify all test cases here.

O.O.Balance

Posted 2019-07-04T18:47:54.880

Reputation: 1 499