Skip to content

Instantly share code, notes, and snippets.

@justinAurand
Created January 15, 2014 06:15
Show Gist options
  • Save justinAurand/8431657 to your computer and use it in GitHub Desktop.
Save justinAurand/8431657 to your computer and use it in GitHub Desktop.
Count the number of increments traversed while opening a combination lock. Challenge link: http://www.reddit.com/r/dailyprogrammer/comments/1v4cjd/011314_challenge_148_easy_combination_lock/
using System;
class Program
{
static void Main(string[] args)
{
#region Argument Count / Data Type Validation
// Check for valid number of command line arguments.
if (args == null || args.Length != 4)
{
Console.WriteLine("Pass 4 command line arguments. The 1st should be the count of numbers on the face of the lock. The next three should be the combination.");
Console.ReadKey();
return;
}
// Ensure all command line arguments can be parsed into integers.
foreach (string arg in args)
{
int trash;
bool isAnInteger = Int32.TryParse(arg, out trash);
if (!isAnInteger)
{
Console.WriteLine("All command line arguments must be integers.");
Console.ReadKey();
return;
}
}
#endregion
#region Gather Command Line Arguments
int countOfNumbersOnLock = Convert.ToInt32(args[0]);
int firstNumberInCombination = Convert.ToInt32(args[1]);
int secondNumberInCombination = Convert.ToInt32(args[2]);
int thirdNumberInCombination = Convert.ToInt32(args[3]);
#endregion
#region Argument Range Validation
// Validate the count of numbers on the lock is within the range 0 to 255.
if (countOfNumbersOnLock < 0 || countOfNumbersOnLock > 255)
{
Console.WriteLine("The first command line argument, the count of numbers on the face of the lock, must be between 0 and 255.");
Console.ReadKey();
return;
}
// Validate the combination falls within possible values.
if (firstNumberInCombination < 0 || firstNumberInCombination > (countOfNumbersOnLock - 1)
|| secondNumberInCombination < 0 || secondNumberInCombination > (countOfNumbersOnLock - 1)
|| thirdNumberInCombination < 0 || secondNumberInCombination > (countOfNumbersOnLock - 1))
{
Console.WriteLine("The numbers in the combination must be positive integers that are less than or equal to the count of numbers on the face of the lock minus one.");
Console.ReadKey();
return;
}
#endregion
#region Calculation
// Calculate increments traversed during each spin.
int incrementsDuringFirstSpin = countOfNumbersOnLock * 2;
int incrementsDuringSecondSpin = firstNumberInCombination;
int incrementsDuringThirdSpin = countOfNumbersOnLock;
int incrementsDuringFourthSpin = ((firstNumberInCombination - secondNumberInCombination) + countOfNumbersOnLock) % countOfNumbersOnLock;
int incrementsDuringFifthSpin = (thirdNumberInCombination == secondNumberInCombination)
? countOfNumbersOnLock
: ((thirdNumberInCombination - secondNumberInCombination) + countOfNumbersOnLock) % countOfNumbersOnLock;
// Total the increments traversed for all spins.
int totalNumberOfIncrements = incrementsDuringFirstSpin
+ incrementsDuringSecondSpin
+ incrementsDuringThirdSpin
+ incrementsDuringFourthSpin
+ incrementsDuringFifthSpin;
#endregion
#region Display Results
// Display command line arguments.
foreach (string arg in args)
Console.Write(arg + " ");
Console.WriteLine();
// Display result.
Console.Write(totalNumberOfIncrements);
Console.ReadKey();
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment