< Return to Video

The Artists | Think Like A Coder, Ep 5

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

more » « less
Video Language:
English
Team:
closed TED
Project:
TED-Ed
Duration:
06:25

English subtitles

Revisions Compare revisions