For this question I'm going to explain
how we take an input string,
which in this case is
dot dot dot x dot dot dot dot,
and we change it according to some rules.
Now the rules are:
look at three characters in the string
and change the middle one
according to the rules that we have here.
Don't worry about the numbers for
the moment, I'll come to those later.
So for this particular three,
we have dot dot x.
Now dot dot x says change
the middle element
so that it's an x.
So on the next string, the new string,
that position is going to be an x.
The position before it
dot dot dot
well dot dot dot becomes a dot
in the middle.
Now at the ends
we want to link around
so this dot here
has to its left a dot
and to its right a dot,
so we have dot dot dot again
and dot dot dot says change the middle
to a dot.
Now moving along,
the next one we have to
fill in is dot x dot.
Dot x dot says change the middle to an x.
x dot dot,
change the middle to an x
dot dot dot again,
middle is a dot
dot dot dot, middle is a dot
dot dot and then
the first one is the last one here
so dot dot dot again is a dot.
Now that completes
one change of the string
and we're going to repeat that again.
So, That's the first generation.
This is the second generation.
Now normally when you do this, you'd
start at the beginning of the string
but just to explain it, it was easier
to show you one in the middle.
So this time we'll do it
from the beginning of the string.
So looking at the line above,
we see that the first
element in the string
has a dot to its left,
and a dot to its right,
so it's dot dot dot,
so that's a dot.
Now we have dot dot x,
and dot dot x, that's number two here,
gives me an x.
dot x x is an x,
x x x is a dot.
x x dot, is a dot.
x dot dot is an x.
dot dot dot, is a dot.
And dot dot dot is also a dot.
And we can continue like that.
And so after five generations,
we have the string
dot dot dot x x dot x x.
And you see we have quite
an interesting pattern building up.
Now, we can do this for lots of different
choices of these dots and x's
in blue, on the second line.
In fact, we can do it
in 256 different ways.
And in order to decide what
pattern we're going to use, what rules rather,
we can give this rule a number.
Now, wherever I've got an x on the bottom row,
I'm going to add up those numbers,
so 2 + 4 + 8 + 16,
and that gives me 30.
So this here uses pattern 30.
Now instead of pattern 30, we could
choose a different pattern.
Let's say we choose pattern 69.
Now 69, is equal to,
now we have to look at these
numbers above and
work out how to make 69
out of those numbers.
Well, 128 doesn't go into 69,
so we don't need any of those.
64 does so we want a 64.
We don't want any 32's.
We don't want any 16's, nor 8.
But we do need 4,
don't need 2, and we do need 1.
So 64 + 4 + 1, 69.
So that means, that we want 1.
That means were going
to replace dot dot dot,
the middle element by an x.
No 2's. We do have a 4.
We don't have an eight.
We don't have 16.
We don't have 32.
We do have 64,
and we don't have 128.
So, we could start off
with the same input as before,
which was, dot dot dot x dot dot dot dot.
So when we have a dot at the beginning,
its neighbor on the left is a dot,
and its neighbor on the right is a dot,
we have dot dot dot,
and that's replaced with an x.
dot dot dot again, is an x.
dot dot x, is a dot.
Remember, we are looking down here.
dot x dot, That's number 4, is an x.
x dot dot is a dot.
dot dot dot is an x.
and dot dot dot is an x, as well.
So that's line 1.
Continuing with the same thing,
I'll fill in a few more lines,
so that we can see
what happens with the pattern.
And now I come to line 6,
I see that I am getting exactly
the same as I had in line 5,
which means that line 7
will also be exactly the same.
Now, some of the values give you
these patterns that just becomes stable,
others are just chaotic and so there's
lots of different things going on
that can be quite interesting to look at.
Now to come to the actual code.
What I'm asking you to do is to take as input, a string,
which is a string of dots and x's,
a pattern number, which is between 0 and 255,
and you're going to have to use that
to work out where the dots and x's go.
You will be supplied
with this information here,
in other words, the numbers
that correspond with the x x dot.
which is the pattern that you look up.
And finally, a number of generations,
and that's a positive integer.
1,2,3,4 and so on.
And your output should be the generation,
or the string at that generation n.
So, for instance, if the
input string was, as we have here,
the pattern was 69
and the generation was five,
you would return '.x.x.x.x'
この問題では入力文字列の使い方を
説明していきます
この場合は...x ....です
いくつかのルールに従ってこれを変更します
ルールを説明しましょう
文字列の中の3文字に目を向けます
そして右にあるルールに従って
下の列の中央の1文字に変更します
この時点で上の数字は気にしないでください
後ほど説明します
特定した3文字は..xです
..xを中央の文字に変更するとxになります
次の文字列で新たな文字列になりますが
この位置はxになります
その前の位置に来る文字は
...から与えられます
...の中央は.になります
端の文字はつなげる必要があります
ここにあるこの.をこの左の.と
この右にある.につなげます
再び...になります
...の中央を.に変えます
次に移動します
次に入力しなければならないのは.x .です
.x .の中央はxに変わるとなっています
x ..の中央をxに変えます
再び...です
中央は.です
...なので中央が.です
..そして1つ目の文字がこの最後に来ます
ですから再び...となり.になります
これで文字列の1つの変更を終了します
これをもう一度繰り返します
先ほどの結果は1世代です
これが2世代です
一般的にこれを行う時は
文字列の最初から開始します
説明するだけでしたので
中央の文字の変更を示す方が簡単でした
それでは今回は文字列の始めから
繰り返しを行ってみましょう
上の行を見ます
文字列の1つ目の要素がその左側に.を有していて
右側に.があるので つまり...です
ですから.になります
次は..xがあります
そして..xはというと
このルールの2つ目でxを与えています
.x xはxです
xxxは.です
xx .は.です
x ..はxです
...は.です
そして...も.です
このように続けることができます
こうして5世代後には
文字列...xx .xxになります
とても面白いパターンを作り上げていますね
ルールの2行目の青色のxとドットの
異なる様々な組み合わせで
これを行うことができます
実に256通りもできます
どのパターンを使用するか
正確にはどのルールにするのか決定するため
このルールに数字を与えます
そして最終行にxがある場合は常に
この数字を加算します
つまりここでは2+4+8+16です
そうすると結果は30となります
なのでここではパターン30を使用しています
パターン30の代わりに
異なるパターンを選ぶこともできます
パターン69を選択すると仮定しましょう
69を成すものは何でしょう
先ほどの数字を探す必要があります
そしてどのようにその数字から
69を作成するか算出します
128は69になりません
このような数字は必要ありません
64にはなります ですから64が必要です
32は必要ありません
16や8も必要ありません
ですが4は必要です
2は必要ありませんが1は必要です
これで64+4+1で69になります
つまり必要なのは1ということです
そして...の中央の文字を
xで置き換えるということです
そして2はありません 4はあります
8はありません
16はありません
32はありません
64はあります
128はありません
以前と同じ入力から始めることができます
それは...x ....でした
最初にドットがある時 その左隣はドットです
その右隣もドットです
...があります それはxで置き換えます
...が再びxになります
.. xは.です
この下の文字を忘れないでください
. x .つまりルールの数字4はxです
x ..は.です
...はxです 次も同じです
その次の...もxになります
これが行1です
同じことを続けます さらに数行入力します
そうすることで
パターンによって起きることが確認できます
行6を見てみます
行5にあるものと
まったく同じであることが確認できます
つまり行7もまったく同じになることを意味しています
このように固定されるパターンを与える値があります
カオス的になる値もあり様々なことが起きているのです
検証してみるととても面白いですね
それでは実際のコードに戻りましょう
皆さんに行ってほしいことは
入力に文字列を使用することです
それはドットとxの文字列です
パターン数はゼロと255の間です
ドットとxの動きを作り出すために
それを使用しなければなりません
この情報を提供します
つまりxx.などの文字列に一致する数です
これがあなたが探すパターンです
そして最後に世代の数です それは正の整数です
1、2、3、4などです
あなたの出力はその世代または
世代nの文字列になるはずです
例えば入力文字列がここにあるものだったら
パターンは69で世代は5です
.x .x .x .xを返します