The Train Heist Think Like A Coder Ep 4

Ethic, Hedge, and Adila, the leader
of the revolution,

plot out how they can steal an artifact
called the Node of Power.

It’s being used to run a heavily fortified
train that runs all around the country,

providing supplies
to settlements and facilities.

This armored behemoth

undergoes a complex and unpredictable
unloading procedure—

a procedure which is displayed, in detail,
on a screen within the engine car.

Right means the train will go one
car length forward,

and left means the train will go the
same distance backwards.

While unloading, the train frequently
moves back and forth,

so a typical sequence
might look like this.

Also within the engine car is a button
that can only be pressed once.

When pressed, it lets down the force
field over the artifact for 10 seconds.

The engine car is tiny
and designed for a robot.

Of your team, only Hedge can fit.

The members of the resistance have
positioned a crane over the train tracks

that can pluck the artifact
once it’s exposed.

They’ll know when to lower
the crane by sight.

But the only way Hedge can determine
the train’s position

and know when to lower
the force field

is by analyzing the unloading procedure,

because he’ll be inside
the windowless engine car.

Hedge can’t program himself though, so
it’s up to Ethic to tell him what to do.

The artifact is in the car
10 positions behind

the one that’s directly under the
crane at the start.

What instructions can Ethic give Hedge

so that he hits the button
at just the right moment?

Here’s a hint to get you started.

The key to this problem, as with many
programming challenges,

is to reframe the information in a way
that a computer can work with.

A computer doesn’t know
what a train is, nor does it need to.

It can, however, work with variables.

Try making a variable that tracks
the train’s position.

How will it change as the train moves?

Let’s start by breaking this problem
into two objectives.

The first is to know where the train will
be as it carries out its instructions.

The second is to hit the button when
the train is in just the right position.

For the first objective it’ll help to
think of the train as a big number line.

Let’s make 0 the car with the node,
1 the car in front of it, and so on.

That means car 10 is under
the crane at the start.

When the train moves one car right,
car 9 is under the crane.

So a right arrow can be
thought of as “subtract 1.”

And when the train moves left
from there,

10 is back under the crane,

making a left arrow the same as “add 1."

Let’s set our train position variable
to 10, since that’s where we start.

We can now use a loop
to read the instructions one at a time,

adding or subtracting as we go,
to track which car is under the crane.

The nice thing about setting up
the variable this way

is that it tells us how far the node
is from the crane.

So as soon as the variable hits 0,
Hedge should hit the button.

And here’s what happens.

Ethic gets into position on the crane
while Hedge rushes off

and slips into the engine car unnoticed,
just before the train lurches to life.

It rolls 3 cars back. 1 forward,
another 4 back.

Then so far forward Ethic loses track
before it reverses once more.

When the artifact
finally rolls into position,

Adila lowers the crane,
hoping Ethic and Hedge got it right.

At the last possible moment,
the force field sputters and falls.

Ethic swoops in, and lifts the
Node of Power to freedom.

When Ethic gives the node
to Hedge for safe keeping,

something incredible happens.

The artifact shimmers to life
with a vision of the past:

When the crystal was unearthed,
no one could make the console inside work.

The government put out a call
for people to try their luck with it,

one at a time.

Ethic loved to figure out what made
things tick, so she signed up.

Within moments at the console,
something clicked into place,

and she created her first robot.

The government hired Ethic as
chief robotics engineer on the spot.

Within a year her creations ran almost
every aspect of society,

and the nation and its people thrived,

no longer needing to toil
in the fields and factories.

The vision ends, and Hedge detects
the second artifact in the 198forest,

to the southeast.

Luckily, the train is going there next,

and has just enough reserve
fuel for the trip.

Ethic and Hedge smuggle themselves aboard

and find a hiding spot
for the long journey ahead.

Ethic、Hedge 和
革命领袖

Adila 计划如何窃取
名为“权力节点”的神器。

它被用来
运行在全国各地运行的重型火车,

为定居点和设施提供物资。

这个装甲庞然大物

经历了一个复杂且不可预测的
卸载程序——

该程序详细
显示在引擎车内的屏幕上。

右表示火车将向前行驶一节
车厢

,左表示火车将
向后行驶相同的距离。

卸货时,火车经常
来回移动,

因此典型的序列
可能如下所示。

引擎车内还有
一个只能按一次的按钮。

按下时,它会降低
神器上方的力场 10 秒。

引擎车很小
,专为机器人设计。

在您的团队中,只有 Hedge 可以适应。

抵抗组织的成员
在火车轨道上放置了一台起重机,

一旦它暴露,它就可以将它拔出。

他们会
通过视觉知道何时降低起重机。

但赫奇
确定火车位置

并知道何时
降低力场的唯一方法

是分析卸载程序,

因为他将
在无窗引擎车内。

不过,Hedge 不能自己编程,
所以由 Ethic 来告诉他该怎么做。

神器在开始时位于起重机正下方的汽车
后面 10 个位置

Ethic 可以给 Hedge 什么指示,

以便他
在正确的时刻按下按钮?

这里有一个提示,可以帮助您入门。

与许多编程挑战一样,这个问题的关键

是以计算机可以处理的方式重新构建信息。

计算机不
知道火车是什么,也不需要知道。

但是,它可以使用变量。

尝试制作一个
跟踪火车位置的变量。

随着火车的行驶,它将如何变化?

让我们首先将这个问题
分为两个目标。

首先是知道火车
在执行指令时将在哪里。

第二种是
在火车处于正确位置时按下按钮。

对于第一个目标,
将火车视为一个大数字线会有所帮助。

让我们将带有节点的汽车设为 0,将
其前面的汽车设为 1,以此类推。

这意味着汽车 10
在开始时位于起重机下方。

当火车向右移动一节车厢时,
9号车厢在起重机下方。

所以右箭头可以被
认为是“减 1”。

当火车从那里向左移动时

10 回到起重机下方,

使左箭头与“加 1”相同。

让我们将火车位置变量设置
为 10,因为这是我们开始的地方。

我们现在可以使用循环
来 一次阅读一个指令,

边走边加减,
以跟踪哪辆车在起重机下。


这种方式设置变量的好处

是它告诉我们
节点离起重机有多远。

所以 一旦变量达到 0,
Hedge 应该按下按钮

。这就是发生的事情

。Ethic 在起重机上就位,
而 Hedge 冲下

并在不被注意的情况下滑入发动机车厢,
就在火车突然恢复生机之前。

它滚回了 3 节车厢 . 1 个向前,
另外 4 个向后。

然后到目前为止,Ethic
在它再次反转之前失去了轨道。

当神器
最终滚动到位时,

Adila 降低了起重机,
希望 Ethic 和 Hedge 做对了。

在最后可能的时刻,
原力 场溅射和坠落。

伦理猛扑进来,并举起
节点 自由的力量。

当 Ethic 将节点
交给 Hedge 进行安全保管时,

不可思议的事情发生了。

这件神器闪烁
着对过去的憧憬:

当水晶出土时,
没有人能让里面的控制台正常工作。

政府
呼吁人们一次次尝试运气

Ethic 喜欢弄清楚是什么让事情发生了变化
,所以她报名了。

在控制台的瞬间,
一些东西点击到位

,她创造了她的第一个机器人。

政府当场聘请 Ethic 为
首席机器人工程师。

一年之内,她的创作
几乎遍及社会的方方面面

,国家和人民繁荣昌盛,

不再需要
在田野和工厂劳作。

幻象结束,
赫奇在东南方的 198 森林中发现了第二个神器

幸运的是,火车接下来要去那里,

并且有足够的储备
燃料供旅行使用。

Ethic 和 Hedge 偷偷上船

,为接下来
的漫长旅程寻找藏身之处。