Return to Video

The Tower of Epiphany | Think Like A Coder, Ep 7

  • 0:32 - 0:37
    Ethic and Hedge are on the ground floor
    of a massive tower.
  • 0:37 - 0:42
    Barriers of energy separate them
    from their quest’s second goal:
  • 0:42 - 0:44
    the Node of Creation.
  • 0:53 - 0:57
    To reach it, Ethic must use
    three energy streams to climb the tower.
  • 0:57 - 1:03
    As soon as she steps forward a timer
    will begin counting down from 60 seconds.
  • 1:07 - 1:12
    At the back of the room
    there’s a basin made of invisible towers
  • 1:12 - 1:15
    that can hold energy between them.
  • 1:15 - 1:19
    After one minute, a torrent of energy
    will pour down from above,
  • 1:19 - 1:21
    filling one unit at a time,
  • 1:21 - 1:25
    with a force field preventing it
    from spilling out the front or back.
  • 1:25 - 1:28
    During the 60 calm seconds,
  • 1:28 - 1:33
    Ethic and Hedge must decide exactly
    how many units of energy will fall.
  • 1:33 - 1:34
    For each of the three challenges,
  • 1:34 - 1:38
    they must choose the amount
    that will fill the basin exactly.
  • 1:38 - 1:42
    If they do so, the energy will propel them
    further upwards.
  • 1:42 - 1:47
    But if they get the amount at all wrong,
    the energy lift will fail,
  • 1:47 - 1:48
    dropping them.
  • 1:48 - 1:51
    Diagrams on the walls
    illustrate some examples.
  • 1:51 - 1:56
    This configuration
    will capture exactly 2 units of energy.
  • 1:56 - 2:01
    This configuration will capture 4—
    3 here, and 1 here.
  • 2:01 - 2:03
    And this one will also capture 4,
  • 2:03 - 2:07
    because any energy on the right
    would spill out.
  • 2:07 - 2:09
    The energy will rain down in such a way
  • 2:09 - 2:14
    that it’ll only overflow
    if there’s no space that could hold it.
  • 2:14 - 2:19
    Hedge can make one tower of blocks visible
    at a time and count how tall it is,
  • 2:19 - 2:23
    but he can’t look at
    the whole structure all at once.
  • 2:23 - 2:26
    How does Ethic program Hedge
    to figure out
  • 2:26 - 2:29
    exactly how much energy
    each basin can hold?
  • 2:29 - 2:39
    Pause now to figure it out for yourself.
  • 2:39 - 2:42
    Here’s one way of thinking about
    what’s happening:
  • 2:42 - 2:45
    each unoccupied cell will hold energy
  • 2:45 - 2:49
    if and only if there is a wall
    eventually to its left,
  • 2:49 - 2:52
    and a wall eventually to its right.
  • 2:52 - 2:56
    But it would take a long time for Hedge
    to check this for each individual cell.
  • 2:56 - 3:01
    So what if he were to consider
    a whole column of blocks at a time?
  • 3:01 - 3:05
    How many units of energy
    can this hold, for instance?
  • 3:05 - 3:10
    Pause now to figure it out for yourself.
  • 3:10 - 3:14
    Let’s analyze the problem
    by looking at our example.
  • 3:14 - 3:16
    There are 5 columns of blocks here.
  • 3:16 - 3:20
    The leftmost one can’t hold any energy,
    because there’s nothing higher than it.
  • 3:20 - 3:23
    The 2nd stack can have 3 units above it,
  • 3:23 - 3:27
    as they would be trapped
    between these two 4 block stacks.
  • 3:27 - 3:32
    We get 3 units by taking the height
    where the energy would level off— 4,
  • 3:32 - 3:36
    and subtracting the height of the stack—
    so that’s 4 minus 1.
  • 3:36 - 3:42
    The 3rd stack is similar— 4 to the left,
    4 to the right, and it’s 3 high,
  • 3:42 - 3:47
    so it’ll hold 4 minus 3 equals 1 unit.
  • 3:47 - 3:51
    The 4th stack and 5th stacks have
    nothing higher than them to the right,
  • 3:51 - 3:53
    so they can’t hold any energy.
  • 3:53 - 3:57
    We can adapt this idea into an algorithm.
  • 3:57 - 4:01
    Considering one column at a time
    as the point of reference,
  • 4:01 - 4:05
    Hedge can look to the left stack by stack
    to find the height of the tallest one,
  • 4:05 - 4:08
    look to the right to find the height
    of the tallest one,
  • 4:08 - 4:13
    and take the smaller of the two
    as the height the energy can fill up to.
  • 4:13 - 4:16
    If the result is higher than the column
    in question,
  • 4:16 - 4:19
    subtract the height
    of the original column,
  • 4:19 - 4:24
    and the result will be the number of units
    that column can hold.
  • 4:24 - 4:27
    If it's equal to or below the level
    of the column in question,
  • 4:27 - 4:29
    the energy would spill off.
  • 4:29 - 4:33
    Hedge can apply that
    to an entire basin with a loop
  • 4:33 - 4:39
    that starts on the left-most column
    and moves right, one column at a time.
  • 4:39 - 4:44
    For each column, he’ll run the same steps—
    look all the way left for the tallest,
  • 4:44 - 4:47
    do the same to the right,
    take the lower height of the two,
  • 4:47 - 4:49
    subtract the original column height,
  • 4:49 - 4:53
    and increase the grand total
    if that number is positive.
  • 4:53 - 4:57
    His loop will repeat
    as many times as there are columns.
  • 4:57 - 5:01
    That will work, but it’ll take a long time
    for a large basin.
  • 5:01 - 5:05
    At every step Hedge repeats the action
    of looking left and looking right.
  • 5:05 - 5:10
    If there are N stacks,
    he’ll look at all N stacks N times.
  • 5:10 - 5:12
    Is there a faster way?
  • 5:12 - 5:16
    Here’s one time saver:
    before doing anything else,
  • 5:16 - 5:17
    Hedge can start on the left,
  • 5:17 - 5:21
    and keep a running tally
    of what the highest stack is.
  • 5:21 - 5:25
    Here that would be 2, 2 again,
    since the first was higher,
  • 5:25 - 5:28
    then 4, 4, 4.
  • 5:28 - 5:31
    He can then find
    the highest right-most stacks
  • 5:31 - 5:37
    by doing the same going right-to-left:
    1, 3, 4, 4, 4.
  • 5:37 - 5:41
    In the end he’ll have a table
    like this in his memory.
  • 5:41 - 5:46
    Now, Hedge can take one more pass
    to calculate how much energy there will be
  • 5:46 - 5:50
    above every stack
    with the same equation from before:
  • 5:50 - 5:54
    take the smaller of the stored left
    and right values,
  • 5:54 - 5:57
    and subtract the height
    of the current tower.
  • 5:57 - 6:02
    Instead of looking at N stacks N times,
    he’ll look at N stacks just 3 times—
  • 6:02 - 6:05
    which is what’s called linear time.
  • 6:05 - 6:08
    There are ways to optimize
    the solution even further,
  • 6:08 - 6:11
    but this is good enough for our heroes.
  • 6:11 - 6:12
    Ethic and Hedge work as one.
  • 6:15 - 6:19
    The first cascade is a breeze,
    and they rise up the tower.
  • 6:22 - 6:24
    The second is a little tougher.
  • 6:33 - 6:37
    The third is huge,
    with dozens of stacks of blocks.
  • 6:37 - 6:41
    The timer ticks down towards zero,
    but Ethic’s program is fast.
  • 6:41 - 6:44
    She gets the wheel in position
    just in time,
  • 6:49 - 6:52
    and the energy lifts them
    to the Node of Creation.
  • 6:56 - 7:01
    Like the first, it reveals a vision:
    memories of years gone by.
  • 7:01 - 7:03
    The world machine changed everything,
  • 7:03 - 7:07
    and Ethic, in her position
    as chief robotics engineer,
  • 7:07 - 7:09
    grew troubled by what she saw.
  • 7:09 - 7:12
    When the Bradbarrier went up
    to keep the people in,
  • 7:12 - 7:15
    she knew something was seriously wrong.
  • 7:15 - 7:17
    So she created three artifacts
  • 7:17 - 7:21
    with the ability to restore people’s
    power, creativity, and memory,
  • 7:21 - 7:24
    and smuggled them to three communities.
  • 7:24 - 7:26
    Before she could tell people
    how to use them,
  • 7:26 - 7:30
    the government discovered her efforts
    and sent bots to arrest her
  • 7:30 - 7:32
    and the other programmers.
  • 7:32 - 7:35
    The last thing Ethic
    used the world machine to create
  • 7:35 - 7:38
    was a robot that would protect
    the ancient device
  • 7:38 - 7:42
    from the forces of ignorance
    by enclosing it in a giant maze.
  • 7:42 - 7:45
    She named her creation Hedge.
  • 7:52 - 7:56
    Without warning, the energy lift flickers,
    then fizzles out.
Title:
The Tower of Epiphany | Think Like A Coder, Ep 7
Speaker:
Alex Rosenthal
Description:

View full lesson: https://ed.ted.com/lessons/the-tower-of-epiphany-think-like-a-coder-ep-7

This is episode 7 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:
07:58

English subtitles

Revisions Compare revisions