The Train Heist | Think Like A Coder, Ep 4

533,259 views ・ 2019-12-09

TED-Ed


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

00:21
Ethic, Hedge, and Adila, the leader of the revolution,
0
21570
3790
00:25
plot out how they can steal an artifact called the Node of Power.
1
25360
5179
00:30
It’s being used to run a heavily fortified train that runs all around the country,
2
30539
5557
00:36
providing supplies to settlements and facilities.
3
36096
3730
00:39
This armored behemoth
4
39826
1628
00:41
undergoes a complex and unpredictable unloading procedure—
5
41454
3988
00:45
a procedure which is displayed, in detail, on a screen within the engine car.
6
45442
5384
00:50
Right means the train will go one car length forward,
7
50826
3560
00:54
and left means the train will go the same distance backwards.
8
54386
4001
00:58
While unloading, the train frequently moves back and forth,
9
58387
3842
01:02
so a typical sequence might look like this.
10
62229
4960
01:07
Also within the engine car is a button that can only be pressed once.
11
67189
5085
01:12
When pressed, it lets down the force field over the artifact for 10 seconds.
12
72274
5390
01:17
The engine car is tiny and designed for a robot.
13
77664
3750
01:21
Of your team, only Hedge can fit.
14
81414
3395
01:24
The members of the resistance have positioned a crane over the train tracks
15
84809
3908
01:28
that can pluck the artifact once it’s exposed.
16
88717
3167
01:31
They’ll know when to lower the crane by sight.
17
91884
2995
01:34
But the only way Hedge can determine the train’s position
18
94879
3360
01:38
and know when to lower the force field
19
98239
2287
01:40
is by analyzing the unloading procedure,
20
100526
3128
01:43
because he’ll be inside the windowless engine car.
21
103654
3968
01:47
Hedge can’t program himself though, so it’s up to Ethic to tell him what to do.
22
107622
5376
01:52
The artifact is in the car 10 positions behind
23
112998
3620
01:56
the one that’s directly under the crane at the start.
24
116618
3163
01:59
What instructions can Ethic give Hedge
25
119781
2704
02:02
so that he hits the button at just the right moment?
26
122485
3748
02:13
Here’s a hint to get you started.
27
133506
3550
02:17
The key to this problem, as with many programming challenges,
28
137056
3857
02:20
is to reframe the information in a way that a computer can work with.
29
140913
4381
02:25
A computer doesn’t know what a train is, nor does it need to.
30
145294
4620
02:29
It can, however, work with variables.
31
149914
3110
02:33
Try making a variable that tracks the train’s position.
32
153024
4313
02:37
How will it change as the train moves?
33
157337
2957
02:46
Let’s start by breaking this problem into two objectives.
34
166362
3650
02:50
The first is to know where the train will be as it carries out its instructions.
35
170012
4873
02:54
The second is to hit the button when the train is in just the right position.
36
174885
5184
03:00
For the first objective it’ll help to think of the train as a big number line.
37
180069
4669
03:04
Let’s make 0 the car with the node, 1 the car in front of it, and so on.
38
184738
4854
03:09
That means car 10 is under the crane at the start.
39
189592
3690
03:13
When the train moves one car right, car 9 is under the crane.
40
193282
4449
03:17
So a right arrow can be thought of as “subtract 1.”
41
197731
3810
03:21
And when the train moves left from there,
42
201541
2270
03:23
10 is back under the crane,
43
203811
1840
03:25
making a left arrow the same as “add 1."
44
205651
3757
03:29
Let’s set our train position variable to 10, since that’s where we start.
45
209408
4455
03:33
We can now use a loop to read the instructions one at a time,
46
213863
4330
03:38
adding or subtracting as we go, to track which car is under the crane.
47
218193
5104
03:43
The nice thing about setting up the variable this way
48
223297
2750
03:46
is that it tells us how far the node is from the crane.
49
226047
3316
03:49
So as soon as the variable hits 0, Hedge should hit the button.
50
229363
4064
03:53
And here’s what happens.
51
233427
1810
03:55
Ethic gets into position on the crane while Hedge rushes off
52
235237
3674
03:58
and slips into the engine car unnoticed, just before the train lurches to life.
53
238911
5898
04:04
It rolls 3 cars back. 1 forward, another 4 back.
54
244809
4893
04:09
Then so far forward Ethic loses track before it reverses once more.
55
249702
5819
04:15
When the artifact finally rolls into position,
56
255521
3021
04:18
Adila lowers the crane, hoping Ethic and Hedge got it right.
57
258542
3790
04:22
At the last possible moment, the force field sputters and falls.
58
262332
4372
04:26
Ethic swoops in, and lifts the Node of Power to freedom.
59
266704
5011
04:31
When Ethic gives the node to Hedge for safe keeping,
60
271715
2910
04:34
something incredible happens.
61
274625
2091
04:36
The artifact shimmers to life with a vision of the past:
62
276716
3850
04:40
When the crystal was unearthed, no one could make the console inside work.
63
280566
4806
04:45
The government put out a call for people to try their luck with it,
64
285372
3250
04:48
one at a time.
65
288622
1452
04:50
Ethic loved to figure out what made things tick, so she signed up.
66
290074
4323
04:54
Within moments at the console, something clicked into place,
67
294397
3143
04:57
and she created her first robot.
68
297540
2760
05:00
The government hired Ethic as chief robotics engineer on the spot.
69
300300
5014
05:05
Within a year her creations ran almost every aspect of society,
70
305314
4170
05:09
and the nation and its people thrived,
71
309484
2149
05:11
no longer needing to toil in the fields and factories.
72
311633
3475
05:21
The vision ends, and Hedge detects the second artifact in the 198forest,
73
321047
6763
05:27
to the southeast.
74
327810
1460
05:29
Luckily, the train is going there next,
75
329270
2336
05:31
and has just enough reserve fuel for the trip.
76
331606
3034
05:34
Ethic and Hedge smuggle themselves aboard
77
334640
2557
05:37
and find a hiding spot for the long journey ahead.
78
337197
3558
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