Code Golf:康威的人生游戏 [英] Code Golf: Conway's Game of Life

查看:114
本文介绍了Code Golf:康威的人生游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挑战:编写实现John H. Conway的生命游戏细胞自动机的最短程序. [链接]

The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [link]

经过大约一周的比赛,我选择了一个胜利者: pdehaan ,设法以一个的优势击败了Matlab解决方案带有perl的字符.

After about a week of competition, I have selected a victor: pdehaan, for managing to beat the Matlab solution by one character with perl.

对于那些还没有听说过生命游戏"的人,您可以选择一个正方形(最好是无限大)的方格网格.细胞可以是活的(填充的)或死亡的(空的).通过应用以下规则,我们可以确定在下一个步骤中哪些细胞还活着:

For those who haven't heard of Game of Life, you take a grid (ideally infinite) of square cells. Cells can be alive (filled) or dead (empty). We determine which cells are alive in the next step of time by applying the following rules:

  1. 具有少于两个活邻居的任何活细胞都会死亡,好像是由人口不足造成的.
  2. 具有三个以上活邻居的任何活细胞都会死去,就像人满为患一样.
  3. 任何有两个或三个活邻居的活细胞都可以存活到下一代.
  4. 任何具有三个活邻居的死细胞都变成一个活细胞,就像通过繁殖一样.

您的程序将读取一个40x80字符的ASCII文本文件,该文件指定为命令行参数,以及要执行的迭代次数(N).最后,它将在N次迭代后将系统状态输出到ASCII文件out.txt.

Your program will read in a 40x80 character ASCII text file specified as a command-line argument, as well as the number of iterations (N) to perform. Finally, it will output to an ASCII file out.txt the state of the system after N iterations.

以下是运行相关文件的示例:

Here is an example run with relevant files:

in.txt:

................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.............................................
.......................................X........................................
................................XXXXXX.X........................................
................................X...............................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................

重复100次:

Q:\>life in.txt 100

结果输出(out.txt)

................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.X...........................................
....................................X...........................................
................................XXXXX.XX........................................
................................X.....X.........................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................

规则:

  • 您需要使用文件I/O来读写文件.
  • 您需要接受输入文件和迭代次数作为参数
  • 您需要以指定的格式生成out.txt(如果存在则覆盖)
  • 不需要需要处理木板的边缘(环绕式,无限网格等)
  • 要做,您的输出文件中必须包含换行符.
  • You need to use file I/O to read/write the files.
  • You need to accept an input file and the number of iterations as arguments
  • You need to generate out.txt (overwrite if it exists) in the specified format
  • You don't need to deal with the edges of the board (wraparound, infinite grids .etc)
  • You do need to have newlines in your output file.

获胜者将由字符数决定.

The winner will be determined by character count.

祝你好运!

推荐答案

perl,127个 129 135 个字符

设法剥离了另外两个字符...

perl, 127 129 135 chars

Managed to strip off a couple more characters...

$/=pop;@b=split'',<>;map{$n=-1;@b=map{++$n;/
/?$_:($t=grep/X/,@b[map{$n+$_,$n-$_}1,80..82])==3|$t+/X/==3?X:'.'}@b}1..$/;print@b

这篇关于Code Golf:康威的人生游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆