# 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 – dingledooper 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. – lyxal 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. – Paŭlo Ebermann 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. – Surculose Sputum 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"? – Jonathan Allan 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. – Surculose Sputum 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 – QBrute 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"\(),. – Traws May 13 '20 at 13:30
• I'd argue that "v" is a char and ,"v" is a string of length 1, which works :) – streetster May 13 '20 at 20:01
• A fair point, indeed – Traws May 13 '20 at 20:09

# 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  # 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 # 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 – dingledooper 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? – Daemon Painter 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 :) – dingledooper May 9 '20 at 5:56

# 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!

# Rust, 67 bytes

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

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