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