Alternatively, join us on Discord.

Advent of Code

Started by Ivoah, December 03, 2015, 03:03:24 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Adriweb

December 06, 2015, 05:17:48 am #15 Last Edit: December 06, 2015, 05:19:48 am by Adriweb
Yay 27th today. Learned my lesson to properly read the rules fully, got both parts right on first try.
I wished I remembered how to properly init big PHP arrays with values though ; I had to Google that >.>

Behold the unoptimized code...
Spoiler: ShowHide
ini_set('memory_limit','1G');

function day_6_1()
{
    $total = 0;
    $map = array();
    $b = array();
    $b = array_pad($b,1000,0);
    $map = array_pad($map,1000,$b);

    foreach(file('day_6.txt') as $line)
    {
        $line = trim($line);
        preg_match('/(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/', $line, $matches);
        $row1 = (int)$matches[2];
        $col1 = (int)$matches[3];
        $row2 = (int)$matches[4];
        $col2 = (int)$matches[5];
        switch ($matches[1])
        {
            case 'toggle':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                        if ($map[$tmpr][$tmpc] === true)
                        {
                            $map[$tmpr][$tmpc] = false;
                        } else {
                            $map[$tmpr][$tmpc] = true;
                        }
                    }
                }
                break;


            case 'turn on':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                        $map[$tmpr][$tmpc] = true;
                    }
                }
                break;

            case 'turn off':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                        $map[$tmpr][$tmpc] = false;
                    }
                }
                break;

            default:
                echo "asd";
                break;
        }

    }
    for ($j=0; $j<=999; $j++)
    {
        for ($i=0; $i<=999; $i++)
        {
            if ($map[$j][$i] === true)
                $total++;
        }
    }
    return $total;
}


function day_6_2()
{
    $total = 0;
    $map = array();
    $b = array();
    $b = array_pad($b,1000,0);
    $map = array_pad($map,1000,$b);

    foreach(file('day_6.txt') as $line)
    {
        $line = trim($line);
        preg_match('/(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/', $line, $matches);
        $row1 = (int)$matches[2];
        $col1 = (int)$matches[3];
        $row2 = (int)$matches[4];
        $col2 = (int)$matches[5];
        switch ($matches[1])
        {
            case 'toggle':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                            $map[$tmpr][$tmpc] += 2;
                    }
                }
                break;


            case 'turn on':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                        $map[$tmpr][$tmpc] += 1;
                    }
                }
                break;

            case 'turn off':
                for($tmpr = $row1; $tmpr <= $row2; $tmpr++)
                {
                    for($tmpc = $col1; $tmpc <= $col2; $tmpc++)
                    {
                        if ($map[$tmpr][$tmpc] > 0)
                            $map[$tmpr][$tmpc] -= 1;
                    }
                }
                break;

            default:
                echo "asd";
                break;
        }

    }
    for ($j=0; $j<=999; $j++)
    {
        for ($i=0; $i<=999; $i++)
        {
                $total += $map[$j][$i];
        }
    }
    return $total;
}
Co-founder & co-administrator of TI-Planet and Inspired-Lua

Juju

Damn you, I only ranked 43th.
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

DJ Omnimaga

What would be considered a good rank at this point?

Adriweb

I guess making it in the leaderboard is somewhat some proof of being proficient enough in some language and algorithmic skills, but within the leaderboard itself, it depends, and the time indicator can let you see how well you performed compared to others (which the rank only may not, if times are very close)
Co-founder & co-administrator of TI-Planet and Inspired-Lua

Ivoah

bump. I updated the OP with more solutions.

DJ Omnimaga

Cool, hopefully they help :)

Juju

There's a new private leaderboard feature, use code 18310-5850abfa to join mine ^_^

http://adventofcode.com/leaderboard/private/view/18310
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

Juju

Welp, it took 4 hours to fill the leaderboard up today... and I'm 42th o.o
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

alexgt

Nice! Better than what I could do :P

Unicorn

Maybe I'll just try to do #24 :P



??? ??? ??? ??? ???

alexgt


DJ Omnimaga

Have the ones here who participated so far gotten any luck with newer challenges? I didn't get much time to check what they were since I was so busy in the last 8 days.

Juju

The last few ones are haaaard. Well, let's say it's not for the faint of heart. The easiest ones takes less than 30 minutes to fill the leaderboard up, but in a few cases, like those ones who requires a lot of computation and if you're doing it wrong you'll take days to compute the answer, it may take 3-4 hours.
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

alexgt

O.O that is crazy O.O
For that stuff I would die since I am not that good at that type of coding

Also what place are you @Juju ?

Juju

97th on the leaderboard and ready for the last one.
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

Powered by EzPortal