1. Home
  2. Explore
  3. Computer Science
  4. Lockpicking Puzzle

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Lockpicking Puzzle

๐Ÿ” The Prison Break: Ethic, Hedge, and the Power of Loops

Upon awakening from stasis with amnesia, Ethic finds herself imprisoned. Her only companion is a mysterious stranger named Hedge, who is literally stuck in her window bars. Hedge reveals he's there to help Ethic save the world and their first task is to break out of jail.


๐Ÿ”’ Understanding the Lock Mechanism

Hedge demonstrates his unique ability to transform his hand into a lockpick. He explains the prison's lock system:

  • Each lock has a red dial inside the keyhole.

  • The dial can be rotated to 100 positions, numbered 1 through 100.

  • The correct key spins the dial to a specific position, which then turns green and unlocks the door.

  • Stealing keys is not an option.

Hedge can execute Ethic's specific instructions. Vague commands like "pick the lock" are not understood, but precise instructions such as "spin the dial 5 positions forward" are.


๐Ÿง  The Challenge: Efficient Lock Picking

Ethic and Hedge must efficiently open their cell door, and then quickly tackle the outer prison door before guards arrive. The core problem is finding the correct dial position without knowing it beforehand.

๐Ÿ’ก Hint: The Concept of a Loop

A key programming concept that can solve this challenge is a loop. Loops allow for the repetition of instructions a specified number of times or until a certain condition is met.

  • Fixed repetitions: "jump up and down 100 times."

  • Conditional repetitions: "keep jumping up and down until itโ€™s 7 oโ€™clock."


๐Ÿ”„ Implementing Loop Solutions for the Lock

The primary goal is for Hedge to try every combination until the correct one is found.

๐Ÿ“ Tedious Manual Approach (Inefficient)

One way would be to instruct Hedge to try each combination sequentially:

  1. "Try 1 and check the light. If green, open door. If not, try 2."

  2. "If that doesnโ€™t work, try 3."

  3. ...and so on, all the way up to 100.

This approach is highly inefficient and would require over 100 lines of instructions.

โœจ Efficient Loop Solutions

Loops drastically reduce the number of instructions needed.

1. For Loop (Fixed Repetition)

A "for" loop specifies the number of repetitions.

  • Instruction: "Check the dialโ€™s color, then spin the dial forward once, for 100 repetitions. Remember where the dial turns green, then have Hedge set it back to that number."

  • Mechanism: Hedge would cycle through all 100 positions, identify the correct one, and then return the dial to that position.

2. Until Loop (Conditional Repetition)

An "until" loop executes an action until a condition is met. This is even more efficient.

  • Instruction: "Spin the dial one position at a time until it turns green. As soon as it turns green, stop and open the door."

  • Advantage: If the door unlocks at position 1, Hedge stops immediately, without needing to check the remaining 99 positions. This is highly time-saving.

3. While Loop (Conditional Repetition)

A "while" loop is similar to an "until" loop, performing an action while a condition is true.

  • Instruction: "While the dial is still red, turn the dial one position forward. Stop when it turns green."

  • Mechanism: Hedge continues spinning as long as the dial remains red, stopping only when it changes to green.


๐Ÿš€ The Escape and the Greater Mission

Hedge successfully uses a loop, and the cell door unlocks at position 41. They seize an opportunity during the guard rotation and escape.

๐Ÿšช The Outer Door

Ethic chooses to crack the outer door. This lock proves more challenging, requiring Hedge to spin the dial all the way to position 93.

๐ŸŒ Hedge's Revelation

With both doors open, Hedge explains his mission:

  • The world is in turmoil, overrun by robots.

  • Ethic is the only one who can restore balance.

  • They must collect three powerful artifacts being used for nefarious purposes.

  • These artifacts are necessary to return to the world machine (the giant crystal) and "set things right."

Ethic has escaped prison, only to find herself embroiled in a much larger, world-saving quest.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent