[Community Puzzle] IPv6 Shortener

https://www.codingame.com/training/easy/ipv6-shortener

Send your feedback or ask for help here!

Created by @Toby75,validated by @Wontonimo,@codybumba and @AndreMarasca.
If you have any issues, feel free to ping them.

It’s fun. Now reversed task :slight_smile:

1 Like

Perhaps a problem with the “Multiple zero” test case

In fact, with the IDE test you can have all tests OK if you remove only the sequence of “0000” with length>1

It is only the validator that tests the case of two sequence of “0000” with length>1

No tested case : two sequence of “0000” with length=2

3 Likes

While reviewing solutions I found cases where the solution doesn’t always work, my own included at the time. Most of the time it was about leading or ending 0000 block, single or multiple.

When testing all solutions with the two ip bellow and corresponding result, all fails except for Coding_4_Fun, JeZzElLutin, geoffroy.
‘0000:0000:0100:0000:0000:0004:aaaa:0000’ > ‘::100:0:0:4:aaaa:0’
‘0000:aaaa:a000:0000:000a:0030:0000:0000’ > ‘0:aaaa:a000:0:a:30::’

I dont know if I missing something here, like not having the good corresponding compressed ip, or not properly testing solutions.
Hope this will help.

1 Like

Added new test cases and validators in view of the comments from @Zorg1 and @Luciole above. Thanks both for pointing out the issue.

2 Likes

I pass all the tests but fail the validator 8… I can’t figure out what might be so special about validator 8… I tested my code with many combinations of ips and everything seems to be in order… Any ideas where could be the trick ?
Thanks !

Try this:

0000:0000:a000:0000:000a:0030:0000:0000
2 Likes

Thanks ! I can see where my mistake is now… :sweat_smile:

I am really stuck on this problem. I fail tests 5, 7, and 8 and would really appreciate if anyone could take a look.

Do you mean the test cases or the validators? Also, have you tried the cases mentioned in the discussion above?

Yeah, I meant the test cases. I fixed case 5, but can’t figure out 7 and 8.

The titles of those test cases tell you what to watch out for.

I understand that, I guess what I am asking is for someone to look at my code and provide any tips.
#include

#include

#include

#include

#include

using namespace std;

/**

Step 1) Replace every zero block with a :0

Step 2) Delete all leading zeros

Step 3) look for multiple :0:0 and replace with a ::

**/

int main()

{

string ip;

getline(cin, ip);

std::regex leadingZeroRegex(":0+([a-zA-z1-9])");

ip = std::regex_replace(ip, leadingZeroRegex, ":$1");

// std::regex zeroBlock("(0000)");

// ip = std::regex_replace(ip, zeroBlock, "0");

//Zero Block Vars

int doubleColonLength{};

int doubleColonStart {-1};

int bestDoubleColonLength{};

int bestDoubleColonStart{-1};

int bestDoubleColonEnd {};

for(int i=0; i<ip.length(); i++)

{

    // cout << "Line " << i << ": " << ip.at(i) << " ";

    if(ip.at(i) == '0')

    {

        doubleColonLength += 1;

        if(doubleColonStart == -1)

            doubleColonStart = i;

        if(doubleColonLength > bestDoubleColonLength)

        {

            bestDoubleColonLength = doubleColonLength;

            bestDoubleColonStart = doubleColonStart;

        }

    }

    else if(ip.at(i) == ':')

    {

        doubleColonLength += 1;

    } //If entry isnt a '0 or ':' reset the current streak to 0

    else

    {

        doubleColonLength = 0;

        doubleColonStart = -1;

    }    

    // cout << bestDoubleColonStart << " " << bestDoubleColonLength << endl;

}

if(bestDoubleColonLength > 5)

{

    // cout << bestDoubleColonStart << " " << bestDoubleColonLength << " " ;

    // if(bestDoubleColonEnd == ip.length())

    //     ip += "";

    ip.replace(bestDoubleColonStart, bestDoubleColonLength, ":");

    // if(bestDoubleColonStart == 0)

    //     ip = "" + ip;

}

std::regex zeroBlock("(0000)");

ip = std::regex_replace(ip, zeroBlock, "0");

cout << ip << endl;

}

Is the current validator 8, the one that nicola included ? 0000:0000:a000:0000:000a:0030:0000:0000

I don’t remember but I don’t think so. :grin:

Thanks for letting me know. I was worried that might be the case.