DEV Community

Discussion on: Daily Challenge #136 - The Deaf Rats of Hamelin

Collapse
 
nickholmesde profile image
Nick Holmes

Thanks for the feedback. With a little inspiration from Avalander's Scala solution, I've got it what I had in mind in the first place.

let deafRats (s:string)=
    let rec inner s piper deaf =
        match s with
        | [] -> deaf
        | 'P' :: tail -> inner tail 1 deaf
        | 'O' :: '~' :: tail -> inner tail piper (deaf + 1 - piper)
        | '~' :: 'O' :: tail -> inner tail piper (deaf + piper)
        | _ :: tail -> inner tail piper deaf

    inner [for c in s -> c] 0 0
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
avalander profile image
Avalander

Nice math trick with the piper, hadn't thought of that!