Yesterday, I bought a harmonica:
Figure 1: The harmonica.
However, my dreams of being able to play soulful blues harmonica that moves people and causes a grown man to cry were quickly dashed by two problems:
- The harmonica can only play certain notes;
- I am depressingly bad at playing the harmonica.
Despite my lack of skill at the harmonica, there are still some songs that I can play on it. However, it is not immediately obvious if I am able to play some piece of music on the harmonica or not. Given the notes of a piece of music, write a program to determine if I could play it on my harmonica or not.
As the above picture shows, my harmonica has ten holes in it. With each hole, I can either exhale into it or inhale into it -- the hole that I choose, and whether I inhale or exhale into it changes the pitch of the resulting sound. Each hole has a different pitch on exhaling and inhaling, but there are some combinations that result in the same note. Overall, my harmonica can play 19 unique different pitches. The pitches are presented in musical scientific notation - the letter represents the note, and the number represents which octave it is in.
Hole Breathing Note
1 Exhale C4
1 Inhale D4
2 Exhale E4
2 Inhale G4
3 Exhale G4
3 Inhale B4
4 Exhale C5
4 Inhale D5
5 Exhale E5
5 Inhale F5
6 Exhale G5
6 Inhale A5
7 Exhale C6
7 Inhale B5
8 Exhale E6
8 Inhale D6
9 Exhale G6
9 Inhale F6
10 Exhale C7
10 Inhale A6
For example, if I exhaled on hole 3, I'd get a G4
note. If I inhaled on hole 2, I'd also get a G4
note. If I exhaled on hole 7, I'd get a C6
.
When I breathe into the harmonica, apart from exhaling or inhaling, I can also choose whether to breathe thinly or widely. Breathing thinly causes only one hole to sound, whereas breathing widely causes one hole and both holes on either side of that hole to sound. I do not have the embouchure skills to blow onto two holes - it is either one or three.
For instance, if I thinly exhaled onto hole 4, only hole 4 would sound, so I would get a C5 sound. If I widely exhaled onto hole 4, holes 3, 4, and 5 would sound, and I'd get a G4,C5,E5 chord. If I widely inhaled onto hole 4, holes 3, 4, and 5 would sound but they would play the inhale pitches instead, which results in a B4,D5,F5 chord. Note that for the holes on either end, if I breath widely into them only two holes would sound (because there is no hole 0 or hole 11).
However, I cannot inhale and exhale at the same time. For instance, I could exhale into holes 4, 5, and 6 to result in the notes C5, E5, and G5 sounding at the same time, forming a chord. However, I can't inhale and exhale at the same time, so it would be impossible for me to play the chord C5,F5,A5 as I'd have to somehow exhale in hole 4 and inhale in hole 5 and 6. If this is stil unclear, this comment thread might be useful.
The input is the notes of the music. The notes are notated in the same way they are above in the table, and they are comma separated. Notes wrapped in curly brackets represent a chord. For instance:
C4,D4,G4,{D5,F5,A5},B5
This means, "C4, then D4, then G4, then D5, F5, and A5 at the same time, then B5." Your program will take a string in this format as input and output True
if it is possible for me to play the music on my harmonica, or False
otherwise. For sample inputs and outputs, the example above should output True
. The input {C5,F5,A5}
on the other hand will output False
.
This is code golf, so the shortest entry wins.
Here are some test cases:
Input (A C Major scale):
C4,D4,E4,F4,G4,A4,B4,C5
Output:
False
(because the harmonica cannot play F4 or A4)
Input (the opening 2 bars of Let It Go):
E6,F6,A5,E6,F6,F6,E6,A5,F6,E6
Output:
True
Input:
{E6,G6,F6}
Output:
False
Input:
{G4,C5,E5},{F5,A5,B5}
Output:
True
You may assume that chords will come in lower to higher pitch order.