As you probably know, there have been multiple lovely Jimmy challenges recently popping up. In these challenges, you were challenged with our beloved friend's acrobatics skills. Now we've got a different challenge for you. Today you will be identifying different types of Jimmys!
Explanation
There are three varieties of Jimmys: dwarf, acrobat, and bodybuilder.
This is dwarf Jimmy: o
This is acrobat Jimmy: /o\
This is bodybuilder Jimmy: /-o-\
These Jimmys are all great friends and they like to stand on the same line as each other. Your task is, given a Jimmy scene like so:
o /o\ o /-o-\/-o-\ o /o\
Output the amount of dwarves, acrobats, and bodybuilders on the line, respectively.
The challenge
- Take input in any reasonable form as a Jimmy scene, as shown in an example above.
The input string should be one line and optionally contains the three varieties of Jimmys and optional whitespace.
The string will not necessarily contain all of the Jimmy varieties or whitespace.
The string will not contain any characters not in
o/\ -.Any combination of Jimmy varieties is possible. This means that the same or different type of Jimmy can be next to each other. You must account for that.
Leading and trailing whitespace is optional and by no means required - your program should account for a string with or without leading and/or trailing whitespace.
The string should contain only valid Jimmys and whitespace. For instance,
---///---is not allowed because it is not a valid Jimmy sequence.
- Output three numbers: The count of dwarves, acrobats, and bodybuilders in the scene (in respective order).
This may be an output to the console as space-separated integers, or it may be a return value from a function as some sort of container (i.e an array type).
The output, in whatever format, must be ordered as mentioned in the top bullet above this rule.
- Standard rules and loopholes apply.
Test cases
/-o-\ /-o-\ o/o\ /-o-\ /-o-\ /-o-\
OUTPUT: 1 1 5
o o /o\ o o o /o\ /o\
OUTPUT: 5 3 0
/-o-\ /-o-\ /-o-\/-o-\ o /o\/o\ /-o-\o /-o-\ /o\/-o-\
OUTPUT: 2 3 7
/-o-\ o /-o-\ o/o\
OUTPUT: 2 1 2
If you'd like more test cases, use this tool to generate more random test cases.
Scoring
This is code-golf, so lowest score in bytes wins.
23 Answers 23
Python 3.8 (pre-release), 51 bytes
lambda s:((c:=s.count)('o')-c('/'),c('/o'),c('/-'))
-
\$\begingroup\$ FINALLY python added something like this. I've been waiting for a while for Python to allow assignments as expressions. A shame it takes an extra byte, but I'll take it :P \$\endgroup\$2019年07月15日 17:02:35 +00:00Commented Jul 15, 2019 at 17:02
-
\$\begingroup\$ @HyperNeutrino That's basically the one reason that you might now see "Python 3.8 (pre-release)" floating around the site. Another restriction is that, unless it's on its own and not the only expression in a statement (in which case you might prefer regular assignment instead), it must be parenthesized (+2 bytes). \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2019年07月15日 17:12:18 +00:00Commented Jul 15, 2019 at 17:12
-
\$\begingroup\$ How is the first count on the
('o')getting called without ac? \$\endgroup\$Quinn– Quinn2019年07月15日 17:23:36 +00:00Commented Jul 15, 2019 at 17:23 -
\$\begingroup\$ @Quinn The assignment expression assigns
s.counttocand then returns it. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2019年07月15日 17:59:19 +00:00Commented Jul 15, 2019 at 17:59 -
\$\begingroup\$ @ErikTheOutgolfer cool, TIL \$\endgroup\$Quinn– Quinn2019年07月15日 18:05:21 +00:00Commented Jul 15, 2019 at 18:05
Python 2, 50 bytes
x,y,z=map(input().count,'o/-')
print x-y,y-z/2,z/2
-10 bytes by converting lambda expression to a full program thanks to @xnor (removes the double-lambda nested thing and uses assignment instead)
-
3\$\begingroup\$ This is a nice method, and it's shorter as a program. \$\endgroup\$xnor– xnor2019年07月16日 05:49:01 +00:00Commented Jul 16, 2019 at 5:49
Jelly, (12?) 13 bytes
ċⱮ"-/o"H1¦ŻIṚ
A monadic Link accepting a list of characters which yields a list of integers, [ dwarves, acrobats, and body-builders] (save the Ṛ byte if we may specify our output)
How?
All Jimmys show a o; all non-dwarves show a /; all body-builders show two -. Count these up, halve the count of -, and perform subtraction to find the Jimmy counts:
ċⱮ"-/o"H1¦ŻIṚ - Link: list of characters
"-/o" - list of characters ['-', '/', 'o']
Ɱ - map across right with:
ċ - count occurrences = [n('-'), n('/'), n('o')]
¦ - sparse application...
1 - ...to indices: [1] -- i.e. n('-')
H - ...action: halve = [n('-')/2, n('/'), n('o')]
Ż - prepend a zero = [0, n('-')/2, n('/'), n('o')]
I - incremental differences
- = [n('-')/2, n('/')-n('-')/2, n('o')-n('/')]
Ṛ - reverse
- = [n('o')-n('/'), n('/')-n('-')/2, n('-')/2]
PowerShell, (削除) 59 (削除ここまで) 55 bytes
$c=,0*3
$args|sls '/?-?o'-a|% m*|% le*|%{++$c[$_-1]}
$c
Unrolled:
$counters=,0*3
$args|select-string '/?-?o'-AllMatches|% Matches|% Length|%{++$counters[$_-1]}
$counters
J, (削除) 36 (削除ここまで) 25 bytes
-11 bytes thanks to cole!
2-/0,円~1 1 2%~1#.'o/-'=/]
Original solution
J, 36 bytes
[:(-/@}:,-/@}.,{:)1 1 2%~1#.'o/-'=/]
Explanation:
'o/-'=/] compare the input with each one of "o/-" characters
/ the result is a 3-row matrix /
1#. add up each row to find the number of occurences
of each character, the result is a vector of 3 items
1 1 2%~ divide the last item by 2 to find the number of
bodybuilder Jimmys
[:( ) use the result to construct the following vector:
{: the last item
, appended to
-/@}. the difference of the second and the third items
, appended to
-/@}: the difference of the first and the second items
A sample J session:
a=:' /-o-\ o /-o-\ o/o\'
'o/-'=/a
0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1#.'o/-'=/a
5 3 4
1 1 2%~1#.'o/-'=/a
5 3 2
(-/@}:,-/@}.,{:)1 1 2%~1#.'o/-'=/a
2 1 2
-
1
-
-
1\$\begingroup\$ @cole Hah, as it often occurs to me, I didn't see the pattern. Thank you! \$\endgroup\$Galen Ivanov– Galen Ivanov2019年07月16日 07:09:10 +00:00Commented Jul 16, 2019 at 7:09
-
\$\begingroup\$ it's unfortunate I only saw it right as you were editing in that explanation - always a shame to lose that much effort of explaining. \$\endgroup\$cole– cole2019年07月16日 07:10:20 +00:00Commented Jul 16, 2019 at 7:10
-
\$\begingroup\$ @cole That's why I will keep the explanation of my blunder visible :) \$\endgroup\$Galen Ivanov– Galen Ivanov2019年07月16日 07:12:03 +00:00Commented Jul 16, 2019 at 7:12
Excel as CSV, 130 bytes
,=LEN(A3)-LEN(A4)
=SUBSTITUTE(A1,"-o",""),=(LEN(A2)-LEN(A3))/2
=SUBSTITUTE(A2,"/o",""),=(LEN(A1)-LEN(A2))/2
=SUBSTITUTE(A3,"o","")
Insert input in space before first ,, save as .csv, open in Excel.
Outputs Dwarfs, Acrobats and Bodybuilders in B1, B2 and B3 respectively.
Excel, 244 bytes
=LEN(SUBSTITUTE(SUBSTITUTE(A1,"-o",""),"/o",""))-LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-o",""),"/o",""),"o",""))&" "&(LEN(SUBSTITUTE(A1,"-o",""))-LEN(SUBSTITUTE(SUBSTITUTE(A1,"-o",""),"/o","")))/2&" "&(LEN(A1)-LEN(SUBSTITUTE(A1,"-o","")))/2
Kotlin (削除) 131 (削除ここまで) (削除) 130 (削除ここまで) (削除) 129 (削除ここまで) (削除) 121 (削除ここまで) (削除) 117 (削除ここまで) (削除) 97 (削除ここまで) (削除) 96 (削除ここまで) 88 bytes
fun String.j(b:Int=count{'-'==it}/2,a:Int=count{'/'==it})=listOf(count{'o'==it}-a,a-b,b)
Edit - Wew, got it under 100! I doubt I can shrink it more, but only time will tell...
Edit - Spoke too soon, dropped one more byte by using a list instead of a string
Edit - minus 8 bytes thanks to AsoLeo suggesting using a extension function
-
1\$\begingroup\$ Let me tell you about extension methods, my friend:
fun String.j(b:Int=count{'-'==it}/2,a:Int=count{'/'==it})=listOf(count{'o'==it}-a,a-b,b)88 bytes. \$\endgroup\$Aso Leo– Aso Leo2019年07月16日 14:01:54 +00:00Commented Jul 16, 2019 at 14:01 -
\$\begingroup\$ @AsoLeo nice, I had actually written it as an extension function originally but I must've messed something else up because mine was more bytes \$\endgroup\$Quinn– Quinn2019年07月17日 18:09:07 +00:00Commented Jul 17, 2019 at 18:09
Retina, (削除) 39 (削除ここまで) 35 bytes
Edit: -4 bytes thanks to @FryAmTheEggMan
^((o)|(/o.)|(/-o-.)| )*
$#2 $#3 $#4
Explanation:
A simple replace stage. It finds all matches of the regex ^((o)|(/o.)|(/-o-.)| )* (which should result in one match - the whole string) and replaces it by the number of captures of groups 2, 3, and 4. Here is the regex broken down:
^((o)|(/o.)|(/-o-.)| )*
^ start at the beginning of the string
( )* have any amount of Jimmy / spaces
| | | select one of:
(o) capturing group 2 - dwarf
(/o.) capturing group 3 - acrobat
(/-o-.) capturing group 4 - bodybuilder
We have to start with ^ or the end of the input counts as a match too.
In the substitution syntax of Retina, $n references the nth capturing group, and the modifier # counts how many matches it made.
-
\$\begingroup\$ You can save some bytes by escaping less, since the string is guaranteed to be only jimmys: Try it online! \$\endgroup\$FryAmTheEggman– FryAmTheEggman2019年07月15日 22:30:48 +00:00Commented Jul 15, 2019 at 22:30
Python 3, (削除) 69 (削除ここまで) (削除) 66 (削除ここまで) (削除) 60 (削除ここまで) 56 bytes
-4 bytes thanks to @Maarten Fabré
g=input().count
b,c=g('/'),g('/-')
print(g('o')-b,b-c,c)
-
1\$\begingroup\$ There is no need for x. You can do
g=input().countimmediately \$\endgroup\$Maarten Fabré– Maarten Fabré2019年07月16日 19:48:52 +00:00Commented Jul 16, 2019 at 19:48
JavaScript, 55 bytes
Searches the string using a regex pattern matching o, o-, or o-\; increments the corresponding count in an array, using the length of each match to determine the index.
s=>s.replace(/o-?\\?/g,m=>a[m.length-1]++,a=[0,0,0])&&a
-
1\$\begingroup\$ @JonathanAllan I rewrote my answer. \$\endgroup\$darrylyeo– darrylyeo2019年07月15日 17:26:38 +00:00Commented Jul 15, 2019 at 17:26
Clojure, 78 bytes
(defn ?[s](def c #(count(re-seq % s)))[(-(c #"o")(c #"/"))(c #"/o")(c #"/-")])
Wolfram Language (Mathematica), 63 bytes
Reverse@{a=(m=CharacterCounts@#)["-"]/2,b=m["/"]-a,m["o"]-a-b}&
55 bytes if the pointless order requirement is dropped...
-
\$\begingroup\$ @Xcali should now count dwarves at far left again \$\endgroup\$Expired Data– Expired Data2019年07月16日 08:10:48 +00:00Commented Jul 16, 2019 at 8:10
R, 63 bytes
Uses Regex matching to find and count the Jimmys.
library(stringr)
str_count(scan(,''),c('(?<![/-])o','/o','/-'))
Perl 5 -p, 41 bytes
$_=1*s/o(?!\\|-)//g.$".1*s|/o||g.$".y/o//
Counts the number of times o appears without being followed by \ or - to find the dwarves and removes them from the string. Then counts the number of times /o appears to find the acrobats and removes them from the string. Then counts the number of o remaining to determine the body builders. Inserts spaces between the numbers and implicitly outputs the result.
-
\$\begingroup\$ -1 byte \$\endgroup\$Nahuel Fouilleul– Nahuel Fouilleul2019年07月17日 05:49:31 +00:00Commented Jul 17, 2019 at 5:49
-
\$\begingroup\$ @NahuelFouilleul That doesn't work if there's a dwarf at the end of a line. It counts it as a body builder. \$\endgroup\$Xcali– Xcali2019年07月17日 19:27:36 +00:00Commented Jul 17, 2019 at 19:27
-
\$\begingroup\$ true, otherwise -4 bytes simply removing
1*\$\endgroup\$Nahuel Fouilleul– Nahuel Fouilleul2019年07月18日 07:40:06 +00:00Commented Jul 18, 2019 at 7:40 -
\$\begingroup\$ @NahuelFouilleul That doesn't work, either. If there are no examples of those types, it outputs nothing (
undef) instead of0. \$\endgroup\$Xcali– Xcali2019年07月18日 15:02:27 +00:00Commented Jul 18, 2019 at 15:02 -
\$\begingroup\$ ok, i just looked at the test cases \$\endgroup\$Nahuel Fouilleul– Nahuel Fouilleul2019年07月18日 15:09:11 +00:00Commented Jul 18, 2019 at 15:09
SNOBOL4 (CSNOBOL4), 135 bytes
I =INPUT
B I '-o' = :F(A)
B =B + 1 :(B)
A I '/o' = :F(D)
A =A + 1 :(A)
D I 'o' = :F(O)
D =D + 1 :(D)
O OUTPUT =+D ' ' +A ' ' +B
END
Removes -o, /o, and o from the string and increments the appropriate counters each time. Leaves behind a lot of arms and legs (/-\, \, and nothing).
Forth (gforth), 118 bytes
: c -rot 0 tuck do over i + c@ 3 pick = - loop nip nip ;
: f 2dup '/ c >r 2dup '- c 2/ -rot 'o c i - . r> over - . . ;
Explanation
- Get Count of
/,-, andocharacters - Bodybuilder is number of
-characters divided by 2 - Acrobat is number of
/characters minus the number of body builders - Dwarf is number of
ocharacters minus the number of Acrobat and Bodybuilders
Code Explanation
\ c counts the number of occurrences of the given character in a string
\ stack usage is ( c-addr u1 w1 - u )
: c \ start a new word definition
-rot 0 tuck \ sets up parameters for a counted loop
do \ loop from 0 to string-length - 1 (inclusive)
over i + \ get the address of the current character in the string
c@ \ get the ascii value of the current character
3 pick = \ compare it to the character we're counting
- \ subtract result from the accumulator (subtract because -1 = true in forth)
loop \ end the loop
nip nip \ remove extra values from the stack
; \ end the word definition
\ Main function
: f \ start a new word definition
2dup \ duplicate the string address and length
'/ c >r \ count the number of '/' characters and stick the result on the return stack
2dup '- c 2/ \ count the number of '-' characters and divide by 2
-rot 'o c \ move the string to the top of the stack and count the number of 'o characters
i - . \ calculate number of dwarf jimmy's and print
r> over - . \ calculate number of acrobat jimmy's and print (drop '/' count from return stack)
. \ print number of body-builder jimmy's
; \ end word definition
05AB1E, 13 bytes
...-/oS¢ć;š0š\R
This one could be 12 bytes by removing the R if an output-order of [bodybuilder, acrobat, dwarf] would have been allowed.
Try it online or verify all test cases.
Minor equal-bytes alternative:
...-/oS¢R`;0)üα
Try it online or verify all test cases.
Explanation:
...-/o # Push string "-/o"
S # Split to a list of characters: ["-","/","o"]
¢ # Count the occurrence of each character in the (implicit) input-string
ć # Extract the head; pop and push head and remainder-list
; # Halve this head
š # And prepend it back in front of the remainder-list
0š # Then also prepend a 0
\ # Get the deltas (forward differences)
R # And reverse the list to get the required order of output-counts
# (after which the result is output implicitly)
...-/oS¢ # Same as above
R # Reverse this list
` # Pop the list and push its values separately to the stack
; # Halve the top value on the stack
0 # Push a 0
) # Wrap all values on the stack into a list
ü # For each overlapping pair of values:
α # Get the absolute difference between the two values
# (after which the result is output implicitly)
C (gcc), (削除) 99 (削除ここまで) (削除) 98 (削除ここまで) 96 bytes
-1 byte thanks to ceilingcat
o,S,d,c;f(char*s){for(o=S=d=0;c=*s++;o+=c>93)d+=c==45,S+=c==47;printf("%d %d %d",o-S,S-d,d/=2);}
Ly, 87 bytes
0>0>0>i['-=[pl'o=[p<1+>0]pp0]p'/=[p<<1+>>0]p'o=[p<<<1+>>>0]psp]<s<l-:l+sp<l-u>' ou>' ou
I think this could be shorter, but I've hit a brick wall at this point.
At a high level, it counts the number of - (matched), / and o characters, then subtracts to remove duplicate counts before printing the results.
First is initializes counters to 0 on three different stacks.
0>0>0>
Then it reads the input onto the stack a codepoints, and loops over each character. The code also saved the current character to the backup cell with s so we can only count matched - characters.
i[ ... sp]
The first check increments the counter on the first stack if it's - and the previous character was o. That works since the characters are processes in reverse order.
'-=[pl'o=[p<1+>0]pp0]p
The second check increments the counter on the second stack anytime the code hits a / character.
'/=[p<<1+>>0]p
And the last check increments the counter on the third stack when the code finds a o character.
'o=[p<<<1+>>>0]p
Once all the input has been scanned and counted, to code subtracts the number of matched - characters (bodybuilders) from the number of / characters to find the number of acrobats.
<s<l-
Then it remember the number of acrobats plus the number of bodybuilders by stashing the sum of the two in the backup cell.
:l+sp
Then is subtracts that from the number of o characters to get the final counter and prints it.
<l-u
The last bits just shift to the next counter and prints with a space appended. >' ou
And does the same shift/print to output the last counter.
>' ou
oo /o\ o owould be nice \$\endgroup\$oo. \$\endgroup\$