47
The scenario
After a long day's work slogging in the office and browsing stackexchange.com, I finally walk out the door at 16:58, already weary with the day. Because I am still only an intern, my current mode of transportation is on bicycle. I head over to my trusty Peugeot Reynolds 501, but before I can sail away on it, I need to unlock it. The lock is a standard four digit combination lock (0-9), through the frame and the front wheel. As I try to stay awake, I pull my hand up to enter in the combination.
The challenge
Because my fingers are so weary, I want to turn the lock to the correct combination with the fewest movements. One movement is defined as a rotation by one position (36 degrees), for example there is one movement from 5737
to 5738
. However, I am able to grasp up to any three consecutive rings at the same time, and rotate them as one, which only counts as a single movement. For example there is also only one movement from 5737
to 6837
or to 5626
. Moving from 5737
to 6838
is not one movement, as digits number 1,2 and 4 have moved in the same direction, but independently of digit number 3.
Therefore, for a given combination I can see on the bike lock (any 4-digit integer), what is the lowest number of movements I can make to get it unlocked, and yes, I can rotate in either direction at any time. By this I mean that I can turn some digits in one direction and other digits in the other direction: not all of my movements will be anitclockwise or clockwise for each unlock.
Because I am lazy, my unlock code is 0000.
This is code golf I can't be bothered writing much code, so the shortest program in number of bytes wins.
Input is from stdin, and your code should output the combinations I can see at each step after each movement, including the 0000 at the end. Each of the combinations output should be separated by a space/newline/comma/period/ampersand.
Examples
Input: 1210
0100
0000
Input: 9871
9870
0980
0090
0000
Input: 5555
4445&3335&2225&1115&0005&0006&0007&0008&0009&0000
Input: 1234
0124 0013 0002 0001 0000
I tried posting this on http://bicycles.stackexchange.com, but they didn't like it...
Disclaimer: First golf, so anything that is broken/any missing information let me know! Also I did all the examples by hand, so there may be solutions which involve less movements!
EDIT: For answers which have multiple solution paths with equal number of movements (practically all of them), there is no preferred solution.
19Welcome to PPCG; very nice first challenge! – Doorknob – 2016-01-11T23:34:06.540
4This looks solid to me! Welcome to PPCG! – Mego – 2016-01-11T23:34:12.613
[tag:path-finding] is an oddly relevant tag. – Addison Crump – 2016-01-12T09:22:53.863
1Nice challenge. Can i ask what should be the output for cases : 7478 and 3737? – noisyass2 – 2016-01-12T10:48:09.667
It's worth noting that if your lock had real numbers written on them and we measured effort by how far we turned a combination, the answer would be writable as the minimum of the dot product of your initial state with a (finite) set of vectors. – Milo Brandt – 2016-01-12T18:55:51.230
1@noisyass2 Thanks; flawr's answer gives the following: 7478 8588 9698 0708 0808 0908 0008 0009 0000
and 3737 2627 1517 0407 0307 0207 0107 0007 0008 0009 0000
Just looking at the 3737, this makes sense: Looking at the first 3 digits only: If I move all of the first three at the same time, it takes 3 movements for digits 1 and 3, and then another 4 movements for digit 2, thus a total of seven. Whereas if I move each individually, each takes 3 moves, thus 9 movements. – Lui – 2016-01-12T20:14:34.043
1I'm wondering whether the title "Combination Lock" (or "Bike Lock") might attract more viewers. – DavidC – 2016-01-12T20:36:32.910
1I support @DavidC 's suggestion! PS: Could you please add
4826
or6284
as test cases? These do need 12 steps, and are (according to my program) the ones who need the greatest minimum number of moves. – flawr – 2016-01-12T20:58:58.447Couldn't decide between "Combination Lock" and "Bike Lock", so I just used both.... And flawr I don't have a working solution, but I've tried that input with a few different methods, and 12 is the best I can find; Either just moving each individually (4 + 2 + 2 + 4 = 12), or digits 1,2,3 at the same time (2), then digits 1,2 (2), then digit 2 (4), and then digit 4 (4), sum = 12. If you follow? – Lui – 2016-01-12T21:27:10.103
I would not use a so weak lock on a bike of mine! Minimum should be a U-Lock. The one here is very easy to cut with a n handheld bolt cutter! – sergiol – 2017-11-19T23:04:26.337