# Mowing the Grass

## Context

After attempting to program in Grass for the entire morning, you decide to go outside and mow some real grass. The grass can be viewed as a string consisting exclusively of the following characters: wWv. w denotes tall grass which takes $$\ 1 \$$ unit of energy to mow. W denotes extremely tall grass which takes $$\ 2 \$$ units of energy to mow. Lastly v denotes short grass which does not need to be mowed.

You decide to mow the grass from left to right (beginning to the end of the string). However, every time you encouter a v (short grass), you stop to take a break to replenish your energy, before carrying on with the mowing. Your task is to calculate the maximum amount of energy expended while mowing. In other words, find the maximum total energy of mowing a patch of grass, that of which does not contain v.

### Example

In the example input below, the answer is $$\ 8 \$$. Although the patch wwwwwww is a longer patch of grass, it only costs $$\ 7 \$$ units of energy, whereas the optimal patch WWWW expends $$\ 2 \times 4 = 8 \$$ units of energy.

Input: WwwvWWWWvvwwwwwwwvWwWw
Output: 8


Here is an example Python program -> Try It Online!.

## Test Cases

WwwvWWWWvvwwwwwwwvWwWw -> 8
w -> 1
W -> 2
vwww -> 3
vWWW -> 6
v -> 0
vvvvvvv -> 0
vwvWvwvWv -> 2
vWWWWWWWWWWvwwwwwwwwwwwwwwwwwwwwwv -> 21
vWWWWWWWWWWvwwwwwwwwwwwwwwwwwwwv -> 20
vvWvv -> 2


This is , so the shortest code in bytes wins!

• @Shaggy Only the 3 characters I've mentioned are allowed, otherwise it may not look like grass :P May 7 '20 at 20:14
• Mowing the Grass, to stop it from Growing the Mass!
– xnor
May 7 '20 at 21:31
• As someone who has actually mowed quite a bit of grass in my life, I'd say this checks out. May 7 '20 at 23:52
• Can anyone do it in Grass?
– user92069
May 8 '20 at 0:42
• Hmm, v doesn't look much shorter than w. May 8 '20 at 9:01

# Python 2, 49 bytes

lambda s:len(max(s.replace("W","ww").split('v')))


Try it online!

Here's how it works:

1. Replace each W with ww
2. Split on v's to produce chunks of consecutive w's
3. Take the max to get the longest one
4. Get its length

# Python 3.8, 55 46 44 bytes

Thanks @xnor for saving 2 bytes!
Thanks @dingledooper for saving 2 bytes!

lambda s,t=0:max((t:=c%2*t+c%5%3)for c in s)


Try it online!

A function that takes in a byte string, and returns the max energy.

Cool trick with eval.

## Python 2, 61 bytes

lambda s:max(eval("+".join(s+"v").replace("v","0,")))
w=1;W=2


Try it online!

Transforms the given string by adding + between characters, then replaces v with comma (e.g wWvW -> w+W+0,W).

• Wow, that is some crazy eval! For the 3.8 one, it looks like you can do stuff-~t for t+1+stuff, or directly express t:=t+[2,-t,1][ord(c)%3] since it happens the three chars are distinct mod 3.
– xnor
May 7 '20 at 20:44
• @xnor Thanks! I was reluctant to go down the mod chain route, but it seems like everything lines up perfectly this time. May 7 '20 at 21:14
• Oh wow, your mapping the mod to the value is super clean too.
– xnor
May 7 '20 at 21:16
• Is it a default that we can accept a list of integers/bytes when it states "string"? May 8 '20 at 1:01
• @JonathanAllan I'm not sure myself, but I've seen people done this before. I think this should be allowed, since Python bytes quacks like a string: gets printed like a string, and supports all string operations. Here is a demonstration. May 8 '20 at 1:11

# Jelly,  9  8 bytes

O’%3ṣ0§Ṁ


Try it online!

### How?

O’%3ṣ0§Ṁ - Link: list of characters   e.g. "wwwvWvwWww"
O        - ordinals                        [119,119,119,118,87,118,119,87,119,119]
’       - decremented                     [118,118,118,117,86,117,118,86,118,118]
%3     - modulo three                    [1,1,1,0,2,0,1,2,1,1]
ṣ0   - split at zeros                  [[1,1,1],[2],[1,2,1,1]]
§  - sums                            [3,2,5]
Ṁ - maximum                         5

• Wow, works out perfectly with modulo May 8 '20 at 8:20

# JavaScript (Node.js), 50 bytes

s=>Math.max(...Buffer(s).map(c=>e+=c%5%3||-e,e=0))


Try it online!

Or 42 bytes if we can take a Buffer (or an array of ASCII codes) as input:

s=>Math.max(...s.map(c=>e+=c%5%3||-e,e=0))


Try it online!

# K (oK), 1918 13 bytes

Solution:

|/+/'3!5!"v"\


Try it online!

Explanation:

Might be further golfable...

|/+/'3!5!"v"\ / the solution
"v"\ / split on "v"
5!     / modulo 5 (turns "vwW" into 3 4 2)
3!       / modulo 3 (turns 3 4 2 into 0 1 2)
+/'         / sum (+/) each (')
|/            / take maximum

• The split fails on the single char test cases. Perhaps you should do |/+/'3!5!"v"\(),. May 13 '20 at 13:30
• I'd argue that "v" is a char and ,"v" is a string of length 1, which works :)
– mkst
May 13 '20 at 20:01
• A fair point, indeed May 13 '20 at 20:09

# Charcoal, 12 bytes

Ｉ⌈Ｅ⪪Ｓv⁺Ｌι№ιW


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

    Ｓ           Input string
⪪ v          Split on literal v
Ｅ             Map over chunks
ι     Current chunk
№ W    Count of literal W
⁺         Plus
Ｌι       Length of current chunk
⌈              Maximum
Ｉ               Cast to string for implicit print


# PowerShell 6+ for Windows, 48 bytes

port of math junkie's answer for Retina.

p$F.map{|w|w.gsub(?W){11}.size}.max  Try it online! # Jelly, 9 bytes ṣ”v<”w‘§Ṁ  Try it online! # Explanation ṣ”v<”w‘§Ṁ Main Link ṣ”v Split on "v" <”w Check if less than "w" ("w" = 0, "W" = 1) ‘ Add one to every element § Sum every sublist Ṁ Find the maximum  # AWK, 85 82 69 bytes {FS="v";gsub("W","ww");for(;i<NF;){s=length($++i);m=m>s?m:s;}print m}


Try it online!

Thanks to @dingledooper for cutting off 13 bytes.

The code could be even less, 62 bytes accounting for the field separator flag being a parameter and not part of the actual code.

pretty straightforward: change all W into w, get lenght of words, v counts as field separator. Retain max score with ternary operator.

• A couple basic golf ideas for 69 bytes May 9 '20 at 5:45
• Thanks @dingledooper, very nice touch! I'm not familiar with the rules, but apparently I can put the -F"v" as argument and further reduce the total number of bytes. Is that a valid move? May 9 '20 at 5:50
• Yes you are allowed to add arguments, just make sure you include it in the header. Personally, I rarely use flags, since it seems a bit of a "cheap" golf, but it's up to you :) May 9 '20 at 5:56

# Java (JDK), 62 bytes

s->{int m=0,n=0;for(var c:s)m=(n=c%2*n+c%5%3)>m?n:m;return m;}


Try it online!

After finishing it, it seems to be a port of Noodle9's C answer.

# C# (Visual C# Interactive Compiler), 69 bytes

Func<string,int>g=s=>s.Replace("W","ww").Split('v').Max(x=>x.Length);


Try it online!

Heavy inspiration from @xnor

# Bash, 28 bytes

sed 's,W,ww,g;y/v/\n/'|wc -L


Input from stdin, output to stdout

There might be a better way.

Try it online!

# Mathematica, 53 bytes

Max[#~(s=StringCount)~"W"+#~s~_&@StringSplit[#,"v"]]&


Max[StringCount[#,"W"]+StringCount[#,_]&[StringSplit[#,"v"]]]&


StringSplit[#,"v"] takes a string and turns it into a list of strings, seperated by 'v', then add together the length of the string, and the count of 'W's, and take the max value.

# Crystal, 57 bytes

def f(a);a.split('v').max_of {|s|s.count('W')+s.size};end


Try it online!

# Python 2, 70 bytes

Comprehensive approach.

lambda n:max([sum([2if i=="W"else 1for i in x])for x in n.split('v')])


Try it online!

# Rust, 67 bytes

|s:&str|s.replace("W","ww").split('v').map(str::len).max().unwrap()


Try it online!

# Stax, 14 bytes

π├♦.8Σ0└ó?∟X≈Æ


Try it online!

# Pip, 13 bytes

MX#*YaR'Wt^'v


Try it online!

### Explanation

     a         First command-line argument
R'W      Replace all occurrences of W
t     with 10 (arbitrary value--the important thing is, it's two characters)
^'v  Split on occurrences of v
Y          Yank (to enforce precedence)
#*           Replace each string in the list with its length
MX             Take the maximum


# Excel, 101 bytes

=MAX(LEN(SUBSTITUTE(FILTERXML("<a><b>x"&SUBSTITUTE(A1,"v","</b><b>x")&"</b></a>","//b"),"W","ww"))-1)


• "<a><b>x"&SUBSTITUTE(A1,"v","</b><b>x")&"</b></a>" converts text to XML containing "x" + the "wW" strings. The additional "x" prevents errors.
• FILTERXML(~,"//b") converts XML to a vertical array.
• SUBSTITUTE(~,"W","ww") change "W" to "ww".
• LEN(~)-1 counts the "w"; minus 1 for the leading "x".
• MAX(~) return the largest one.