Return to Video

The Gauntlet | Think Like A Coder, Ep 8

  • 0:22 - 0:25
    Their fall from the tower sends
    Ethic and Hedge
  • 0:25 - 0:29
    spinning into the rapids
    of a river of pure energy.
  • 0:31 - 0:37
    This torrent flows from the Bradbarrier
    all the way to Huxenborg.
  • 0:37 - 0:40
    There an entire city’s worth of factories
  • 0:40 - 0:43
    build the robots
    and house the Node of Memory,
  • 0:43 - 0:47
    the last of the three powerful artifacts
    Ethic needs to collect.
  • 0:47 - 0:50
    After a long day and a longer night
  • 0:50 - 0:54
    they find themselves
    in a canyon of brick and steel.
  • 0:59 - 1:02
    Just when they’re about to reach
    the end of the line,
  • 1:02 - 1:03
    a rope catches them.
  • 1:07 - 1:11
    Their savior, Lemma,
    has been waiting for them.
  • 1:11 - 1:15
    When Ethic claimed the Node of Creation
    from the forest tower,
  • 1:15 - 1:19
    radios all across the land
    came back to life.
  • 1:19 - 1:24
    Adila, the resistance leader,
    immediately started contacting her allies,
  • 1:24 - 1:26
    none more important than Lemma,
  • 1:26 - 1:32
    a brilliant scientist working from within
    Huxenborg to bring down the machines.
  • 1:32 - 1:36
    Unfortunately, the radios
    also tipped off the robots.
  • 1:36 - 1:39
    So they’ve taken defensive measures
  • 1:39 - 1:43
    to protect the final artifact in its home
    in the very heart of the city.
  • 1:43 - 1:49
    There’s only one way to get there:
    the gauntlet of forking paths.
  • 1:49 - 1:55
    It’s a deadly series of luminous conveyors
    that wind underneath Huxenborg.
  • 1:55 - 1:57
    Starting from the current position,
  • 1:57 - 2:01
    each section runs for a distance,
    then splits in two.
  • 2:01 - 2:05
    Every branch does the same thing,
    again and again.
  • 2:05 - 2:07
    There are thousands of branches.
  • 2:07 - 2:13
    Only one path leads to the artifact;
    all the others to destruction.
  • 2:13 - 2:18
    Fortunately, the Node of Creation
    has granted Hedge a strange power:
  • 2:18 - 2:21
    he can produce slightly smaller versions
    of himself.
  • 2:21 - 2:26
    Each version can do only two things:
    radio information back to its parent,
  • 2:26 - 2:30
    and produce slightly smaller
    versions of itself…
  • 2:30 - 2:34
    which can do the same two things,
    as can their children,
  • 2:34 - 2:37
    for as many generations as needed.
  • 2:37 - 2:42
    A patrol is closing in on their position,
    so Ethic’s time is limited.
  • 2:42 - 2:47
    What instructions should she give
    Hedge to find the one safe path?
  • 2:47 - 2:54
    Pause the video to figure it out yourself.
  • 2:54 - 2:55
    Hint in 3
  • 2:55 - 2:56
    Hint in 2
  • 2:56 - 2:57
    Hint in 1
  • 2:58 - 3:03
    Programmers have an elegant tool
    in their arsenal called recursion.
  • 3:03 - 3:08
    Recursion is when you have a set of
    instructions that refers back to itself.
  • 3:08 - 3:11
    It’s like using a word
    in its own definition,
  • 3:11 - 3:16
    except where that’s frowned upon,
    this is incredibly effective.
  • 3:16 - 3:20
    Recursion involves repetition,
    but in a different way than loops.
  • 3:20 - 3:24
    Where a loop takes one action
    and repeats it again and again,
  • 3:24 - 3:29
    recursion will start an action,
    and before it’s finished, use it again,
  • 3:29 - 3:33
    and before that’s finished,
    use it again, and so on.
  • 3:33 - 3:37
    It keeps doing this until
    some end state is reached.
  • 3:37 - 3:41
    It then passes the information back up,
    layer after layer,
  • 3:41 - 3:44
    until it reaches the top
    and ends the cycle.
  • 3:44 - 3:49
    Recursion is ideal for problems
    that involve self-similarity,
  • 3:49 - 3:52
    where each part resembles
    the larger whole.
  • 3:52 - 3:58
    Like, for example, a deadly defense
    system designed to end any person or thing
  • 3:58 - 4:00
    who dares tread upon it.
  • 4:00 - 4:02
    Pause the video to figure it out yourself.
  • 4:02 - 4:03
    Solution in 3
  • 4:03 - 4:04
    Solution in 2
  • 4:04 - 4:05
    Solution in 1
  • 4:05 - 4:08
    Ethic’s conundrum seems sprawling
    on the surface,
  • 4:08 - 4:12
    but there’s a remarkably simple
    solution to it using recursion.
  • 4:12 - 4:17
    In order to find it, let’s first look
    at the simplest version of this puzzle:
  • 4:17 - 4:20
    what if the entire maze
    were just two paths?
  • 4:20 - 4:25
    If Hedge copies himself, the copy
    that goes the wrong way will be destroyed.
  • 4:25 - 4:28
    So the other one,
    which will reach the artifact,
  • 4:28 - 4:32
    can radio back the path it took,
    and then no matter which way is correct,
  • 4:32 - 4:35
    that’s the answer Hedge will receive.
  • 4:35 - 4:38
    This is called the "base case"
    of the recursion.
  • 4:38 - 4:42
    Now, suppose the maze
    branches twice from the starting point,
  • 4:42 - 4:45
    and at every intersection,
    Hedge’s copies—
  • 4:45 - 4:48
    let’s call them Twig 1 and Twig 2—
  • 4:48 - 4:53
    make more copies—
    let’s call them Leaves 1 through 4.
  • 4:53 - 4:56
    Three Leaves will be destroyed.
  • 4:56 - 5:00
    The one that reaches the artifact
    will radio back the right answer,
  • 5:00 - 5:02
    but only to its parent.
  • 5:02 - 5:06
    So if Twig 1 or 2 is waiting
    at an intersection
  • 5:06 - 5:08
    and hears something over the radio,
  • 5:08 - 5:11
    that’s the right way to go
    to the artifact from where it is.
  • 5:11 - 5:15
    To tell Hedge the right answer
    from his perspective,
  • 5:15 - 5:17
    the Twig should say which way it went,
  • 5:17 - 5:21
    and then the route
    it just heard over the radio.
  • 5:21 - 5:25
    This same process will work no matter
    how many times the maze branches.
  • 5:25 - 5:28
    Any answer a copy hears on the radio
  • 5:28 - 5:32
    must be the way to the control room
    from its location,
  • 5:32 - 5:34
    and if it then adds the branch it took,
  • 5:34 - 5:37
    it can tell its parent
    how to get there as well.
  • 5:37 - 5:41
    We can sum up the instructions
    in an action called Pathfinder
  • 5:41 - 5:44
    that every version of Hedge will follow:
  • 5:44 - 5:47
    1. If you’ve reached the artifact,
  • 5:47 - 5:51
    radio to your parent whether
    you got there by going left or right.
  • 5:51 - 5:55
    2. When you reach an intersection,
    move off the conveyor
  • 5:55 - 5:59
    and send new copies down the left
    and right paths.
  • 5:59 - 6:01
    Have them each run Pathfinder.
  • 6:01 - 6:03
    This is where recursion comes in,
  • 6:03 - 6:08
    and this may happen many times before
    the last instruction triggers, which is:
  • 6:08 - 6:13
    3. If you hear anything over the radio,
    you should radio to your parent
  • 6:13 - 6:17
    whether you got to your spot
    by going left or right,
  • 6:17 - 6:19
    then repeat everything you just heard.
  • 6:19 - 6:23
    Pathfinder is an example
    of what programmers call functions,
  • 6:23 - 6:26
    subroutines, or procedures.
  • 6:26 - 6:30
    No matter the terminology,
    the idea is the same—
  • 6:30 - 6:34
    it’s a set of instructions given a label
    so that it can be easily reused—
  • 6:34 - 6:37
    perhaps even by itself.
  • 6:37 - 6:40
    And in our case that’ll work perfectly—
  • 6:40 - 6:45
    an entire network of paths mapped
    using just three instructions.
  • 6:46 - 6:48
    So here's what happens.
  • 7:10 - 7:16
    By the time the patrol rounds the corner,
    Ethic and Lemma have improvised disguises.
  • 7:16 - 7:20
    They try to confuse the bots
    long enough to buy Hedge time.
  • 7:31 - 7:36
    Finally, Hedge’s radio crackles to life
    with a series of directions.
  • 7:36 - 7:40
    The three dive onto the conveyor
    and flee for their lives,
  • 7:40 - 7:44
    with a squadron of enforcer bots
    in hot pursuit.
Title:
The Gauntlet | Think Like A Coder, Ep 8
Speaker:
Alex Rosenthal
Description:

View full lesson: https://ed.ted.com/lessons/the-gauntlet-think-like-a-coder-ep-8

This is episode 8 of our animated series “Think Like A Coder.” This 10-episode narrative follows a girl, Ethic, and her robot companion, Hedge, as they attempt to save the world. The two embark on a quest to collect three artifacts and must solve their way through a series of programming puzzles.

Lesson by Alex Rosenthal, directed by Kozmonot Animation Studio.

more » « less
Video Language:
English
Team:
closed TED
Project:
TED-Ed
Duration:
08:01

English subtitles

Revisions Compare revisions