Skip to content

Instantly share code, notes, and snippets.

@rasher
Last active March 11, 2021 13:30
Show Gist options
  • Save rasher/f49c146de01ef0a374e48c2ed7f7c8f6 to your computer and use it in GitHub Desktop.
Save rasher/f49c146de01ef0a374e48c2ed7f7c8f6 to your computer and use it in GitHub Desktop.
"""
Copyright (c) 2018, Jonas Häggqvist <rasher@rasher.dk>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the program nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
A model of the Master Lock Speed Dial (1500iD) lock (also known as Master Lock One).
It will tell you the internal state of the lock, given a list of moves.
The utility of this is admittedly limited. I have used it to generate possible
sequences to find alternative sequences that will all unlock the lock. The
results are unsurprising and not particularly interesting.
Based on research and documentation by Michael Huebler:
https://toool.nl/images/e/e5/The_New_Master_Lock_Combination_Padlock_V2.0.pdf
"""
class Wheel:
def __init__(self):
self.N = 0
self.M = 0
def pin0(self):
if self.M in [0, 1]:
self._incN()
self.M = 0
def pin1(self):
if self.M in [1]:
self._incN()
self.M = 1
def pinminus1(self):
self._incN()
self.M = -1
def _incN(self):
self.N += 1
if self.N == 5:
self.N = 0
@property
def position(self):
pos = (self.N * 72) + (self.M * 24)
if pos > 360:
pos -= 360
if pos < 0:
pos += 360
return pos
def reset(self):
self.N = 0
self.M = 0
def __repr__(self):
return "{0.N}; {0.M} - Angle: {0.position}".format(self)
class Lock:
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
SYMBOLS = "▲▶▼◀"
def __init__(self):
self.wheels = [Wheel() for _ in range(4)]
self.moves = []
def reset(self):
self.moves = []
[wheel.reset() for wheel in self.wheels]
def state(self):
return tuple([wheel.position for wheel in self.wheels])
def moves_str(self):
return " ".join([self.SYMBOLS[move] for move in self.moves])
def __repr__(self):
return "Moves: " + self.moves_str() + "\n" + "\n".join([
"Wheel {0}: {1}".format(idx, wheel) for idx, wheel in enumerate(self.wheels)
])
def _act(self, a, b, c):
self.wheels[a].pin0()
self.wheels[b].pin1()
self.wheels[c].pinminus1()
def up(self):
self.moves.append(self.UP)
self._act(0, 1, 3)
def down(self):
self.moves.append(self.DOWN)
self._act(2, 3, 1)
def left(self):
self.moves.append(self.LEFT)
self._act(3, 0, 2)
def right(self):
self.moves.append(self.RIGHT)
self._act(1, 2, 0)
@david-miller
Copy link

This helped me understand the speedial more than anything because of the nice clean code and good OO. Thanks alot.

Now I will destroy the beauty by addding my own sloppy additions to it :)

thanks

@rasher
Copy link
Author

rasher commented Mar 9, 2021

Hi,

Happy it was helpful! I've updated the gist with license information (BSD).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment