IPv6 Shortener
Statement
Goal
An IPv6-Address is the next-generation network address for electronic devices.Compared to an IPv4 address, it is made up of 8 blocks that are 4 hexadecimal digits long, split by colons.
An example of an IPv6 address is 2001:0000:3c4d:0015:0000:0000:0db8:1a2b
To avoid needing to write so many digits, you can short an IPv6 address very easily by removing unneeded zeros.
The goal of this puzzle is to compress the IPv6 address, that it gets shortened.
The shortened version would look like this: 2001:0:3c4d:15::db8:1a2b
You can see that the leading zeros of all the blocks were removed, and a double colon replaced the longest chains of blocks that contained zeros only.
To find out where the double colon needs to be placed, you simply take a look where the longest streak of zero-only blocks is:
2001:0000:3c4d:0015:0000:0000:0db8:1a2b
|----|
This is a zero-only block, but not the longest - it can be shortened to simply a 0
2001:0:3c4d:0015:0000:0000:0db8:1a2b
|---------|
When you find the longest streak, as in the example above, simply replace it with a ::
If there are two or more streaks, separate from each other, with the same length, replace the first one starting from the left. The number of zero-only blocks does not matter how many colons you need to use - it is always two colons.
The last step is to remove the leading zeros of every block.
If everything was successful, then you have your shortened IPv6, in this case: 2001:0:3c4d:15::db8:1a2b
Notes: The double colon may only be used once in the whole address at the longest streak of zero blocks, and only if there are two or more zero blocks next to each other. The rest of the single blocks that contain zeros only must be represented as only one zero in the whole block, no matter if they are next to each other.
Input
A string ip that is the uncompressed IPv6 address.
Output
A string output that is the compressed IPv6 address.
Constraints
Length of IP: 39 symbols, 8 blocks to 4 hexadecimal symbols split by 7 colons
Example
Input
2001:0000:3c4d:0015:0000:0000:0db8:1a2b
Output
2001:0:3c4d:15::db8:1a2b
Tags
Regular expressions, Strings, Data compression, Loops, Parsing
Difficulty
Easy
Test cases
Simple Address Test
Input
2001:0000:3c4d:0015:0000:0000:0db8:1a2b
Output
2001:0:3c4d:15::db8:1a2b
Simple Address Validator
Input
2001:0000:0000:0015:27ab:1203:1a2f:1a2b
Output
2001::15:27ab:1203:1a2f:1a2b
Loopback Address Test
Input
0000:0000:0000:0000:0000:0000:0000:0001
Output
::1
Similar to Loopback Address Validator
Input
0000:0000:0000:0000:0000:0000:0001:0001
Output
::1:1
Multiple Zero Blocks Test
Input
2001:0000:0000:0000:0001:0000:1a2f:1a2b
Output
2001::1:0:1a2f:1a2b
Multiple Zero Blocks Validator
Input
2001:0000:0000:0000:3ac3:0000:0000:1a2b
Output
2001::3ac3:0:0:1a2b
No Zero Blocks Test
Input
2001:2021:ab23:82ae:0021:0001:1a2f:1a2b
Output
2001:2021:ab23:82ae:21:1:1a2f:1a2b
No Zero Blocks Validator
Input
2001:2071:ab23:82ae:0021:0073:1a3f:1a2b
Output
2001:2071:ab23:82ae:21:73:1a3f:1a2b
One Zero Block Test
Input
fe80:5c77:3430:7667:0000:ce27:43d0:ab34
Output
fe80:5c77:3430:7667:0:ce27:43d0:ab34
One Zero Block Validator
Input
1a4b:cafe:0029:1317:abe2:0000:2acf:90ab
Output
1a4b:cafe:29:1317:abe2:0:2acf:90ab
No Full Zero Block Test
Input
fe80:0023:2027:00dd:918f:0001:0023:0e68
Output
fe80:23:2027:dd:918f:1:23:e68
No Full Zero Block Validator
Input
fe80:0273:2027:00dd:91ef:0001:023b:0e68
Output
fe80:273:2027:dd:91ef:1:23b:e68
Zero Blocks of Same Length Test
Input
0000:0000:0100:0000:0000:0004:aaaa:0000
Output
::100:0:0:4:aaaa:0
Zero Blocks of Same Length Validator
Input
0000:0000:0000:0100:0000:0000:0000:0006
Output
::100:0:0:0:6
Zero Block at the End Test
Input
0000:aaaa:a000:0000:000a:0030:0000:0000
Output
0:aaaa:a000:0:a:30::
Zero Block at the End Validator
Input
0000:0000:097d:0000:0064:0000:0000:0000
Output
0:0:97d:0:64::
Solution language
Solution
Stub generator input