51
7
I have a crank-operated music box that can play a series of four notes. When I turn the crank, it plucks one of four strings, depending on the position of the crank and the direction of the turn. When the crank is turned due north, the box (with its strings numbered 1 through 4) looks like this:
1 | 2
|
O
4 3
From there, I can turn the crank clockwise to pluck the #2 string and point the crank east:
1 2
O---
4 3
Alternatively, I could have also turned the crank counterclockwise from north to play the #1 string and end with a crank pointing west:
1 2
---O
4 3
At any given time, then, the box can play one of two notes: the next note available in the clockwise direction or the next note in the counterclockwise direction.
Challenge
Your challenge is to write a program or function that accepts a non-empty string of note values (i.e., numerals 1
through 4
) and determine if it is ever possible to play that sequence of notes on the music box. Produce a truthy or falsy result to indicate the playability or non-playability of the input.
Some notes:
The input makes no assumptions about initial start position. The inputs
214
(starting east and moving strictly counterclockwise) and234
(starting north and moving strictly clockwise) and both valid.The crank may move freely in either direction after each note. A series of the same note is possible (e.g.,
33333
) by moving back and forth across one string. The series1221441
is perfectly playable (starting west, moving clockwise two steps, then counterclockwise three steps, then clockwise two steps).
Samples
Some true
cases:
1
1234
1221
3333
143332
22234
2234
22214
1221441
41233
Some false
cases:
13 (note 3 is never available after note 1)
1224 (after `122`, the crank must be north, so 4 is not playable)
121 (after `12` the crank is east; 1 is not playable)
12221 (as above, after `1222` the crank is east)
43221
Can the input be the string including quotes? – Luis Mendo – 2016-01-26T20:47:30.673
@LuisMendo Sure, I'll allow it -- I'm interested in your algorithm, not making you jump through hoops to get the input. Anyway, there's unofficial community consensus that it's generally okay: String input with or without “”?
– apsillers – 2016-01-26T20:51:48.3731I didn't know that. Thanks for the link! – Luis Mendo – 2016-01-26T20:52:28.703
Is there a limit to the maximum number of times a song will make me go all the way around? – AJMansfield – 2016-01-26T21:37:08.517
1@AJMansfield No, solutions should allow for arbitrarily many cycles. Of course, if some input causes your code to exceed a limit in your language's interpreter or your computer's memory, that's fine (since it's merely limited by however much memory you physically have or your interpreter allows), but your solution should not impose extra limitations on how far or how many times the crank moves. – apsillers – 2016-01-26T21:41:58.700
Test case:
234123412341234: True
– lirtosiast – 2016-01-27T06:25:14.613This problem reminds me of the code I wrote to play the Toyshop Trouble music (https://www.youtube.com/watch?v=miN90xEJh3s) on the Atari 2600. Each 8-beat measure uses a set of four melody notes, and every 32-beat phrase uses the same four sets; since I budgeted 256 bytes for music code and data (I actually went somewhat over) so the tune had to work with something like your four-note music box.
– supercat – 2016-01-28T17:05:45.0571
This challenge has won the Not as simple as it looks category in Best of PPCG 2016. Unfortunately, we can't give bounties to challenges, but Zgarb has written a challenge in your honour. Congratulations!
– Martin Ender – 2017-03-07T15:49:58.337