Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 31

Thread: Beginners Programming Challenge 16

  1. #11
    Join Date
    Sep 2009
    Location
    UK
    Beans
    535
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Beginners Programming Challenge 16

    This is a very interesting and fun challenge .

    @mo.reina: I like your idea of using the unicode boxes; they make it easier to read than the +s and -s that I was using.

    A note of warning for those taking part: don't modify your grid in place. You need to make a full clone of your data structure, applying the rules to the original but writing to the clone. Otherwise the patterns get messed up! (I made this mistake at first and it took me ages to work out what the problem was ).

    I look forward to seeing everyones' solutions and will post my (ruby) version after the challenge has been judged.
    DMedia - Distributed Media Library
    LaVida - A simulation game for Linux
    AskUbuntu

  2. #12
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Beginners Programming Challenge 16

    Some fun extensions to try:

    * Multiple species: anything but 0 is a species, amend the dead cell -> live cell rule to pick a majority if present and randomly otherwise.
    * Steady-state detection.
    * Cycle detection, a superproblem of the previous.


    Code:
    0010000010000001110000000000000022200002000000020
    has a sample 6x6 grid w/ 2 gliders; according to my sim (a very uninteresting and straightforwards Perl implementation -- one module which does the computation, and one driver script which handles reading the file and outputing results to STDOUT; never bothered to learn any graphic modules for Perl), glider #2 is gone 4 iterations after the start state, and 6 iterations after the start state a steady state is reached w/ 6 1's.

  3. #13

    Re: Beginners Programming Challenge 16

    Quote Originally Posted by schauerlich View Post
    I wrote a few here you can use to test.
    hey schauerlich, i've used the sequences you posted. i was wondering if you found them on an external site or did you just reverse engineer (count the cells manually) the patterns that you found on the 'net?

  4. #14
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge 16

    Quote Originally Posted by mo.reina View Post
    hey schauerlich, i've used the sequences you posted. i was wondering if you found them on an external site or did you just reverse engineer (count the cells manually) the patterns that you found on the 'net?
    I just used pictures from wikipedia and "drew" them myself in a text editor, and removed all the newlines.


    Code:
    1100
    1000
    0001
    0011
    =>
    1100100000010011
    Last edited by schauerlich; October 10th, 2010 at 09:08 AM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  5. #15
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Beginners Programming Challenge 16

    Well, there are quite a few patterns in Wikipedia...

    For amusement:
    Code:
    .........................................                                                                                                                                    
    ..........................O..............                                                                                                                                    
    ........................O.O..............                                                                                                                                    
    ..............OO......OO............OO...                                                                                                                                    
    .............O...O....OO............OO...                                                                                                                                    
    ..OO........O.....O...OO.................                                                                                                                                    
    ..OO........O...O.OO....O.O..............                                                                                                                                    
    ............O.....O.......O..............                                                                                                                                    
    .............O...O.......................                                                                                                                                    
    ...............OO........................                                                                                                                                    
    .........................................
    (sourced from http://en.wikipedia.org/wiki/File:Ga...glider_gun.svg)

    That's the Gosper glider gun, although I've taken the liberty of using periods instead of zeros and not cramming it into a single row so that it's actually easier to examine and check for errors. If you separate your computational and I/O layers, it's not terribly inconvenient to have I/O methods that e.g. accept an alternate character for 'dead' space or cheerfully accept one-row-per-line.


    Code:
    : # Find perl...
    eval 'exec perl5 -w -S $0 "$@"'
    if 0;
    
    
    my $line = undef;
    my $colcount = undef;
    my @lines = ();
    
    while (defined($line = <STDIN>)) {
            $line =~ s/\n$//go;
            ((length $line) > 0) || next;
            $line =~ tr/.O/01/;
    
            if (!defined($colcount)) {
                    $colcount = length $line;
            } else {
                    die unless ((length $line) == $colcount);
            }
    
            push @lines, $line;
    }
    print "$colcount\n";
    print join("", @lines);
    print "\n";
    
    exit 0;
    is Perl that accepts such a format (one row per line, . for empty, O for live) and outputs the number of columns followed by the 1/0 string. It won't enforce squareness, however.

  6. #16
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Beginners Programming Challenge 16

    Hey... this looks like fun
    What they mean by "beginner" can anyone take part?
    I mean I figure that not programming for 15 years must have set me back to beginner status right?

    p.s when is it due by ?

  7. #17
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Beginners Programming Challenge 16

    Quote Originally Posted by worksofcraft View Post
    Hey... this looks like fun
    What they mean by "beginner" can anyone take part?
    I mean I figure that not programming for 15 years must have set me back to beginner status right?

    p.s when is it due by ?
    These challenges run forever And basically anybody that considers himself to be a beginner in some respect can participate.

    I'm still struggling against this and I'm not sure if I'll ever be able to do it... having some little spare time in my hands

  8. #18
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners Programming Challenge 16

    Well, this may not be the cleanest or most efficient code i ever wrote. But its working and it's got a gui

    Code:
    #!/usr/bin/env python
    
    import sys
    import pygtk
    pygtk.require('2.0')
    import gtk
    import gobject
    
    class GOL:
        def __init__(self, infile, gridsize):
            self.gridsize = gridsize
            self.grid = [[0 for i in range(self.gridsize)] for i in range(self.gridsize)]
            start = open(infile, 'r')
            for y in range(self.gridsize):
                for x in range(self.gridsize):
                    s = start.read(1)
                    if not s:
                        break
                    self.grid[y][x] = int(s)
    
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            self.window.set_title('GOL')
            self.window.connect('destroy', lambda w : gtk.main_quit())
    
            self.da = gtk.DrawingArea()
            self.da.set_size_request(4 * self.gridsize, 4 * self.gridsize)
    
            self.window.add(self.da)
    
            self.window.show_all()
            self.da.connect("expose-event", self.draw_state)
    
            self.pixmap = gtk.gdk.Pixmap(None, 4 * self.gridsize, 4 * self.gridsize, 24)
    
            self.style = self.da.get_style()
            self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
            self.black = self.gc.get_colormap().alloc_color(0, 0, 0)
            self.white = self.gc.get_colormap().alloc_color(65535, 65535, 65535)
            self.gc.set_rgb_bg_color(self.black)
            self.gc.set_rgb_fg_color(self.white)
    
            self.draw_state()
    
            self.timeout_id = gobject.timeout_add(200, self.next_state)
    
    
        def draw_state(self, widget = None, data = None):
            self.gc.set_rgb_fg_color(self.black)
            self.pixmap.draw_rectangle(self.gc, True, 0, 0, 4 * self.gridsize, 4 * self.gridsize)
            self.gc.set_rgb_fg_color(self.white)
            for y in range(self.gridsize):
                for x in range(self.gridsize):
                    if self.grid[y][x]:
                        self.pixmap.draw_rectangle(self.gc, True, x * 4, y * 4, 4, 4)
            self.da.window.draw_drawable(self.gc, self.pixmap, 0, 0, 0, 0, 4 * self.gridsize, 4 * self.gridsize)
    
    
        def next_state(self):
            newgrid = [[0 for i in range(self.gridsize)] for i in range(self.gridsize)]
            max = self.gridsize - 1
            for y in range(self.gridsize):
                for x in range(self.gridsize):
                    around = 0
                    if x > 0    and self.grid[y][x - 1]: around += 1
                    if x < max  and self.grid[y][x + 1]: around += 1
                    if y > 0    and self.grid[y - 1][x]: around += 1
                    if y < max  and self.grid[y + 1][x]: around += 1
                    if x > 0    and y > 0   and self.grid[y - 1][x - 1]: around += 1
                    if x < max  and y > 0   and self.grid[y - 1][x + 1]: around += 1
                    if x > 0    and y < max and self.grid[y + 1][x - 1]: around += 1
                    if x < max  and y < max and self.grid[y + 1][x + 1]: around += 1
    
                    if around == 3:
                        newgrid[y][x] = 1
                    elif around == 2 and self.grid[y][x]:
                        newgrid[y][x] = 1
            self.grid = newgrid
            self.draw_state
            self.da.queue_draw()
            return True
    
    def main(argv):
        gol = GOL(argv[1], int(argv[2]))
        gtk.main()
    
    if __name__ == '__main__':
        main(sys.argv)

  9. #19
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Beginners Programming Challenge 16

    Quote Originally Posted by nvteighen View Post
    These challenges run forever And basically anybody that considers himself to be a beginner in some respect can participate.
    Kewl

    Well I always fancied having an excuse to write something using ncurses, so here is one I slapped together just now and I include initial setting "seed" file in the archive too. Anyone else is welcome to hack it for their own ends.

    p.s. OK so I got a moment to have another look and I see the rule is different for dead cells than living cells so I think I fixed now and also I made it so it should work with non rectangular seed files by specifying the number of rows and calculating number of columns... but that glider gun I copied from above still doesn't work
    Attached Files Attached Files
    Last edited by worksofcraft; October 11th, 2010 at 01:21 AM.

  10. #20
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Beginners Programming Challenge 16

    OK that glider gun has a buglet and I adapted my code to allow new lines (or anything other than a 1) for non living cells and so now it does work. I also added some extra lines so you can see the gliders as they go:
    Code:
    ........................................
    ..........................1.............
    ........................1.1.............
    ..............11......11............11..
    .............1...1....11............11..
    ..11........1.....1...11................
    ..11........1...1.11....1.1.............
    ............1.....1.......1.............
    .............1...1......................
    ..............11........................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    ........................................
    Do specify the height as 25 lines tho as it's not a square.

    btw the sample in post #5 given by schauerlich does work, it's the one in post 15 that has an error on line 10.
    Last edited by worksofcraft; October 11th, 2010 at 01:50 AM.

Page 2 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •