Wie viele Jimmys können passen?

29

Bei dieser einfachen, aber unterhaltsamen Herausforderung mussten Sie feststellen, ob Jimmy von ihrer Plattform fallen würde. Jimmy hat drei Körperteile /, ound \wie diese angeordnet sind

/o\

Plattformen sind mit vertreten -. Jimmy fällt von der Plattform, wenn sich zwei oder mehr Körperteile nicht direkt über einer Plattform befinden.

Einige Beispiele:

   /o\
- -------

Jimmy gleicht aus, da alle Körperteile über a liegen -.

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

Jimmy wird ausgeglichen, da zwei Körperteile über -s liegen.

 /o\
-- ----  --

Jimmy gleicht aus, obwohl sie auf zwei Plattformen aufgeteilt sind

  /o\
   -

Jimmy wird nicht ausbalanciert, da sich zwei Körperteile nicht über einer Plattform befinden.


Ihre Aufgabe ist es, ein Programm zu schreiben, das eine Plattform als einen längeren Container mit nur -s und s (z. B. einem String) betrachtet und die Anzahl der Jimmys ausgibt, die auf der Plattform so platziert werden können, dass keiner von ihnen herunterfällt und keiner von ihnen herunterfällt Überlappung. Ein Jimmy kann einen seiner Körperteile links vom Anfang der Saite oder rechts vom Ende der Saite haben.

Dies ist daher werden Antworten in Bytes bewertet, wobei weniger Bytes das Ziel sind.

Testfälle

Eingänge

-  -  -

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

Entsprechende Ausgänge

0
0
1
1
2
2
2
3
Weizen-Assistent
quelle

Antworten:

15

JavaScript (ES6),  45 41  40 Byte

4 Bytes gespart dank @Shaggy

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

Probieren Sie es online!

Arnauld
quelle
1
41 Bytes
Shaggy
7
@ Shaggy Danke! Ich wusste, dass dort etwas nicht stimmte, aber ich musste meiner Frau in der Zwischenzeit auf einem Super Mario Galaxy Level helfen ... und es war auch ein Problem mit kaputten Plattformen. : p
Arnauld
2
Die ---saßen auch nicht gut bei mir, bis ich an meinem Hafen arbeitete und merkte, dass sie nicht gebraucht wurden. Ich nehme an, ich nenne es einen Tag hier, schnappe mir eine Tüte Dosen und schmeiße mir selbst SMG drauf - habe es schon lange nicht mehr gespielt.
Shaggy
Und jetzt verwenden alle anderen Antworten den gleichen regulären Ausdruck.
Cœur
8

Python 2 , 53 Bytes

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

Probieren Sie es online!

Basierend auf Arnauld's Regex . Sucht gierig nach allen nicht überlappenden Teilzeichenfolgen der Länge 3 mit zwei oder mehr -. Ein Trick besteht darin `s`, die Eingabezeichenfolge in Anführungszeichen zu setzen, damit Jimmys an beiden Enden Platz hat

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

Python 2 , 57 Bytes

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

Probieren Sie es online!

Erfordert ein kitschiges E / A-Format der Eingabe, die bereits in Anführungszeichen steht. Ausgänge Falsefür 0.

Eine rekursive Funktion, mit der jeder Jimmy ganz links platziert werden kann, indem entweder Jimmy über die ersten drei Zeichen platziert wird, wenn sie Jimmy halten können, oder das erste Zeichen auf andere Weise gelöscht wird. Ein netter Trick ist, zu überprüfen, ob s[:3]zwei oder mehr -Dateien enthalten '--'in s[:3]*2, indem zwei Kopien von verkettet werden s[:3]und nach zwei benachbarten gesucht wird -.

xnor
quelle
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
quelle
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
quelle
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
quelle
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 n1 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
quelle
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
quelle
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
quelle