The Artists | Think Like A Coder, Ep 5

436,291 views ・ 2020-01-13

TED-Ed


Please double-click on the English subtitles below to play the video.

00:22
Dawn and the train are both breaking when Ethic and Hedge arrive in the woods.
0
22840
5488
00:28
The adventurers have recovered the first artifact—
1
28328
2880
00:31
the Node of Power—
2
31208
1497
00:32
and have come to the 198forest in search of the second.
3
32705
4030
00:36
Here they’re welcomed by the director of the colony, Octavia.
4
36735
5111
00:41
She established this treehouse sanctuary
5
41846
2600
00:44
after the robots freed everyone from having to work.
6
44446
2969
00:47
It was meant to be a haven where people could follow their passions,
7
47415
3290
00:50
take up crafts, and find fulfillment.
8
50705
2370
00:53
Which they did… at first.
9
53075
2263
00:55
Some years ago everyone forgot the point.
10
55338
2510
00:57
They abandoned arts and crafts
11
57848
1630
00:59
and instead just painted and exhibited pictures of themselves
12
59478
3200
01:02
over, and over, and over.
13
62678
2600
01:05
The location of the second artifact is no secret;
14
65278
2610
01:07
it’s in a tower, guarded by a garrison of bots,
15
67888
3460
01:11
a bottomless ravine, and who knows what other traps.
16
71348
4150
01:15
As soon as the tower went up with the node inside,
17
75498
3158
01:18
human communication across the land went dark.
18
78656
3922
01:22
Octavia’s been after it for years,
19
82578
2050
01:24
but try as she might, the defenses thwart her.
20
84628
4245
01:28
In order to even get to the tower, the team will need a distraction.
21
88873
4258
01:33
Octavia has an idea:
22
93131
1849
01:34
stir up the people through some well-intentioned vandalism.
23
94980
4131
01:39
The residents’ paintings are all squares that come in different sizes,
24
99111
3832
01:42
all an odd number of pixels across.
25
102943
3608
01:46
Helper-bots pick up the finished portraits
26
106551
2412
01:48
and hang them in public places for everyone to admire.
27
108963
3848
01:52
There’s a slim margin of time when Hedge can access the paintings.
28
112811
4190
01:57
If he were to deface each one with an X,
29
117001
3361
02:00
the people would blame the helper-bots,
30
120362
2439
02:02
creating just the distraction the team needs.
31
122801
3410
02:06
If only it were so easy.
32
126211
2300
02:08
Hedge can’t just paint an X—
33
128511
2230
02:10
his painting processor requires very specific instructions.
34
130741
4121
02:14
Treating the paintings as square grids,
35
134862
2630
02:17
he can fill in one pixel, or little square, at a time.
36
137492
4000
02:21
He can move forwards and make 90 degree turns over the canvas,
37
141492
3720
02:25
but can’t move diagonally.
38
145212
2728
02:27
How does Ethic program Hedge to paint an X over each portrait?
39
147940
4802
02:32
Pause now to figure it out for yourself.
40
152742
8528
02:41
Here’s a hint.
41
161270
1530
02:42
Try drawing a square grid like this,
42
162800
2702
02:45
and simulating Hedge’s path over it.
43
165502
2950
02:48
What patterns can you find to guide him?
44
168452
2556
02:51
Pause now to figure it out for yourself.
45
171008
4790
02:55
The challenge here is to craft a set of instructions
46
175798
2599
02:58
that will work for any square grid.
47
178397
2791
03:01
Fortunately, one of the strengths of programming
48
181188
2570
03:03
is the flexibility to solve not just one problem,
49
183758
2980
03:06
but a whole class of them all at once.
50
186738
3110
03:09
It often helps to start with one case, and work towards the general.
51
189848
4801
03:14
Let’s say we had this square.
52
194649
1970
03:16
Hedge can measure the length of its sides and store that number as a variable.
53
196619
4479
03:21
Now, what we need is a plan for how Hedge will paint an X,
54
201098
4585
03:25
pixel by pixel.
55
205683
1540
03:27
There’s more than one right answer for how to do this;
56
207223
2952
03:30
let’s look at two.
57
210175
1483
03:31
First, what if Hedge went row by row, like a typewriter?
58
211658
3638
03:35
If it’s a 9 pixel by 9 pixel painting,
59
215296
3023
03:38
in the first row he’d paint, skip 7, and then paint again.
60
218319
4309
03:42
In the second row he’d skip the first, paint, skip 5, and paint.
61
222628
4818
03:47
And so on.
62
227446
1330
03:48
The pattern here is that for each row the pixels skipped at the beginning
63
228776
4242
03:53
go up by one,
64
233018
1410
03:54
and the pixels skipped in the middle go down by 2.
65
234428
3650
03:58
Things get more complicated when Hedge reaches the center.
66
238078
3080
04:01
Here there’s a row with just one pixel painted.
67
241158
3041
04:04
Then the whole thing reverses—
68
244199
1969
04:06
the number of pixels skipped goes down by one each time on the left,
69
246168
4123
04:10
and up by two each time in the middle.
70
250291
3366
04:13
Instructing Hedge to do this with a series of loops will work
71
253657
3454
04:17
and is a perfectly fine solution.
72
257111
2439
04:19
The main drawback is that this requires quite a bit of logic—
73
259550
3601
04:23
knowing what to do in the middle, when to reverse the process,
74
263151
3020
04:26
and exactly how to reverse it.
75
266171
2690
04:28
So how might we approach this
76
268861
1784
04:30
so that the logic remains consistent from start to finish?
77
270645
3916
04:34
The key insight is to look at a grid as a series of concentric squares.
78
274561
5488
04:40
Each square follows the same pattern—
79
280049
2640
04:42
painted pixels in the corners, and unaltered pixels in between.
80
282689
4222
04:46
So if we can figure out a way to paint one nested square,
81
286911
3418
04:50
transition to the next, and repeat, we can paint them all.
82
290329
4435
04:54
Painting the outermost one is easy.
83
294764
2361
04:57
Start in a corner and paint that pixel.
84
297125
2489
04:59
If we call the length of the painting n,
85
299614
2440
05:02
fly forward n minus 1 spaces.
86
302054
2750
05:04
Paint another pixel, and turn right.
87
304804
2500
05:07
Now do the whole thing again… and again.
88
307304
3530
05:10
Now move forward one less space, turn right, fly forward once,
89
310834
5195
05:16
and Hedge will be in the next concentric square
90
316029
3170
05:19
and ready to repeat the whole process.
91
319199
2785
05:21
Each square is n minus 2 pixels smaller than the last in length and width,
92
321984
5742
05:27
and we can follow this spiral pattern all the way to the center
93
327726
3920
05:31
with a loop and a variable that tracks how far Hedge should fly.
94
331646
5147
05:36
Is one of these methods better than the other?
95
336793
2240
05:39
It really depends on what you value.
96
339033
2064
05:41
The strength of the spiral is the simplicity of finding a pattern
97
341097
3430
05:44
and reusing the same logic from start to finish.
98
344527
3040
05:47
The advantage of the typewriter approach
99
347567
2100
05:49
is that it’s a more generalized solution,
100
349667
2290
05:51
meaning it can be adapted much more simply to fill in any pattern.
101
351957
4070
05:56
For Ethic’s sake, either will do just fine.
102
356027
3271
05:59
So here’s what happens.
103
359298
1830
06:01
Hedge rapidly defaces all of the portraits.
104
361128
3109
06:04
And within moments
105
364237
1110
06:05
cries of anguish break out all over the forest.
106
365347
3671
06:09
The garrison guarding the tower abandon their posts
107
369018
2980
06:11
to calm the agitated people,
108
371998
1970
06:13
and Ethic, Hedge, and Octavia slip through—
109
373968
2990
06:16
and nearly slip into the depths of the gorge
110
376958
3040
06:19
standing between them and the tower.
111
379998
2889
About this website

This site will introduce you to YouTube videos that are useful for learning English. You will see English lessons taught by top-notch teachers from around the world. Double-click on the English subtitles displayed on each video page to play the video from there. The subtitles scroll in sync with the video playback. If you have any comments or requests, please contact us using this contact form.

https://forms.gle/WvT1wiN1qDtmnspy7