/wpc/ - Weekly Programming Challenge

Now in general format.

Previous Thread This week's challenge: Conway's Game of Life

Your goal is to create a Conway's Game of Life simulator using the language of your choice. The basic rules are as follows:

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

> Any live cell with fewer than two live neighbours dies, (underpopulation).
> Any live cell with two or three live neighbours lives on to the next generation.
> Any live cell with more than three live neighbours dies (overpopulation).
> Any dead cell with exactly three live neighbours becomes a live cell (reproduction).

The initial pattern is the seed. The rules are then applied to every single cell in the seed simultaneously. The rules continue to be applied to create further generations.

Post results / bits of code

Tips:
>If you find the task too simple, you're encouraged to try different variations and figure out ways to make the problem more challenging.
>These threads are still in their early stages, if you have any ideas for how these threads should work in the future or for new challenges, feel free to contribute in this thread

Other urls found in this thread:

en.wikipedia.org/wiki/Conway's_Game_of_Life
youtube.com/watch?v=R9Plq-D1gEk
pastebin.com/TXUuP2Yh
pastebin.com/bKGKWbUb
gist.github.com/iamevn/2d6380d8eb4bc47e270ea5678116d1bb
esolangs.org/wiki/Wordy
twitter.com/NSFWRedditImage

first one for THE COMFY Sup Forumseneral

hope this turns out as good as the last one

have fun lads

Maybe as an extra requirement for those that do not know how to make video output:
-Load a start image (.pbm)
-save the result after X iterations in another image

Its already hard enough to write a nice conways game of life that is efficient, if people try to work too much with input and video output this will take too long

So, if I get it right, it's about creating shapes and see how it evolves applying these rules (which creates the effect of movement)?
The white pixels/squares are dead? Sorry for brainletism.

Suggestions on how to get started if I completely failed at the last challenge?

(working on C# as it's the only language that I half understand)

you want to finish the first one, right?
tell me where you got stuck

Think first about the abstract things that you have to implement, I like to ask myself questions in this way:
>What do I have to do first? -> I have to create some spaces where I can save all the data
>What kind of data? Where do I get the data from? -> I create a random array of char's to start with, maybe I emplement loading an image later for conveniance
> How do I implement the rules? -> Well, my data has this format, which basically means this and this, so to follow the rules I have to change this from this to that.
> Where do I save the changes? -> In a seperat array
>etc...

Yep, check the wikipedia article:
en.wikipedia.org/wiki/Conway's_Game_of_Life

I suppose I can change that for the next thread but I left it open-ended so you'd have to figure out how to create a basic video simulation.

>you want to finish the first one, right?
Yeah although I was thinking about just giving it up and starting with this one

>tell me where you got stuck
I was trying to get the program to draw lines in a new bitmap with this code:
Bitmap Output = new Bitmap(srcPic.Width, srcPic.Height);
int iterations = new int();
int maxW = Output.Width;
int maxH = Output.Height;
for (iterations = 0; iterations < iterationsController.Value; iterations++)
{
Random rndA = new Random();
Random rndB = new Random();
int x0 = rndA.Next(maxW);
int y0 = rndA.Next(maxH);
int x1 = rndA.Next(maxW);
int y1 = rndA.Next(maxH);
int clr = rndB.Next(srcColors.Count);
Color rndColor = srcColors[clr];
using (Output)
{
Pen myPen;
myPen = new Pen(rndColor);
Graphics formGraphics = CreateGraphics();
formGraphics.DrawLine(myPen, x0, y0, x1, y1);
myPen.Dispose();
formGraphics.Dispose();
}
}
The problem is that I can't get it to display on a picturebox or to save to a file, I tried with pictureBox1.Image = Output (pictureBox1 is a component I already added and successfully used to display the original image), it resulted in an error saying that the parameter was not valid. I also thought about using Output.Save but I just don't know how to do it.

I already do that but I just don't know enough code to translate those concepts into a working program.

youtube.com/watch?v=R9Plq-D1gEk

>I already do that but I just don't know enough code to translate those concepts into a working program.
Oh, Okay. Basically you just have to learn how to explain to the machine what it has to do. You do that by using the keywords provided by you through the language. But the only way to learn that is through lots and lots of training. But while training you also have to learn more and more of the language so that you know about all the vocabulary that exists.

hello

are you japanese or a turboweeb?
nice work

gj weebshit

just regular weeb

can you share your code

cubes that aren't moving are dead right?

I'm not C# programmer but...
You are not even writing anything to Output, you are creating graphics and disposing them. You need to write to the Output.

Also create only one myPen, rndA, formGraphics and use them all the time (you don't need rndB). It's more efficient than creating these objects over and over again for each iteration and initializing PRNG for each iteration is a sin.

read examples from Stack Overflow.

Welp.
const a = Array(100).fill(Array(100).fill(0))
a[25][50] = 1 //seed
a[25][51] = 1
process.stdin.setRawMode(true)
process.stdin.setEncoding('utf8')
process.stdin.on('data', c => (c == 'q'?process.exit:next)(0))
function next() {
a.map((a1, i1) => a1.map((e, i, a2) => {
let neighbours = [i - 1, i + 1, (i1 - 1)[i], (i1 - 1)[i - 1], (i1 - 1)[i + 1], (i1 + 1)[i], (i1 + 1)[i - 1], (i1 + 1)[i + 1]].filter(_ => _).length
return (neighbours < 2 || neighbours > 3)?0:1
}))
console.log(a.join('\n'))
}

>Error: Comment too long
pastebin.com/TXUuP2Yh

pastebin.com/bKGKWbUb
I suck at C. you might need to trplace the arc4random_uniform(2) to something on your system

A bit confusing but white cells are dead and black cells are alive.

A 2x2 cube doesn't move in the OP gif because it's self sustaining. Each pixel has 2 neighbors so the cube doesn't move.

I figured it out now
Needed some time

BTW any suggestion on best way to visualize this with python

Here's a game of life I made with ncurses and c++ 8 months ago when I was learning

fuck, I see, thanks for the tip

>the only way to learn that is through lots and lots of training
Well, figured as much

nice square dots

And here is another one I made when learning SDL2

>Well, figured as much
took my 3 years to be ok at c++ (tho I'm studying something more or less unrelated at the same time), its only after you've gone far that you appreciate all you've learned.

In Python, instead of using list generators based on the range function, it's a lot faster to create a large list in-place.
Probably won't make a difference if you only generate it once, but in a long loop it could make a huge improvement.

cool, thanks

3.841.992 Iterations
75.000 lines drawn
133 seconds

It takes way longer for my program (uses Manhattan distance) to add another line to the image compared to other source images.

My instinct is to render out individual frames first with pillow's get/put pixel functions to get the rules down, and then try to implement a GUI with pygame or pyglet. Not sure if this is the best way to do it, but it's how I'm approaching it.

For next week or the week after that, we could do creating tree maps.

Was thinking maze solving and maze generation might be fun as well.

Either way, I think a github repo would be a good way for people to contribute ideas

Nice

finally finished my implementation.

Getting image comparison right was the hardest part

This is 20000 iterations in ~29s

I have a toy programming language that's pretty much a joke but was designed to be turing complete and stable. Bottles of beer took me 2203 loc in it, should I give this a go?

Now playing around with various options..

This is drawing using crosses instead of plain lines. Again 20000 iterations but ~38s. Im using openmp to parallelize my image-comparison loop

>>These threads are still in their early stages, if you have any ideas for how these threads should work in the future or for new challenges, feel free to contribute in this thread

Perhaps have a pastebin or git of all previous challenges.
Also perhaps have an easy/hard mode with example code for easy and benchmark performance challenges(e.g. 100 generations per second) for hard.

do it user

fugg. I don't have arrays or anything and I can output a character at a time. I just have an implementation-defined sized vector/list/array/set/whatever of variables to work with. iirc my implementation is "infinite" so this could work

def friends(board, x, y):
cnt = 0
if board[x-1][y] == 1:
cnt += 1
if board[x+1][y] == 1:
cnt += 1
if board[x][y-1] == 1:
cnt += 1
if board[x][y+1] == 1:
cnt += 1
if board[x-1][y-1] == 1:
cnt += 1
if board[x-1][y+1] == 1:
cnt += 1
if board[x+1][y+1] == 1:
cnt += 1
if board[x+1][y-1]:
cnt += 1
return cnt

I don't like this. Why not something like
def friends(board, x, y):
return sum([sum(board[j][y-1:y+2]) for j in range(x-1,x+2)]) - board[x][y]

I'm sure it can be made better.

Is it better to generally use True or False values or 1s and 0s for cell states? Or does it not really matter?

either works but there's some neat things you can do with 1/0

Using 1/0 lets you do arithmetic on them like but then it's probably not worth it if in your language 1/0 != true/false.

Depends on the language. In C for example a boolean is just a byte which is either 1 or 0 (true or false) but in some other languages, a boolean still is a byte in size but it can combine several booleans into a byte with a bit for each one. So booleans are typically more memory efficient but slightly slower, so I would say use booleans when you are using a huge amount of them which is the case in game of life.

it grows faster to the right for some reason

optimized some loops

There's obviously a lot I could do to improve this, but I think I'm satisfied moving on to game of life now.

when you do a step, are you adjusting things in place and letting the next step's values bleed into current step's calculations?

cool effect tho

what?
i'm just doing it according to OP's rules

uint8_t &cell = world.get(x, y);

if (cell == ALIVE)
{
neighbors--;
// 1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
// 3. Any live cell with more than three live neighbours dies, as if by overpopulation.
if (neighbors < 2 || neighbors > 3)
cell = DEAD;
// 2. Any live cell with two or three live neighbours lives on to the next generation.
}
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
else if (neighbors == 3)
cell = ALIVE;


After this I have another loop where I go over every cell and "make" a SDL_Pointer for each alive cell

I love it 6686,30s in Python 10M iterations

The last challenge was /comfy/

I have my results set as my wallpaper.

>cell = DEAD;
>cell = ALIVE;

yeah this is the problem, you should be doing something like

>cellCopy = ALIVE;

If you change them in place then the change affects the results of it's neighbors that are tested later. That's why it moves faster to the right and why it grows wildly instead of behaving like CGoL usually does.

what primitves are you using?

cool, thanks

i like this thread

How do I spice things up? Was kinda easy compared to the previous one.
Was thinking about overlaying it on an image and reveal pixels where the "alive" bits pass or something similar.

almost done with wimpmode version I think. Just need to write the output code. I didn't do the obvious optimizations because this is going to be slow no matter what and making a Turing Tarpit less readable means frustration if I make a mistake and need to debug. It's surprisingly terse.

Worked really hard, but thanks to my friend Karlie, I've come up with this pro hacker version:

//welcome screen for conways game of live
//written in gnu coding style cause its so elegant ^_^

class ConwaysGameOfLife
{
/**
* displays a nice welcome screen for the game of lifee
*
* @param args the list of strings that encode arguments that you are passed to the app
* @return nothing lol
*/
public static void
main (String[] args)
{
System.out.println ( "hello welcome to game of throne?" ) ;
}
};

You could add races

What kinds of data structures did you lads use

Good general

A matrix, that's it.

Conway in 3d. Python and Blender. Blender is really bad for voxels unless you want to do meshes by yourself. Never again

Rules: alive only if less than 8 neighbors and more than 1 neighbor alive.

gist.github.com/iamevn/2d6380d8eb4bc47e270ea5678116d1bb
The language is esolangs.org/wiki/Wordy

Please excuse the shitty quality of the webm

I just now realize that my language doesn't have any sort of SLEEP instruction. I've got two choices
>busy wait with tens of thousands of NOPs to slow it down to be watchable
>write out frames to a file that could be played back smoothly afterward
>wait on user input each frame
going with the latter since that's cleaner

Before trying to convert this to an actual Wordy program (each symbol would be converted to a sentence of text), I'd need to convert all the literal numbers in the program into things that can actually occur in sentences and I'd like to do some optimizations, explore adding a SLEEP instruction, build a better database of prose to generate from, write a better UI for sentence generator, and try out writing a markov text generator that is able to generate aiming for a certain word length ratio.

>How do I spice things up? Was kinda easy compared to the previous one.

Write a GUI for it with different features or try out different variations of the game.

neat. how long did that take?

Hello I am very new to c++ while I want to figure out and develop the logic for this program on my own I am wondering how I render the output with time.

numpy my ass. I have made a version numpy version of my script at and now it runs almost 3 times as slow.

What specific picture?

Numpy Arrays

Anything that is not 0 is defined as true in C - not just 1.

dumb question before i attempt this, should i be doing it as a console application?

You may.

Sure, you want to focus on the logic - skip the fancy graphics. You can add it in at a later time if you want.

What was the name of last challenge?

...

Absolutely love the last challenge

Is it ok if I make a finite version? I don't want to fuck around too much.

if you are going to make a finite version just change the for loop to a while(true).

How would that help? I'm talking about finite sized board, and not finite steps. Anyway, I'm using Erlang, so there aren't gonna be any for/while loops to begin with.

Then that question makes no sense, because if it was not a finite size board you would run out of memory before you even started the loop.

There are ways of handling a pseudo-infinite board, like you only store the the cells that are alive, and calculate the next set of alive cells based on that. It could lead to ever-increasing memory usage, but it wouldn't need an infinite amount of memory from the start. I assumed OP wanted us to be clever like that.

You could perfectly reasonably have an infinite grid but only start with a small region of it being non-empty and then grow it as your gliders piss off to infinity.

Make running out of memory a problem for future you.

I think it's just that the description of the system doesn't really bother with bounds.

Each bitplane is its own game of life.

this is not even a computational heavy. Only hardship is the graphics

You could make the initial conditions an image.

It already works like that. Not quite as interesting as i thought it would be, though.

make it so new generations inherit a mixture of their parents colors.

Now, that i think about it the lowest bits are pretty much invisible, so it doesn't make much sense to do it this way. I'll try to find some rules using generations that could be pretty.

>neat. how long did that take?
Too long, although the logic and rendering is light. Blender gets heavier after each iteration, because the blender's scripting api can't handle multiple cubes (probably due to overhead generated by the script). I was able to make this on one run but if I wanted to do more iterations I would need to restart blender occasionally and join the meshes separately.

Made with pygame with pause and randomize controls

...

GoL is fairly chaotic, what did you expect?

thats a fun challenge. I once did an opengl one with infinite area and panning / zooming / editing and all that shit.
dont really feel like doing it again tho

>input and video output this will take too long
why need image processing at all?

plaintext ascii is literally all you will ever need for this

>obligatory Java pajeet-tier cancer incoming.
>now with more cancer with JavaFX

Try the last one from the previous threads if you haven't. Was pretty fun, and the results were really cool.

>Its already hard enough to write a nice conways game of life that is efficient, if people try to work too much with input and video output this will take too long
Too much time? using C and SDL2 it's like 10 function calls or so to have a simple window drawing the output. Which is a hell of a lot easier then dealing with file io.