> For the complete documentation index, see [llms.txt](https://anton-veselskyi.gitbook.io/codding-problems-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anton-veselskyi.gitbook.io/codding-problems-solutions/codewars/6-kyu/the-deaf-rats-of-hamelin.md).

# The Deaf Rats of Hamelin

### [The Deaf Rats of Hamelin](https://www.codewars.com/kata/598106cb34e205e074000031)

![](https://i.imgur.com/ta6gv1i.png?1)

## Story

The [Pied Piper](https://en.wikipedia.org/wiki/Pied_Piper_of_Hamelin) has been enlisted to play his magical tune and coax all the rats out of town.

But some of the rats are deaf and are going the wrong way!

## Kata Task

How many deaf rats are there?

## Legend

* `P` = The Pied Piper
* `O~` = Rat going left
* `~O` = Rat going right

## Example

* ex1 `~O~O~O~O P` has 0 deaf rats
* ex2 `P O~ O~ ~O O~` has 1 deaf rat
* ex3 `~O~O~O~OP~O~OO~` has 2 deaf rats

## Series

* [The deaf rats of Hamelin (2D)](https://www.codewars.com/kata/the-deaf-rats-of-hamelin-2d)

  \</span>

### Solutions

#### 🧠 C++

```cpp
int countDeafRats(const std::string &town)
{
    //until we found the PP all the rats that goes to the left is deaf
    //after we dound PP all that foes to the right
    bool found_PP = false;
    int deaf_counter  = 0;
    for(auto iter = town.begin(); iter != town.end(); iter++)
    {
        if(*iter == 'P') found_PP=true;

        //going right
        else if(iter[0]=='~' && iter[1]=='O') ++iter , deaf_counter+=found_PP;
        //going left
        else if(iter[0]=='O' && iter[1]=='~') ++iter , deaf_counter+=!found_PP;
    }

    return deaf_counter;
}
```
