r/dailyprogrammer • u/nint22 1 2 • Jan 13 '14
[01/13/14] Challenge #148 [Easy] Combination Lock
(Easy): Combination Lock
Combination locks are mechanisms that are locked until a specific number combination is input. Either the input is a single dial that must rotate around in a special procedure, or have three disks set in specific positions. This challenge will ask you to compute how much you have to spin a single-face lock to open it with a given three-digit code.
The procedure for our lock is as follows: (lock-face starts at number 0 and has up to N numbers)
- Spin the lock a full 2 times clockwise, and continue rotating it to the code's first digit.
- Spin the lock a single time counter-clockwise, and continue rotating to the code's second digit.
- Spin the lock clockwise directly to the code's last digit.
Formal Inputs & Outputs
Input Description
Input will consist of four space-delimited integers on a single line through console standard input. This integers will range inclusively from 1 to 255. The first integer is N: the number of digits on the lock, starting from 0. A lock where N is 5 means the printed numbers on the dial are 0, 1, 2, 3, and 5, listed counter-clockwise. The next three numbers are the three digits for the opening code. They will always range inclusively between 0 and N-1.
Output Description
Print the total rotation increments you've had to rotate to open the lock with the given code. See example explanation for details.
Sample Inputs & Outputs
Sample Input
5 1 2 3
Sample Output
21
Here's how we got that number:
- Spin lock 2 times clockwise: +10, at position 0
- Spin lock to first number clockwise: +1, at position 1
- Spin lock 1 time counter-clockwise: +5, at position 1
- Spin lock to second number counter-clockwise: +4, at position 2
- Spin lock to third number clockwise: +1, at position 3
12
u/[deleted] Jan 13 '14
Your specification has a bug (probably a typo) and what looks to me like an ambiguity.
You probably mean 0, 1, 2, 3 and 4 :-)
If the first digit is 0, does this mean 2 full spins, or 3 full spins? "Continue" suggests 3, to me, but 2 makes more sense so I guess it's probably that.
17
u/5outh 1 0 Jan 13 '14 edited Jan 13 '14
Here's a solution:
because:
2n + xnumbersnnumbers plus subtracting the second number from the first, modnnIn general, moving clockwise from
xtoy(without looping) goes overx - y (mod n)numbers, and moving counterclockwise goes overy - x (mod n)numbers.My solution is just a (haskell) function that represents the above, cleaned up a little bit mathematically.
Edit: if x == y, n totally needs to be added once more. It's uglier now. But thank you /u/MoldovanHipster for catching this!
7
u/dongas420 Jan 14 '14
Perl:
6
u/Edward_H Jan 14 '14
A solution in COBOL:
+/u/CompileBot COBOL
Input:
5
u/razeal113 Jan 13 '14
python
4
u/thestoicattack Jan 13 '14 edited Jan 13 '14
bash:
4
u/Coder_d00d 1 3 Jan 13 '14 edited Jan 14 '14
C
Edit: added 1 line to handle when the 2nd and 3rd number are same - it will do 1 extra spin and not just sit on the number.
4
u/sinemetu1 Jan 15 '14
Clojure:
+/u/CompileBot Clojure
4
u/JimRaynorInADress Jan 15 '14 edited Feb 14 '14
python:
4
u/angryninjaduck Jan 17 '14
Java
10
u/chunes 1 2 Jan 14 '14 edited Jan 14 '14
I took a look at this problem and decided that a circular doubly-linked list would be a neat way to represent the dial. Why? Because it's fun and I'm probably a little bit insane. Once I have my ADT set up, solving the problem is almost brainlessly easy. All I do is turn the dial. I don't have to worry about number-wrapping or any other such trivialities.
CombinationLock.java
CircularDoublyLinkedList.java
Node.java
3
u/ooesili Jan 14 '14
C99 solution. I made sure to count a full spin if there are two numbers in a row, like from 3 to 3.
3
u/phantomesse Jan 14 '14
My solution in OCaml.
3
u/__robin__ Jan 16 '14
C
3
u/lukz 2 0 Jan 21 '14
BASIC for 8-bit computers
4
u/Rekvijem Jan 13 '14
My solution in python2.7:
2
u/SeaCowVengeance 0 0 Jan 14 '14
Ruby:
+/u/CompileBot Ruby
Input:
2
u/MoldovanHipster Jan 14 '14
c++
first time I've touched the language in a while (and first post)
2
u/nowne Jan 14 '14
Haskell:
2
u/pbl24 Jan 14 '14
Arrived at the same basic solution as 5outh. Python 2.7:
2
u/slackertwo Jan 14 '14
Ruby:
Output:
2
u/goofolph Jan 14 '14
Just a note, why are the numbers listed counter-clockwise? I've always seen them listed clockwise, and I'm unable to find one online that goes counter-clockwise.
2
u/ReginaldIII Jan 15 '14 edited Jan 15 '14
Cheeky Matlab one-liner
2
u/CaraCaraOrange Jan 15 '14 edited Jan 15 '14
My solution in Python, apprently it works for 5 1 2 3, and for 5 0 0 0 giving 21 and 20 respectively. Any constructive criticism is appreciated.
+/u/CompileBot Python
Input:
2
u/[deleted] Jan 16 '14
New to C++, here goes nothing:
2
u/[deleted] Jan 16 '14 edited Jan 16 '14
Here's my OO python solution with error checking, do I win longest entry? :)
2
u/Kristian_dms Jan 16 '14
Python 2.7
or more compact:
2
u/try_username Jan 17 '14 edited Jan 18 '14
Nimrod:
+/u/CompileBot Nimrod
Input:
2
u/munkyeetr Jan 18 '14
C# one-liner:
step-by-step:
2
u/AultimusPrime Jan 18 '14
One line of python, is this too simple?
2
u/ihatemorningpeople Jan 18 '14
Obfuscated Perl 1-liner:
+/u/CompileBot perl
Input:
2
u/drguildo Jan 19 '14 edited Jan 19 '14
+/u/CompileBot java
2
u/i4X-xEsO Jan 19 '14
python3
2
u/ghuge32 Jan 20 '14 edited Jan 21 '14
Scala w/ Implicit: (had to poke around here for the equation to use since I'm at about a 3rd grade level when it comes to turning words into equations, equation came from /u/ReginaldIII)
Pay no attention to the variable names, was really bored. I also find that I'm using this method to solve most of these challenges for no apparent reason.
2
u/illtragic Jan 21 '14
I'm worried because some of these solutions are really long. So maybe I'm not taking something into account?
+/u/CompileBot C++
Input:
2
u/euleri Jan 21 '14
Haven't done python in a while but here goes.
2
u/Die-Nacht 0 0 Jan 22 '14 edited Jan 22 '14
No where near as short as it could be, or as pretty, but at least it allows for some lock where you need to turn it X amount of times before landing on something:
+/u/CompileBot Haskell
EDIT: DAMN IT! I forgot this is exactly what mod is for! I knew that representing the lock as an infinite repeating list was stupid.
2
u/galymzhan Jan 22 '14
C
2
u/jnazario 2 0 Jan 24 '14
decided to play with the julia language this evening. here's a straight port of the bash answer /u/hestoicattack posted.
usage:
yields the right answer. the 5,0,0,0 test yields the right answer too.
(i'm continuing my explorations far beyond python)
2
u/kevinsucks Jan 25 '14 edited Jan 25 '14
I came to the realization not too long ago that after a year of CS courses, I am still very bad at programming. Anywhere here is my solution in C#:
2
u/Taunk Jan 26 '14 edited Jan 26 '14
Python 2.7.6
2
u/Devil777 Jan 27 '14
Java (first post in this sub reddit)
}
2
u/killmefirst Jan 28 '14 edited Jan 28 '14
My C++ code:
EDIT: b >= a, not b > a
2
u/AndrewBenavides Feb 01 '14
Here's a couple of verbose F# solutions I worked up while teaching myself F#.
Here's link to github with commit history, if anyone's interested.
+/u/CompileBot F#
2
u/Bad_The_Guy Feb 07 '14
Question, wouldn't the "Spin lock to second number counter-clockwise: +4, at position 2" be +5?
I notice that when you spin the lock for a complete rotation you don't count the initial position as part of the iterations, "Spin lock 2 times clockwise: +10, at position 0"
However when you spin to go directly to a number you count that towards the iterations, for example from 0 to 1 gives me a +1 towards my iterations "Spin lock to first number clockwise: +1, at position 1"
So in this step, "Spin lock to second number counter-clockwise: +4, at position 2", wouldn't it be a +5 since you have to include the 2? Or is that just an unstated rule that only the first spin directly to number counts towards the iterations?
2
u/spfy Jan 13 '14 edited Jan 14 '14
edit: I've updated how mine works. Combinations with repeating numbers will work fine now. My Java solution! Lots of class methods, but I think it's pretty clean. :P
Outputs:
2
u/swingtheory Jan 13 '14 edited Jan 13 '14
In c#. Maybe being concise wasn't the goal, and maybe I got a little confusing with writing the code to count the rotation increments:
2
u/eslag90 Jan 14 '14
Python 2.7:
2
u/i_am_nicky_haflinger Jan 14 '14
Reactive javascript (using bacon.js for a simple message bus). Fun :)
Watch decoder() and lock() chatting back and forth on the jsfiddle.
2
u/undergroundmonorail Jan 14 '14
I don't actually know if mine is correct! It works for the test case, but I didn't test it with anything else because I can't think of anything that would outright break it, and testing arbitrary locks would require me working out the correct solution manually (which I am far too lazy to do).
Python 2.7
2
u/aplicable Jan 15 '14
I've got a nice one-liner for this. I just drew down a dial and logic'd my way through it. JAVA
2
u/unemployed_coder Jan 15 '14 edited Jan 16 '14
Java EDIT: couldn't do 5 0 0 0 before. +/u/CompileBot Java
Input: 5 0 0 0
1
u/ponkanpinoy Jan 15 '14 edited Jan 15 '14
+/u/CompileBot Common Lisp
EDITed because programming on paper and posting via mobile is buggy. Fixed now :)
1
u/aerozepplin Jan 22 '14 edited Jan 22 '14
Python. +/u/CompileBot python
Input:
1
u/kilimanjaroxyz Jan 29 '14 edited Jan 29 '14
Ruby tuesday:
Any feedback welcome!
edit: formatting
1
u/iloverabbits Jan 30 '14
Hello, first time contributing! Here is my ruby solution.
1
u/liaobaishan Jan 31 '14
java:
}
1
u/[deleted] Feb 09 '14 edited Feb 09 '14
Just did this one rather late, but here it is in factor, I can't get it to post any output at ideone, but it does on my machine.
1
u/league_of_taric Feb 11 '14
Ruby
1
u/ChinchillaxDOTcom Feb 11 '14
My Java Solution
{
}
1
u/skjennings Feb 15 '14
python def combo_lock( rotate, first, second, third ): print "Total Spins: %d" % ( rotate*2 ) + first + rotate + (rotate + first - second) + (thrid - second)
1
u/ttr398 0 0 Feb 16 '14
Python:
1
u/jabez007 Feb 17 '14
Cache Openscript
1
u/Hero_764 Feb 19 '14
Basic C solution:
1
u/nezaj Feb 21 '14 edited Feb 21 '14
I'm still pretty new to Python. Would love some feedback. Here's my solution with tests.
Test
Solution
1
u/stubarfoo Feb 23 '14 edited Feb 23 '14
Ruby (my first submission and first time coding ruby :))
1
u/tehusername Feb 26 '14
Python
1
u/[deleted] Mar 06 '14
1
u/bodieskate Mar 06 '14
Python solution (first-time poster, sorta new at programming):
Any critiques would be more than welcome (even though this is an old post).
1
u/felix1429 Mar 09 '14
Java:
1
u/moustachauve Mar 19 '14
This is the solution I've come with in C#, I'm not sure I could have done a lot better
Input:
Output:
1
u/csharperperson Mar 25 '14
C# Solution
1
u/masterftp Mar 25 '14
I have a doubt.
Based on the procedure given shouldnt the example be:
1
u/[deleted] May 23 '14
Java
1
u/Kingofhearts102 Jun 13 '14
c++ very convoluted version. tried to make it so the function automatically prefixes by a variable amount
1
u/Germy_Widemirror Jun 21 '14
Late Javascript entry
1
u/razeal113 Jan 13 '14
what is the syntax for hiding code?
1
u/thinksInCode Jan 13 '14
Java (overkill compared to the other solutions):
1
u/pirate_platypus Jan 13 '14
Python 3.x
1
u/antiHerbert Jan 13 '14
first time posting, C# for you guys
1
u/FaithOfOurFathers Jan 13 '14
Java
The logic seems right in my head, and the code works with the sample input, but If someone has some other inputs I can put in to check, that would be great. Also I have no idea how to do space delimitation, all the explanations online seem really confusing to me. Thanks!
1
u/gamehelp16 Jan 14 '14
Javascript (honestly I'm not that sure if this really works as written or not)
1
u/taterNuts Jan 14 '14 edited Jan 14 '14
Python (3), just got it working with the initial input:
1
u/SensationalJellyfish Jan 14 '14
Here is my solution in OCaml:
The functions left and right returns the number of steps from index f to index t by rotating the lock to the left or right.
1
u/Frigguggi 0 1 Jan 14 '14 edited Jan 16 '14
Java:
1
u/jamheldion Jan 14 '14 edited Jan 14 '14
I was wondering if we could have more sample inputs and outputs. I'm not so sure if I already got the logic right.
Java:
1
u/[deleted] Jan 14 '14
Some python 3:
1
u/hellionsoldier Jan 14 '14 edited Jan 14 '14
Java:
1
u/conor_fogarty Jan 15 '14 edited Jan 15 '14
In Go:
Edit: Handles 5 0 0 0 correctly; uses array instead of slice because length of combination is fixed.
1
u/mxxz Jan 15 '14
Python solution:
1
u/hamc17 Jan 15 '14
Here's my Java solution.
I decided to make a class for a combination lock in case I wanted to reuse it in the future (probably not). It doesn't have a main method as go() is run from another class that I have.
Works fine for the sample input, for an input like "5 0 0 0" it treats the 3 combination 0s as entirely new positions, so already being on 0 from the clockwise/anticlockwise rotations doesn't count.
-2
u/inagiffy Jan 18 '14 edited Jan 18 '14
+ /u/CompileBot PYTHON
print("Hello world!")
1
u/[deleted] Jan 15 '14 edited Jan 15 '14
[removed] — view removed comment