using System.Collections.Generic;
public class PasswordChallenge
public static object Hack(int mainKey, int mainLock, int lockX)
var lockStates = new Dictionary<int, int>();
var visitedLocks = new HashSet<int>();
if (visitedLocks.Contains(currentLock))
return "system with a high level of security";
visitedLocks.Add(currentLock);
int attemptsForThisLock = Math.Min(10, currentLock);
totalAttempts += attemptsForThisLock;
currentLock = currentLock + mainKey;
if (currentLock > 1000000)
return "system with a high level of security";
public static object HackOptimized(int mainKey, int mainLock, int lockX)
var visitedLocks = new HashSet<int>();
while (!visitedLocks.Contains(currentLock))
visitedLocks.Add(currentLock);
return totalAttempts + currentLock;
currentLock = currentLock + mainKey;
if (currentLock > 10000000)
return "system with a high level of security";
return "system with a high level of security";
public static object HackFinal(int mainKey, int mainLock, int lockX)
var visitedLocks = new HashSet<int>();
while (!visitedLocks.Contains(currentLock))
visitedLocks.Add(currentLock);
return totalAttempts + currentLock;
if (totalAttempts > 10000)
return "system with a high level of security";
return "system with a high level of security";
public static void Main()
Console.WriteLine("=== Password Challenge Solver ===");
Console.WriteLine("This program calculates how many attempts a hacker needs to crack a security system.");
Console.Write("Enter the main key: ");
int mainKey = int.Parse(Console.ReadLine());
Console.Write("Enter the main lock: ");
int mainLock = int.Parse(Console.ReadLine());
Console.Write("Enter the target lock (lock_x): ");
int lockX = int.Parse(Console.ReadLine());
if (mainKey <= 0 || mainLock <= 0 || lockX <= 0)
Console.WriteLine("Error: All values must be positive integers.");
if (mainKey >= 1000000 || mainLock >= 1000000 || lockX >= 1000000)
Console.WriteLine("Error: All values must be less than 1,000,000.");
Console.WriteLine("\nCalculating...");
var result = HackFinal(mainKey, mainLock, lockX);
Console.WriteLine($"\nResult: {result}");
if (result is int attempts)
Console.WriteLine($"The hacker needs {attempts} attempts to guarantee access to the lock.");
Console.WriteLine("The system has a high level of security - it's impossible to guarantee access.");
Console.Write("\nWould you like to see a step-by-step trace? (y/n): ");
string traceChoice = Console.ReadLine().ToLower();
if (traceChoice == "y" || traceChoice == "yes")
Console.WriteLine("\nStep-by-step trace:");
TraceExample(mainKey, mainLock, lockX);
Console.WriteLine("Error: Please enter valid integers only.");
catch (OverflowException)
Console.WriteLine("Error: Number is too large. Please enter a smaller value.");
Console.WriteLine($"Error: {ex.Message}");
Console.Write("Would you like to try another set of values? (y/n): ");
string continueChoice = Console.ReadLine().ToLower();
if (continueChoice != "y" && continueChoice != "yes")
Console.WriteLine("Thank you for using the Password Challenge Solver!");
Console.WriteLine("Press any key to exit...");
public static void TraceExample(int mainKey, int mainLock, int lockX)
Console.WriteLine($"Initial lock: {currentLock}");
Console.WriteLine($"Step {step}: Lock = {currentLock}, too big (> 10)");
Console.WriteLine($" Making 10 attempts, all fail");
Console.WriteLine($" Lock updated to: {currentLock}");
Console.WriteLine($" Total attempts so far: {totalAttempts}");
Console.WriteLine($"Step {step}: Lock = {currentLock}, small enough (<= 10)");
Console.WriteLine($" Can crack with at most {currentLock} attempts");
totalAttempts += currentLock;
Console.WriteLine($"Final answer: {totalAttempts}");