The Gauntlet  Think Like A Coder Ep 8

Their fall from the tower sends
Ethic and Hedge

spinning into the rapids
of a river of pure energy.

This torrent flows from the Bradbarrier
all the way to Huxenborg.

There an entire city’s worth of factories

build the robots
and house the Node of Memory,

the last of the three powerful artifacts
Ethic needs to collect.

After a long day and a longer night

they find themselves
in a canyon of brick and steel.

Just when they’re about to reach
the end of the line,

a rope catches them.

Their savior, Lemma,
has been waiting for them.

When Ethic claimed the Node of Creation
from the forest tower,

radios all across the land
came back to life.

Adila, the resistance leader,
immediately started contacting her allies,

none more important than Lemma,

a brilliant scientist working from within
Huxenborg to bring down the machines.

Unfortunately, the radios
also tipped off the robots.

So they’ve taken defensive measures

to protect the final artifact in its home
in the very heart of the city.

There’s only one way to get there:
the gauntlet of forking paths.

It’s a deadly series of luminous conveyors
that wind underneath Huxenborg.

Starting from the current position,

each section runs for a distance,
then splits in two.

Every branch does the same thing,
again and again.

There are thousands of branches.

Only one path leads to the artifact;
all the others to destruction.

Fortunately, the Node of Creation
has granted Hedge a strange power:

he can produce slightly smaller versions
of himself.

Each version can do only two things:
radio information back to its parent,

and produce slightly smaller
versions of itself…

which can do the same two things,
as can their children,

for as many generations as needed.

A patrol is closing in on their position,
so Ethic’s time is limited.

What instructions should she give
Hedge to find the one safe path?

Pause the video to figure it out yourself.

Hint in 3

Hint in 2

Hint in 1

Programmers have an elegant tool
in their arsenal called recursion.

Recursion is when you have a set of
instructions that refers back to itself.

It’s like using a word
in its own definition,

except where that’s frowned upon,
this is incredibly effective.

Recursion involves repetition,
but in a different way than loops.

Where a loop takes one action
and repeats it again and again,

recursion will start an action,
and before it’s finished, use it again,

and before that’s finished,
use it again, and so on.

It keeps doing this until
some end state is reached.

It then passes the information back up,
layer after layer,

until it reaches the top
and ends the cycle.

Recursion is ideal for problems
that involve self-similarity,

where each part resembles
the larger whole.

Like, for example, a deadly defense
system designed to end any person or thing

who dares tread upon it.

Pause the video to figure it out yourself.

Solution in 3

Solution in 2

Solution in 1

Ethic’s conundrum seems sprawling
on the surface,

but there’s a remarkably simple
solution to it using recursion.

In order to find it, let’s first look
at the simplest version of this puzzle:

what if the entire maze
were just two paths?

If Hedge copies himself, the copy
that goes the wrong way will be destroyed.

So the other one,
which will reach the artifact,

can radio back the path it took,
and then no matter which way is correct,

that’s the answer Hedge will receive.

This is called the “base case”
of the recursion.

Now, suppose the maze
branches twice from the starting point,

and at every intersection,
Hedge’s copies—

let’s call them Twig 1 and Twig 2—

make more copies—
let’s call them Leaves 1 through 4.

Three Leaves will be destroyed.

The one that reaches the artifact
will radio back the right answer,

but only to its parent.

So if Twig 1 or 2 is waiting
at an intersection

and hears something over the radio,

that’s the right way to go
to the artifact from where it is.

To tell Hedge the right answer
from his perspective,

the Twig should say which way it went,

and then the route
it just heard over the radio.

This same process will work no matter
how many times the maze branches.

Any answer a copy hears on the radio

must be the way to the control room
from its location,

and if it then adds the branch it took,

it can tell its parent
how to get there as well.

We can sum up the instructions
in an action called Pathfinder

that every version of Hedge will follow:

  1. If you’ve reached the artifact,

radio to your parent whether
you got there by going left or right.

  1. When you reach an intersection,
    move off the conveyor

and send new copies down the left
and right paths.

Have them each run Pathfinder.

This is where recursion comes in,

and this may happen many times before
the last instruction triggers, which is:

  1. If you hear anything over the radio,
    you should radio to your parent

whether you got to your spot
by going left or right,

then repeat everything you just heard.

Pathfinder is an example
of what programmers call functions,

subroutines, or procedures.

No matter the terminology,
the idea is the same—

it’s a set of instructions given a label
so that it can be easily reused—

perhaps even by itself.

And in our case that’ll work perfectly—

an entire network of paths mapped
using just three instructions.

So here’s what happens.

By the time the patrol rounds the corner,
Ethic and Lemma have improvised disguises.

They try to confuse the bots
long enough to buy Hedge time.

Finally, Hedge’s radio crackles to life
with a series of directions.

The three dive onto the conveyor
and flee for their lives,

with a squadron of enforcer bots
in hot pursuit.

他们从高塔上坠落,使
Ethic 和

Hedge 陷入了
纯能量河流的急流中。

这股洪流从布拉德堡
一路流向胡森堡。

整座城市的工厂都在

建造机器人
并安置记忆节点,

这是 Ethic 需要收集的三个强大神器中的最后一个

经过漫长的一天和漫长的夜晚,

他们发现自己置身
于砖块和钢铁的峡谷中。

就在他们即将到达队伍
的尽头时,

一根绳子抓住了他们。

他们的救星Lemma
一直在等着他们。

当 Ethic 从森林塔中夺取创造节点时

整个土地的无线电
都恢复了生机。

抵抗运动的领袖阿迪拉
立即开始联系她的盟友,

其中最重要的莫过于莱玛,

一位在
胡森堡内部工作以摧毁机器的杰出科学家。

不幸的是,无线电
也向机器人发出了提示。

因此,他们采取了防御措施

来保护
位于城市中心的家中的最终神器。

只有一种方法可以到达那里:
岔路的挑战。

这是一系列致命的发光传送带
,在 Huxenborg 下方盘旋。

从当前位置开始,

每个部分运行一段距离,
然后分成两部分。

每个分支一次又一次地做同样的事情

有数以千计的分支机构。

只有一条路径通向神器;
其他人都毁灭了。

幸运的是,创造节点
赋予了赫奇一种奇怪的力量:

他可以制造出稍微小一点
的自己。

每个版本只能做两件事:将
无线电信息传回其父代,

并生成稍小
的自身版本……

它可以做同样的两件事
,就像他们的孩子一样,可以

根据需要代代相传。

一支巡逻队正在逼近他们的阵地,
所以 Ethic 的时间有限。

她应该给 Hedge 什么指示
以找到一条安全的路径?

暂停视频以自己弄清楚。

Hint in 3

Hint in 2

Hint in 1

程序员
在他们的武器库中有一个优雅的工具,称为递归。

递归是指当您有一组
指向自身的指令时。

这就像
在自己的定义中使用一个词,

除了不赞成的地方,
这是非常有效的。

递归涉及重复,
但与循环不同。

在循环执行一个动作
并一次又一次地重复它的情况下,

递归将启动一个动作
,在它完成之前再次使用它,

在它完成之前
再次使用它,依此类推。

它一直这样做,直到
达到某种最终状态。

然后,它将信息一层一层地向上传递,

直到到达顶部
并结束循环。

递归对于
涉及自相似性的问题是理想的,

其中每个部分都
类似于更大的整体。

例如,一个致命的防御
系统旨在终结任何

敢于践踏它的人或事物。

暂停视频以自己弄清楚。

3 中的

解决方案 2 中的

解决方案 1 中的解决方案

Ethic 的难题似乎
在表面上蔓延开来,

但使用递归有一个非常简单的
解决方案。

为了找到它,让我们先看
一下这个谜题的最简单版本

:如果整个
迷宫只有两条路径怎么办?

如果 Hedge 复制自己
,走错路的副本将被销毁。

所以
另一个会到达工件的,

可以无线电回它所走的路径,
然后无论哪种方式是正确的,

这就是 Hedge 将收到的答案。

这称为递归的“基本情况
”。

现在,假设迷宫
从起点开始分叉两次

,在每个交叉点,
Hedge 的副本——

我们称它们为 Twig 1 和

Twig 2——制作更多的副本——
我们称它们为叶子 1 到 4。

三片叶子将被破坏。

到达工件的那个
将通过无线电返回正确答案,

但仅限于其父级。

因此,如果 Twig 1 或 2 在十字路口等待

并通过无线电听到某些声音

,那么这是从工件所在位置前往工件的正确方法。

为了从他的角度告诉 Hedge 正确答案

,Twig 应该说出它去哪条路,

然后是
它刚刚通过无线电听到的路线。

无论迷宫分支多少次,相同的过程都将起作用

副本在无线电中听到的任何答案都

必须是从其位置到控制室的路

,如果它随后添加了它所采用的分支,

它也可以告诉其父级
如何到达那里。

我们可以
在一个名为 Pathfinder 的动作中总结

出每个 Hedge 版本都将遵循的指令:

  1. 如果你已经到达了神器,无论你是向左还是向右,都

向你的父母发送无线电

  1. 当你到达一个十字路口时,
    离开传送带

并沿着左右路径发送新副本

让他们各自运行探路者。

这就是递归的用武之地

,这可能会
在最后一条指令触发之前发生很多次,即:

  1. 如果您通过收音机听到任何声音,无论您是向左还是向右,
    您都应该向您的父母广播

然后重复你刚刚听到的一切。

Pathfinder
是程序员称为函数、

子例程或过程的示例。

不管用什么术语
,想法都是一样的——

它是一组带有标签的指令,
这样它就可以很容易地重复使用——甚至可以单独

使用。

在我们的例子中,这将完美地工作——

整个路径网络
只使用三个指令映射。

所以这就是发生的事情。

当巡逻队拐到拐角处时,
埃蒂克和莱玛已经即兴伪装。

他们试图混淆机器人
足够长的时间来购买对冲时间。

最后,Hedge 的收音机
发出一系列的指示。

三人跳上
传送带逃命

,一队执行
机器人紧追不舍。