# A Smart Lock

### Build a Lock

You all know what a combination lock is. While you can't probably build a mechanical one, it is fairly simple to simulate one in software.

Let's assume you have a combination lock where the unlocking combination is (10, 23, 7). To open the lock you must:

• Rotate clockwise (right) to 10
• Anticlockwise (left) to 23
• Clockwise past the first number to 7
• Pull to open

The lock you simulate in software will have to do something similar, except that there is no knob to turn right or left. Instead you will have to enter all this information from the keyboard.

Your program will read from standard input. Each line in the input is either a 3-tuple which contains

• direction to rotate: R or r for right (clockwise) and L or l for left
• number of times to turn in that direction (maximum 5)
• number to stop at; numbers on the lock's dial are between 0 and 24

or a line which contains the letter P or p indicating that you want to pull the lock to open.

For the number of times to turn you will use zero if less than a turn is needed to reach the number on the dial, one if you have to make more than a complete turn but less than two, and so on.

For the example above, the required input would then be:

```    R  0  10
L  0  23
R  1   7
P
```

Malformed input will be rejected with an indication of why it's wrong. Examples of wrong input include (but are not limited to):

```    K  0  10     *** Wrong direction
R  8  10     *** Maximum 5 turns!
R -2  10     *** Wrong number: -2
R  3  1.0    *** Wrong number: 1.0
```

The output of your program will be a line indicating whether the lock could be opened or not.

You will find what's the combination for your lock based on your Social Security Number as follows:

1. D8D7D6 -D5D4 -D3D2D1 D0 is your SSN
2. Right, D8 mod 6 times, stop at D7D6 mod 25
3. Left, D5 mod 6 times, stop at D4D3 mod 25
4. Right, D2 mod 6 times, stop at D1D0 mod 25

If your combination is impossible, like being at 6 and having to turn 0 times to reach 6 (which would be the equivalent of no move), then talk to your instructor to have a lock combination assigned to you.

### Break a Lock

Let's now assume that a possible intruder does not have any idea about your lock's combination, but he (should I say she to be PC?) wants to break it. One way to do it is to check all possible combinations. We assume that he knows the limitations of the lock (3-tuple, maximum number of turns is five, etc.). Another possible way to try to break the lock is to generate a random sequence of input strings to the device.

• Try to estimate how long it would take (on the average) to break the lock if one has to wait one second to see if the lock has unlocked or not. Assume the random number generator has an uniform distribution.
• Do the actual test: use some random number generator to generate the input strings for the lock. Count how many you have to try before the lock is unlocked. Ignore the waiting time in this case and have the program run at full speed. Since you use random numbers you will have to repeat the test several times to obtain significant results; find the minimum, maximum and the average.

## What you'll turn in

1. A well commented listing of your program (C or C++ is recommended but not required). Note that grading, as described below, considers both the functionality and the readability of your code. You may also want to have a look at a grading sheet used in the past for a similar assignment. If you are unsure about what the documentation of a program means, then you may want to read this document.
2. A floppy disk (3.5") which contains the source file(s), a DOS executable named comblock.exe and a README file indicating what the program does, how to build the executable, the platform (software and hardware) it's been tested on, and how to run it, plus any other information you consider useful - like the name of the author, disclaimers, etc.
3. A memo describing what you have done, how and your findings. In the memo clearly state what is the language of the FA(s) you have implemented. Also indicate what is the regular expression(s) corresponding to that language(s). The following will be attachments to the memo:
• context free grammar(s) that generates the same language(s)
• state transition diagram(s) for the FA(s)
• tables with experimental results

Direct the memo to your class instructor.

1. A mark between 0 and 10 for functionality. The mark basically indicates in what measure your program works properly and how well you have followed the initial specification.
2. A mark between 0 and 10 for readability. This mark indicates how well documented your program is; it also considers the general appearance of your final report.
3. Multiply the above two marks to get the mark for this programming assignment.

Example:

• You turn in your assignment three days earlier: a 3*5=15% bonus will be added to your assignment mark
• We find a single mistake in the functionality section and the penalty for that mistake is 0.5 points. Since you have turned the assignment three days earlier than the deadline, the penalty becomes 0.5*(1+3*5%)=0.5*(1+3*0.05)=0.575
• We calculate the mark by multiplying the marks for the functionality and readability; in your case the mark for functionality is 10-0.575=9.425, and the mark for readability is 10. The result is 9.425*10=94.25
• We add the bonus to this mark: 94.25+94.25*15%=108.38 which we round to 108. This is your final mark.

You may be asked to do a code review with your instructor.

## Varia

You don't need to be concerned about optimization(s). In particular you are not required to minimize the number of states in your FAs. You can do so if you want but this is not a must.

(c) 1997, Virgil Bistriceanu - virgil@csam.iit.edu
Posted: March 26, 1997. Last update: March 26, 1997