[Challenge] Snakes and ladder style board

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Challenge] Snakes and ladder style board

Post by requinix »

For the fun of it, here's the benchmarks I have. Times below are averages over 20K iterations including peak memory usage for all iterations[1].

Code: Select all

astions             : 0.00032996s  7027 KB
astions2            : 0.00011592s  7027 KB
mikemike            : 0.00006905s  6047 KB
tasairis            : 0.00006718s  6048 KB
tasairis_loopless1  : 0.00008440s  6038 KB[2]
tasairis_loopless2  : 0.00056466s  6074 KB[2]
johncartwright      : 0.00010650s  6222 KB[3]
pytrin_loopless     : 0.00009284s  7019 KB
The numbers are a bit different because I rearranged the benchmarking code. Should only measure a function call and the actual work now.

[1] Memory usage is proportional to the number of iterations so it seems there are memory leaks somewhere. Use this figure for comparisons between methods only.
[2] Includes simple modifications so that create_function is not called multiple times.
[3] Doesn't do the back-and-forth thing.
Last edited by requinix on Sat Aug 29, 2009 2:34 am, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] Snakes and ladder style board

Post by Eran »

what about john's solution? how does it perform?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Challenge] Snakes and ladder style board

Post by requinix »

pytrin wrote:what about john's solution? how does it perform?
I thought it missed the snakes-and-ladders-style requirement...

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));

Code: Select all

100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
Regardless,

Code: Select all

johncartwright      : 0.00010650s  6222 KB
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: [Challenge] Snakes and ladder style board

Post by mikemike »

Good job guys :)

I'll come up with a new (harder) one soon.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

LOL That's great!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

John Cartwright wrote:

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", $e);'), array_chunk(array_reverse(range(1, 100)), 10)));
It doesn't meet the challenge criteria though :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: [Challenge] Snakes and ladder style board

Post by John Cartwright »

Thats what I get for thinking I do not need to test :)

Here is my updated version, and the more I think about it, you've actually got it wrong. The Starting point is actually on the left.

If you want the "correct version",

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, 1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: [Challenge] Snakes and ladder style board

Post by McInfo »

Yet another solution:

Code: Select all

<?php
header('Content-Type: text/plain');
for ($i = 100; $i > 0; $i -= 20) {
    echo implode(' ', range($i, $i - 9)), PHP_EOL
       , implode(' ', range($i - 19, $i - 10)), PHP_EOL;
}
?>
And another:

Code: Select all

for ($i = 100; $i > 0; $i -= 20) {
    for ($j = $i; $j > $i - 10; $j--) {
        echo $j, ' ';
    }
    echo PHP_EOL;
    for ($k = $i - 19; $k < $i - 9; $k++) {
        echo $k, ' ';
    }
    echo PHP_EOL;
}
I'm not sure how we're calculating "7 lines".

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 7:41 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [Challenge] Snakes and ladder style board

Post by requinix »

John Cartwright wrote:

Code: Select all

echo implode("\r\n", array_map(create_function('$e', 'return implode(" ", substr($e[0], 0, -1) % 2 ? array_reverse($e) : $e); '), array_chunk(array_reverse(range(1, 100)), 10)));
So close... It's that 91-100 line:

Code: Select all

91 92 93 94 95 96 97 98 99 100
81 82 83 84 85 86 87 88 89 90
If you change the substr to use 0,-1 instead of 0,1 it's perfect.

Code: Select all

with loops
astions1  : 0.00032996s  7027 KB
astions2  : 0.00011592s  7027 KB
mikemike  : 0.00006905s  6047 KB
tasairis  : 0.00006718s  6048 KB
mcinfo1   : 0.00006479s  6247 KB
mcinfo2   : 0.00005899s  6450 KB
 
without loops
tasairis1       : 0.00008440s  6038 KB[2]
tasairis2       : 0.00056466s  6074 KB[2]
pytrin          : 0.00009284s  7019 KB
johncartwright  : 0.00014869s  6230 KB[2]
[2] Includes simple modifications so that create_function is not called multiple times.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Snakes and ladder style board

Post by VladSun »

A recursive, loopless one :)

Code: Select all

function gen($start, $end, $reverse = 0)
{
    if ($start > 0)
        return (
            implode(" ",
                range(
                    $start - $reverse*20 + $reverse,
                    $end - !$reverse*20 + 20 + !$reverse
                )
            ).
            "\r\n".
            gen($start - $reverse*20, $end - !$reverse*20, !$reverse)
        );
}
echo gen(100, 90);
=>

Code: Select all

function gen($start, $end, $reverse = 0)
{
    if ($start > 0)
        return (implode(" ", range($start - $reverse*20 + $reverse, $end - !$reverse*20 + 20 + !$reverse))."\r\n".gen($start - $reverse*20, $end - !$reverse*20, !$reverse));
}
echo gen(100, 90);
PS: Sorry for the delay, I was on vacation :)
Last edited by VladSun on Tue Sep 01, 2009 2:50 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

Ah decomposition in action :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Snakes and ladder style board

Post by VladSun »

mikemike wrote:Print list of numbers from 1-100. These numbers should be in rows of 10 and in a zig-zag fashion.
First I thought you meant the Z-scan used in JPEG format - http://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg :)
Maybe it could be a good challenge too :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [Challenge] Snakes and ladder style board

Post by onion2k »

astions wrote:I was trying to be cool by decreasing the number of lines which compromised performance.
I think that's a valid goal, but I also think solutions for optimised performance, and for optimised readability are valid too. These Challenges should be about thinking up new and interesting approaches to problems, regardless of how they're interesting.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [Challenge] Snakes and ladder style board

Post by onion2k »

Just for completeness, here's my solution. It should be pretty quick considering it's not doing very much.

Code: Select all

<style>
#board {
    float: left;
    width: 600px;
    height: 600px;
    border: 10px solid #888;
}
.square {
    width: 60px;
    height: 60px;
    color: #888;
}
.l {
    float: left;
}
.r {
    float: right;
}
</style>
 
<div id="board">
 
<?php
 
    for ($x=100;$x>0;$x--) {
 
        if ($x%10==0) {
            $dir = ($c++%2==0) ? "l" : "r";
        }
        
        echo "<div class=\"square ".$dir."\">".$x."</div>";
        
    }
    
?>
 
</div>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Challenge] Snakes and ladder style board

Post by Benjamin »

Very nice onion2k, I didn't think to create it using css.

Might want to put an @ sign in front of the $c ;)
Post Reply