The Artists | Think Like A Coder, Ep 5
-
0:23 - 0:28Dawn and the train are both breaking
when Ethic and Hedge arrive in the woods. -
0:28 - 0:31The adventurers
have recovered the first artifact— -
0:31 - 0:33the Node of Power—
-
0:33 - 0:37and have come to the 198forest
in search of the second. -
0:37 - 0:42Here they’re welcomed
by the director of the colony, Octavia. -
0:42 - 0:44She established this treehouse sanctuary
-
0:44 - 0:47after the robots freed everyone
from having to work. -
0:47 - 0:51It was meant to be a haven
where people could follow their passions, -
0:51 - 0:53take up crafts, and find fulfillment.
-
0:53 - 0:55Which they did… at first.
-
0:55 - 0:58Some years ago everyone forgot the point.
-
0:58 - 0:59They abandoned arts and crafts
-
0:59 - 1:03and instead just painted and exhibited
pictures of themselves -
1:03 - 1:05over, and over, and over.
-
1:05 - 1:08The location of the second artifact
is no secret; -
1:08 - 1:11it’s in a tower,
guarded by a garrison of bots, -
1:11 - 1:15a bottomless ravine,
and who knows what other traps. -
1:15 - 1:19As soon as the tower
went up with the node inside, -
1:19 - 1:23human communication
across the land went dark. -
1:23 - 1:25Octavia’s been after it for years,
-
1:25 - 1:29but try as she might,
the defenses thwart her. -
1:29 - 1:33In order to even get to the tower,
the team will need a distraction. -
1:33 - 1:35Octavia has an idea:
-
1:35 - 1:39stir up the people
through some well-intentioned vandalism. -
1:39 - 1:43The residents’ paintings are all squares
that come in different sizes, -
1:43 - 1:47all an odd number of pixels across.
-
1:47 - 1:49Helper-bots
pick up the finished portraits -
1:49 - 1:53and hang them in public places
for everyone to admire. -
1:53 - 1:57There’s a slim margin of time
when Hedge can access the paintings. -
1:57 - 2:00If he were to deface each one with an X,
-
2:00 - 2:03the people would blame the helper-bots,
-
2:03 - 2:06creating just the distraction
the team needs. -
2:06 - 2:09If only it were so easy.
-
2:09 - 2:11Hedge can’t just paint an X—
-
2:11 - 2:15his painting processor
requires very specific instructions. -
2:15 - 2:17Treating the paintings as square grids,
-
2:17 - 2:21he can fill in one pixel,
or little square, at a time. -
2:21 - 2:25He can move forwards
and make 90 degree turns over the canvas, -
2:25 - 2:28but can’t move diagonally.
-
2:28 - 2:33How does Ethic program Hedge
to paint an X over each portrait? -
2:33 - 2:41Pause now to figure it out for yourself.
-
2:41 - 2:43Here’s a hint.
-
2:43 - 2:46Try drawing a square grid like this,
-
2:46 - 2:48and simulating Hedge’s path over it.
-
2:48 - 2:51What patterns can you find to guide him?
-
2:51 - 2:56Pause now to figure it out for yourself.
-
2:56 - 2:58The challenge here
is to craft a set of instructions -
2:58 - 3:01that will work for any square grid.
-
3:01 - 3:04Fortunately,
one of the strengths of programming -
3:04 - 3:07is the flexibility
to solve not just one problem, -
3:07 - 3:10but a whole class of them all at once.
-
3:10 - 3:15It often helps to start with one case,
and work towards the general. -
3:15 - 3:17Let’s say we had this square.
-
3:17 - 3:21Hedge can measure the length of its sides
and store that number as a variable. -
3:21 - 3:26Now, what we need is a plan
for how Hedge will paint an X, -
3:26 - 3:27pixel by pixel.
-
3:27 - 3:30There’s more than one right answer
for how to do this; -
3:30 - 3:32let’s look at two.
-
3:32 - 3:35First, what if Hedge went row by row,
like a typewriter? -
3:35 - 3:38If it’s a 9 pixel by 9 pixel painting,
-
3:38 - 3:43in the first row he’d paint,
skip 7, and then paint again. -
3:43 - 3:47In the second row he’d skip the first,
paint, skip 5, and paint. -
3:47 - 3:49And so on.
-
3:49 - 3:53The pattern here is that for each row
the pixels skipped at the beginning -
3:53 - 3:54go up by one,
-
3:54 - 3:58and the pixels skipped in the middle
go down by 2. -
3:58 - 4:01Things get more complicated
when Hedge reaches the center. -
4:01 - 4:04Here there’s a row
with just one pixel painted. -
4:04 - 4:06Then the whole thing reverses—
-
4:06 - 4:10the number of pixels skipped
goes down by one each time on the left, -
4:10 - 4:14and up by two each time in the middle.
-
4:14 - 4:17Instructing Hedge to do this
with a series of loops will work -
4:17 - 4:20and is a perfectly fine solution.
-
4:20 - 4:23The main drawback is that
this requires quite a bit of logic— -
4:23 - 4:26knowing what to do in the middle,
when to reverse the process, -
4:26 - 4:29and exactly how to reverse it.
-
4:29 - 4:31So how might we approach this
-
4:31 - 4:35so that the logic remains consistent
from start to finish? -
4:35 - 4:40The key insight is to look at a grid
as a series of concentric squares. -
4:40 - 4:43Each square follows the same pattern—
-
4:43 - 4:47painted pixels in the corners,
and unaltered pixels in between. -
4:47 - 4:50So if we can figure out a way
to paint one nested square, -
4:50 - 4:55transition to the next, and repeat,
we can paint them all. -
4:55 - 4:57Painting the outermost one is easy.
-
4:57 - 5:00Start in a corner and paint that pixel.
-
5:00 - 5:02If we call the length of the painting n,
-
5:02 - 5:05fly forward n minus 1 spaces.
-
5:05 - 5:07Paint another pixel, and turn right.
-
5:07 - 5:11Now do the whole thing again… and again.
-
5:11 - 5:16Now move forward one less space,
turn right, fly forward once, -
5:16 - 5:19and Hedge will be
in the next concentric square -
5:19 - 5:22and ready to repeat the whole process.
-
5:22 - 5:28Each square is n minus 2 pixels smaller
than the last in length and width, -
5:28 - 5:32and we can follow this spiral pattern
all the way to the center -
5:32 - 5:37with a loop and a variable
that tracks how far Hedge should fly. -
5:37 - 5:39Is one of these methods
better than the other? -
5:39 - 5:41It really depends on what you value.
-
5:41 - 5:45The strength of the spiral
is the simplicity of finding a pattern -
5:45 - 5:48and reusing the same logic
from start to finish. -
5:48 - 5:50The advantage of the typewriter approach
-
5:50 - 5:52is that it’s a more generalized solution,
-
5:52 - 5:56meaning it can be adapted
much more simply to fill in any pattern. -
5:56 - 5:59For Ethic’s sake,
either will do just fine. -
5:59 - 6:01So here’s what happens.
-
6:01 - 6:04Hedge rapidly defaces
all of the portraits. -
6:04 - 6:05And within moments
-
6:05 - 6:09cries of anguish
break out all over the forest. -
6:09 - 6:12The garrison guarding the tower
abandon their posts -
6:12 - 6:14to calm the agitated people,
-
6:14 - 6:17and Ethic, Hedge,
and Octavia slip through— -
6:17 - 6:20and nearly slip
into the depths of the gorge -
6:20 - 6:23standing between them and the tower.
- Title:
- The Artists | Think Like A Coder, Ep 5
- Speaker:
- Alex Rosenthal
- Description:
-
View full lesson: https://ed.ted.com/lessons/the-artists-think-like-a-coder-ep-5
This is episode 5 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:
- 06:25
![]() |
lauren mcalpine edited English subtitles for The Artists | Think Like A Coder, Ep 5 | |
![]() |
lauren mcalpine edited English subtitles for The Artists | Think Like A Coder, Ep 5 | |
![]() |
Elise Haadsma approved English subtitles for The Artists | Think Like A Coder, Ep 5 | |
![]() |
Elise Haadsma accepted English subtitles for The Artists | Think Like A Coder, Ep 5 | |
![]() |
lauren mcalpine edited English subtitles for The Artists | Think Like A Coder, Ep 5 |