r/dailyprogrammer • u/XenophonOfAthens 2 1 • Sep 23 '15
[2015-09-23] Challenge #233 [Intermediate] Game of Text Life
Description
John Conway's Game of Life is a classic game among computer programmers and mathematicians.
The basic idea is this: the game takes place on an infinite 2D grid of cells. Cells can be either "alive" or "dead". The game evolves in generations, where old cells die out or are born again according to very simple rules. The game can inhibit remarkably complex patterns despite the simplicity of the rules, which is why it's called "Game of Life". It's as if the little cells become living organisms.
The rules are as follows:
- Any cell that is alive and zero or just one living neighbor is dead in the next generation
- Any cell that is alive and has two or three living neighbors lives happily on to the next generation
- Any cell that is alive and has four or more neighbors get "smothered" and is dead in the next generation
- Any cell that is dead and has exactly three neighbors is "born", and is thus alive in the next generation.
To be clear, a "neighbor" is defined as a cell that's right next another cell, either horizontally, vertically or diagonally.
If something is unclear or you need more information, I highly recommend reading Game of Life's Wikipedia entry for a more thorough description of the rules.
Usually, "alive" cells are filled in and black and "dead" cells are just white. In ASCII you could represent alive cells with asterisks and dead cells with spaces. So, if one generation of the Game of Life looks like this
**
**
*
Then the next generation will look like this:
***
*
**
Poor little middle dude has died, but a bunch of new ones have been born.
There is, however, no law that says that the alive cells have to be represented by asterisks. It could be any text, really. So then we could have this first generation:
He
ll
o
Which leads to this generation
lHe
l
lo
Notice that the character that represents newly born cells are selected randomly from the three neighbors that spawned it. Your challenge today is to implement this variation of the Game of Life.
Formal inputs & outputs
Since there's a random component to this challenge (i.e. which character a newly born cell will be, your solutions obviously don't have to match these character for character)
Inputs
You will receive a number of lines which you will play the Game of Life on.
Outputs
You will output the next generation of the "textual" Game of Life according to the rules laid up above.
There is one more rule that deserves mention: in normal Game of Life, you play on an infinite grid. Here, the size of the grid is the smallest rectangle that can fit the input. No cells can be born outside of it.
Sample inputs
Input 1
He
ll
o
Output 1
lHe
l
lo
Input 2
What?
This is exceedingly silly.
Really, we would like some ACTUAL programming challenges around here.
Output 2
W ?? exceeding sill
T i xceedingl illy
. ACTU programmi challeng arou her
eally oul ik om CTUA rogrammin hallenge roun ere
Challenge inputs
The challenge outputs will perhaps be a bit long, so consider using a service like hastebin or gist to share your results instead of pasting them into your comments.
Challenge 1
The full text of this post (either the markdown source, or just copy the text itself)
Challenge 2
The source code for your own program. If you use tabs instead of spaces to indent your code, you can treat those like a single space, or you can treat them like four or eight spaces, whichever it is you use when you write your code.
Bonus
Apply your program over and over again to your source code, so that you get an animated game of life (maybe make a plugin that does this for your favorite editor?) and upload a video of it. It can be an animated gif, a webm, a giphy, a youtube, or whatever it is the kids are using nowadays (if you want to make a Vine of your code being destroyed by the Game of Life, don't let me stop you).
Notes
As always, if you have a challenge suggestion, head on over to /r/dailyprogrammer_ideas and suggest it.
By the way, I've gotten several comments saying that the easy problem from this week was a bit too difficult. Mea culpa, sometimes you misjudge the difficulty of a problem when you design it. I do really appreciate it when readers point out whether they think a problem is too difficult or too easy for the difficulty level, as long as you do so in a pleasant manner. Constructive feedback is always welcome. :)
5
u/adrian17 1 4 Sep 23 '15
The sample outputs look wrong:
Here the middle right cell is dead and has 4 neighbors, so it should stay dead.
Here the second letter "e" has 2 neighbors ("r" and "."), so it should stay alive.
23
u/lukz 2 0 Sep 23 '15
Intermediate challenge solved in assembly. Here it comes.
Program starts at address 1200h and is 121 bytes long. Program is for Sharp MZ-800.
Clear the screen, type your input on the screen and leave 1 character border empty. Run the program using command G1200. And watch what happens :-).
Screenshot, input is on top, output on bottom.
13
u/mn-haskell-guy 1 0 Sep 23 '15 edited Sep 23 '15
Basic Javascript - you can interact with it here: (link)
One that respects the bounds of the initial text: (link)
8
u/adrian17 1 4 Sep 23 '15 edited Sep 23 '15
Python. Went a bit far with list comprehensions.
Used on itself: http://hastebin.com/ezosucavov.py
5
u/casualfrog Sep 23 '15 edited Sep 24 '15
JavaScript (feedback welcome)
Edit: see it in action!
4
u/Godspiral 3 3 Sep 23 '15
This would be much more fun than I am allowed to have today.
in J,
to kind of match spec, (prints random letter rather than random neightbour)
but a more interesting version is one that grows the space/payfield around the letters. I will then shrink down the space to get orginal window size. Adverb that takes iteration count as left param:
not sure if the extra padding makes it any cooler, but remove it with deleting &.(pad:5)
1
u/eatsfrombags Sep 23 '15 edited Sep 23 '15
Is there an error in the example input/output?
I assume we're counting diagonals as neighbors? The dead "top left" corner of the input becomes an "l" because it has exactly three neighbors, to the right, below, and below-right. The dead "middle right" input becomes an "o", but if we're counting diagonals then this has 4 neighbors, doesn't it?
1
u/robi2106 Sep 23 '15
I'm confused on the input. so the two lines of text are actually the positions of alive cells at the start of the simulation, correct? just using any character instead of a 1 to mark an alive cell? and spaces are dead cells?
3
u/eatsfrombags Sep 23 '15 edited Sep 24 '15
Python 3, nothing clever. Feedback appreciated!
Here's an [animated gif] (http://i.imgur.com/gvcdfnK.gif) of the code applied to itself.
EDIT: Cleaned up and generally improved.
3
u/a_Happy_Tiny_Bunny Sep 23 '15 edited Sep 24 '15
Haskell
There are ways to simplifygameOfLifeso thatn1throughn8aren't got by hand. I thought of one already, so I'll probably update this answer when I get to implement my approach.The old version is here. It might be easier to understand
gameOfLifewith the previous version.EDIT: This
mainfunctionshould do the bonus in Windows, Linux, and OS X. It requires
import System.Console.ANSIandimport Control.Concurrent. I am using Windows, and I'm not sure how to go about making the gif or video.EDIT2: Quick-fixed issue pointed out by /u/mn-haskell-guy
2
u/[deleted] Sep 24 '15 edited Sep 24 '15
Common Lisp
Edit: updated to run the bonus animation in a terminal. Save as 'solution.lisp' and run
sbcl --load solution.lisp(depends on quicklisp)2
u/banProsper Sep 24 '15
C#
It appends whitespace but doesn't return 'correct' character where current one is blank (it doesn't check clockwise from top like in the OP).
2
u/smnslwl Sep 24 '15
C++14 code here.
Will run on its own source code for a 100 generations by default, although the filename and num. of generations can be specified. Uses ANSI escape code
"\033[2J\033[1;1H"to clear the screen and position the cursor to the left, displays the board and waits for user to hitEnterbefore showing the next generation.1
u/SportingSnow21 Sep 23 '15
Go
Recursive implementation Github:
1
u/vesche Sep 23 '15 edited Sep 23 '15
Python. That grid variable deceleration is juicy (and also PEP8).
1
u/cluk Sep 23 '15
C. Rather long. I would appreciate any feedback.
https://github.com/lroza/r-dailyprogrammer/blob/master/2015-09-23/c/challenge.c
Animation: http://gfycat.com/SevereThirdCrustacean
1
u/glenbolake 2 0 Sep 23 '15
Python 3, set to run on itself for 100 generations.
Output (and in gif form)
1
u/NoAnalHere Sep 24 '15
I attempted this not realizing it said intermediate. Was not able to complete. I need to brush up to get on this skill level
1
u/grangelov Sep 24 '15
My perl solution
1
u/[deleted] Sep 24 '15
bonus challenge:
1
u/[deleted] Sep 24 '15 edited Sep 24 '15
Java 7. Kinda lengthy without list comprehensions...
1
u/fjom Sep 24 '15
This is the 50-th iteration of my program running on its own source, rebuilding the original C# file is left as an exercise for the reader
1
u/robi2106 Sep 24 '15
this challenge has the same title as the previous challenge.....
1
u/Eggbert345 Sep 24 '15
Golang again. Go doesn't have any simple way of writing text to an image, so no cool gifs of vines.
1
u/ChazR Sep 25 '15
I went a bit off the reservation with this one. Cellular automata are cool, so I built a little utility to execute any cellular automaton.
Haskell code on GitHub.
1
u/FIuffyRabbit Sep 25 '15
Golang
webm link. The blinking is probably due to frame rate and the flush rate of console.
1
u/FlammableMarshmallow Sep 25 '15
Python3 solution, woo!
1
u/SquirrelOfDooom Sep 26 '15
Python 3.
GitHub-Links:
Source code:
1
u/FrankRuben27 0 1 Sep 26 '15 edited Sep 27 '15
Emacs Lisp - using strings and current elisp would have been too similar too CL, so I've been using buffer functions instead. Not exactly straightforward, quite slow (see screencast), but fun (and yes - color inheritance from neighbours to new cell would be even nicer, but time is sparse):
Emacs Lisp (elisp) - EDIT: cosmetic changes
1
u/[deleted] Sep 26 '15 edited Sep 26 '15
Fortran.
I was trying to do a 'matrix at once' solution based on 'where' - but I couldn't figure out how to choose a random neighbor using that approach. So I gave up and iterated over the matrix.
This solution wraps around at the edges, pac-man style. I chose a very small fixed size for the board, instead of detecting the input size. It doesn't have any cool way of displaying the output, you basically tell it how many steps to iterate forward, and it shows the output after that many steps.
output:
source:
1
u/yoavst Sep 26 '15
My first time ever answer, written in Kotlin.
1
u/NiceGuy_Ty Sep 26 '15
Racket
1
u/The_Chimp Sep 27 '15 edited Oct 04 '15
Python 3
[source code] - GitHub
Outputs:
This is my second python program so any feedback is appreciated!
1
u/ettupaca Sep 28 '15
I did a modified version of this in Golang to play around with subroutines and mutexs. This version generates a random 25/55 board and then iterates once a second for 60 seconds while simultaneously rendering the board at roughly 29 frames per second.
I'm sure there's a better option for clearing the screen than shelling out but that wasn't what I was really focused on when I wrote this, I'd be curious on other opinions (like a good way to avoid having 3 boards in memory at once).
1
u/Minolwa Oct 05 '15
I'm a little late to the party, but here's a solution in Java 8.
The brutest of force: it's very inefficient and very slow, but it works. The animation is set to play directly into the console. If I try to load too much into the input text file, the console takes a too long to scroll for good animation quality.
1
u/jeaton Oct 07 '15
python:
1
u/Shenkie18 Oct 25 '15
This was a cool challenge. My programs asks for the time in msec when a new generation will appear.
0
u/[deleted] Sep 23 '15
[deleted] — view deleted comment