49
\$\begingroup\$

This reminds me when a few years ago someone uploaded a torrent "Hacker tool: complete list of all IP addresses". This was, of course, just a generated list of the ~4 billion IPv4 addresses, but thousands of "h4xx0rz" downloaded it. Look mum, imahacker!

This was then, but nowadays everybody has switched to IPv6. (Right?)

Your task is to write a program that prints out all IPv6 addresses.

You should write a full program that takes no input and prints IPv6 addresses, one per line, and no other output. Your program must print all 2128 possible addresses, including invalid ones. Each address must be printed exactly once. You may print the addresses in any order.

Each address may be printed in full, with 8 groups of 4 hexadecimal digits separated by colons, e.g.

2001:0db8:85a3:0000:0000:8a2e:0370:7334

You may, at your discretion, use any of the standard abbreviations from RFC 5952:

  • Leading zeros in a group may be omitted, except that 0 cannot be abbreviated further.
  • :: may be used at most once per address to abbreviate a sequence of one or more all-zero groups.
  • The hexadecimal digits may use lowercase or uppercase.

If you achieve the representation recommendation from RFC 5952 (lowercase letters only, shortest possible representation, with :: used as early as possible if there are multiple places where it can be used), you get a -20% bonus.

Due to the size of the output, your program is not expected to finish while we're sitting there. Your program may be interrupted by external means at some point (Ctrl+C, pulling out the power, …). Your program must produce output as a stream, so that after a “reasonable” wait, it will have produced some lines. Basically, building a giant string in memory only to print it at the end is not allowed. Any program that would run out of memory on a “standard” PC is disqualified. (Nonetheless, if your program was left to run for sufficiently long, it must print all IPv6 addresses and then exit.)

(If this condition is a problem for web interpreters that run the program until completion and then let you see the output, and you don't have a hosted interpreter, test your program on a smaller version of the problem, and then carefully adjust it to the full 2128.)

Your score is your program's length in bytes, multiplied by 0.8 if you get the bonus. It's code golf, so the lowest score wins.

\$\endgroup\$
13
  • 24
    \$\begingroup\$ This is 5.445*10¹⁵ yottabytes. To store all of that data, you need at least 15 Earth-sized datacenters containing nothing but hard drives packed in as tightly as possible. That's one big torrent..... \$\endgroup\$
    – Kaz Wolfe
    Commented Oct 7, 2015 at 9:26
  • 7
    \$\begingroup\$ @Mew I'm wondering how small the file would be compressed (using some generic compression like gzip) though. \$\endgroup\$
    – SztupY
    Commented Oct 7, 2015 at 12:54
  • 36
    \$\begingroup\$ @SztupY: apparently the output can be compressed to 25 bytes (Decompression algorithm = a Pyth interpreter, compression algorithm = posting to PPCG) From lurking on this site, it seems that Pyth is a rather generic compressed format. \$\endgroup\$
    – Ben Voigt
    Commented Oct 7, 2015 at 16:24
  • 3
    \$\begingroup\$ In my experience, @Gilles is a real SO heavyweight! Glad to see you bringing questions to our little PPCG corner of SO! Also fun for me to see networking-related questions. \$\endgroup\$ Commented Oct 7, 2015 at 18:08
  • 5
    \$\begingroup\$ This reminds me of that "iterate over all possible GUIDs" question on SO. \$\endgroup\$ Commented Oct 7, 2015 at 19:08

22 Answers 22

53
\$\begingroup\$

Python 3, 65 bytes · 0.8 = 52.0

from ipaddress import*
n=4**64
while n:n-=1;print(IPv6Address(n))
\$\endgroup\$
4
  • 7
    \$\begingroup\$ Dang python! It always has the right methods! :D \$\endgroup\$
    – bren
    Commented Oct 9, 2015 at 1:57
  • \$\begingroup\$ ipaddress is python3 only. \$\endgroup\$
    – user45510
    Commented Jan 28, 2016 at 20:05
  • \$\begingroup\$ @Hurricane996, yeah, I had been using it in Python 2 from PyPI without realizing that it only entered the standard library in Python 3. So I’ve switched this to Python 3 at the expense of one byte. \$\endgroup\$ Commented Feb 1, 2016 at 18:14
  • \$\begingroup\$ @MDXF Python doesn't have ++ or -- operators \$\endgroup\$
    – Draconis
    Commented Dec 5, 2017 at 1:52
14
\$\begingroup\$

Pyth, 27 25 24 bytes

Note: the code had a bug previously, fixing it saved 1 byte

J^4 64WJj\:c%"%032x"=tJ4

Prints the addresses like

ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe
ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffd
ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc
...
0000:0000:0000:0000:0000:0000:0000:0003
0000:0000:0000:0000:0000:0000:0000:0002
0000:0000:0000:0000:0000:0000:0000:0001
0000:0000:0000:0000:0000:0000:0000:0000

Previous (more complicated) version using the pad operator (also 24 bytes):

J^4 64WJj\:c.[\032.H=tJ4

Explanation

J^4 64                  set J to 2^128
WJ                     while J is not 0:
            =tJ               decrement J
    %"%032x"                 format to length-32 hex string
   c           4            split every 4 chars
j\:                        join by : and print

Pyth, 21 bytes (invalid)

jmj\:c.[\032.Hd4^4 64

This can't be run since 1) it would consume at least 2132 bytes (252 yobibytes) of memory and 2) the interpreter doesn't like it (2128 doesn't fit in ssize_t, so no lists of that size). It would print the addresses in lexicographical order. You can try out the algorithm by changing the number(s) in the end to something usable.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ What about ... "Any program that would run out of memory on a “standard” PC is disqualified." ? \$\endgroup\$ Commented Oct 8, 2015 at 0:19
  • 2
    \$\begingroup\$ @TessellatingHeckler The first one won't, as it runs iteratively. The second one I have clearly marked as invalid. \$\endgroup\$ Commented Oct 8, 2015 at 7:36
12
\$\begingroup\$

C (with GCC extensions), 76 bytes * 0.8 = 60.8

__uint128_t i;main(){char s[50];for(;inet_ntop(10,&i,s,49),puts(s),++i>0;);}

This uses the 128-bit integers GCC extension to simply count up from :: to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff. inet_ntop() correctly formats each address so the -20% bonus can be claimed.

Output

Using sed to output every millionth line up to 10 million:

$ ./ipv6all | sed -n '1~1000000p;10000000q'
::
4042:f00::
8084:1e00::
c0c6:2d00::
9:3d00::
404b:4c00::
808d:5b00::
c0cf:6a00::
12:7a00::
4054:8900::
$ 

Note I am using a little-endian x86_64 machine, and that network addresses are typically always in network-order (big-endian), so the endianness is effectively swapped by using inet_ntop(). This does not matter - all addresses will still (eventually) be displayed.

\$\endgroup\$
1
  • \$\begingroup\$ __int128_t i;s[];main(){for(;inet_ntop(10,&i,s,49),puts(s),++i;);} is 66 bytes, you can always make s[] -> s[50] for +2 bytes just to be sure, but this works \$\endgroup\$
    – c--
    Commented Feb 8, 2023 at 16:22
11
\$\begingroup\$

CJam, 36 27 bytes

G32#{(_"%032x"e%4/':*oNo}h;

-9 bytes thanks to @Dennis (I forgot that CJam has string formatting). Prints the addresses lowercase and descending.

For obvious reasons, use the Java interpreter, not the online one. You can replace G32# with something smaller for testing online though, e.g. here's the last 100.

Explanation

G32#             16^32 = 2^128. Call this n
{ ... }h;        While loop. The final ; is to pop n at the end
 (               Decrement n
 _               Copy n
 "%032x"e%       String format to hex, padded to 32 digits
 4/              Split into groups of 4
 ':*             Join with colons
 oNo             Output with newline
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What's kind of interesting is that the online interpreter, beyond not being able to handle the size of course, also prints the result wrong. If you remove the loop, and only print the first value, it prints 0000:0000:0000:0000:0000:0000:ffff:ffff. Looks like the string formatting might work differently online. I confirmed that it works fine with the offline version. \$\endgroup\$ Commented Oct 8, 2015 at 15:59
  • \$\begingroup\$ n is the same as oNo in TIO. \$\endgroup\$ Commented Apr 27, 2017 at 19:06
8
\$\begingroup\$

Python 2.7, 67 bytes

n=4**64
while n:n-=1;s='%032x'%n;exec"s=s[4:]+':'+s[:4];"*7;print s

As a side effect of the method used to insert the colons, the addresses are printed with the rightmost column appearing at the left:

ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
fffe:ffff:ffff:ffff:ffff:ffff:ffff:ffff
fffc:ffff:ffff:ffff:ffff:ffff:ffff:ffff
...
0003:0000:0000:0000:0000:0000:0000:0000
0002:0000:0000:0000:0000:0000:0000:0000
0001:0000:0000:0000:0000:0000:0000:0000
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The rotating is really neat! Also belated, but welcome from anarchy :) \$\endgroup\$
    – Sp3000
    Commented Oct 8, 2015 at 13:05
  • 3
    \$\begingroup\$ If you say the rightmost column is at the left, it's printing invalid IPv6 addresses, but if the columns are in the correct places, then it's [printing] the addresses in any order. ;) \$\endgroup\$ Commented Oct 8, 2015 at 15:59
8
\$\begingroup\$

Cinnamon Gum, 16 bytes

0000000: 678b 36d0 b54c d44d 8bc5 455b 8d0c 0500  g.6..L.M..E[....                               .

Try it online. (TIO limits output)

Explanation

The g mode puts Cinnamon Gum in generate mode. The rest of the string decompresses to this regex:

[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]

It then creates a generator of all possible strings that match the regex and iterates through it, printing out each one.

Somewhat amusingly, the golfier regex ([0-9a-f]{4,4}:){7,7}[0-9a-f]{4,4} actually compresses to a longer string than the regex above.

\$\endgroup\$
0
7
\$\begingroup\$

Verilog, 335

My first Verilog submission, probably could use more golfing but I don't have the energy to do so right now. c is clock, o is ASCII output. Does not qualify for formatting bonus due to zero-padding instead of abbreviating.

module b(output[0:38]o,input c);reg[127:0]a;wire[0:39]d;assign o=d[0:38];always @(posedge c) a<=a+(~(&a));genvar i,j;generate for(i=0;i<8;i=i+1) begin:q for(j=0;j<4;j=j+1) begin:r assign d[5*i+j]=a[16*i+4*j:16*i+4*j+7]>9?{4'h6,a[16*i+4*j:16*i+4*j+7]-9}:{4'h3,a[16*i+4*j:16*i+4*j+7]};end assign d[5*i+4]=8'h3A; end endgenerate endmodule

This is a simple iteration followed by some bit-twiddling to make the output ASCII. I chop the colon after the last group with a small hack. Synthesizes and appears to work for xc3s500e-4ft256-4 on ISE 13.7 lin64.

\$\endgroup\$
6
\$\begingroup\$

Pyth, 21 bytes

KJ^8CdWJj\:ct.H+K=tJ4

Uses a while loop with J as the iterator variable. Initializes the maximum using 8^chr(' '). Pads by adding that initial value, converting to hex, then removing the first character.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That code looks like somebody sneezed on their keyboard, then tried to clean it. \$\endgroup\$
    – darksky
    Commented Dec 5, 2017 at 1:17
  • \$\begingroup\$ @darksky That's golflangs for you :P \$\endgroup\$ Commented Dec 5, 2017 at 2:55
6
\$\begingroup\$

C, 91-126 bytes

My original version, 119 bytes.

long a[9],i;
f(long*x){if(65536&++*x)*x=0,f(x+1);}
main(){for(;!a[8];f(a))for(i=7;i+1;i--)printf(i?"%lx:":"%lx\n",a[i]);}

Best golfed portable-ish version, 103 bytes (thanks @Dennis for some of these concepts)

long*p,a[9];
main(i){while(!a[8]){
for(i=8;i--;printf(i?"%lx:":"%lx\n",a[i]));
for(p=a;++*p>>16;*p++=0);}}

Explanation: The algorithm itself is reasonably straightforward. I used long rather than unsigned int because it's shorter. Declaring them at the file level means everything's preinitialized with zeros. The f function is a simple increment with carry that operates on the low 16 bits of each word. The loop ends when it carries into the 129th bit.

Iterating backwards for the printf means that we print the addresses in the "proper" order and also the check for printing a newline is a few characters shorter.

This does use some non-portable constructs. It's best regarded as a K&R dialect of C, since it uses implicit int return types and does not include stdio.h. And my use of long was informed by this - on most modern systems int is sufficient because it's 32 bits. This could probably run unmodified on PDP-11 Unix.

However, it can be shorter. If we assume that we can use int (either as a type wider than 16 bits, or a type of exactly 16 bits with various properties that happen to be true on many systems such as twos complement and arithmetic rollover), we can get rid of the stuff related to using long.

Version for int wider than 16 bits, 97 bytes.

a[9],*p;main(i){while(!a[8]){
for(i=8;i--;printf(i?"%x:":"%x\n",a[i]));
for(p=a;++*p>>16;*p++=0);}}

Version for 16-bit systems, 91 bytes.

a[9],*p;main(i){while(!a[8]){
for(i=8;i--;printf(i?"%x:":"%x\n",a[i]));
for(p=a;!++*p;p++);}}

Oddly enough, though, the original K&R compiler didn't actually support the declaration without int (it compiles fine, but treats the variables as external and therefore undefined at link time), so an additional three bytes are needed to change the declaration to int*p,a[9]; for a total of 94.

Also, if the assumption that it is interrupted before completing output were a hard constraint, we could remove the end check, saving five bytes.

Bonus: fully ANSI portable version, 126 bytes:

#include<stdio.h>
long*p,i,a[9];
int main(){while(!a[8]){
for(i=8;i--;printf(i?"%lx:":"%lx\n",a[i]));
for(p=a;++*p>>16;*p++=0);}}

Newlines in all versions are inserted for readability and in locations where whitespace is not required, and are excluded from the byte count, except for the newline after the #include line in the ANSI version.

All versions except the ANSI version fall through at the end of main and therefore may return a spurious exit code to the operating system.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Portability usually isn't a concern here. This works on my machine: a[9];f(int*x){if(++*x>>16)*x=f(x+1);}main(i){for(;!a[8];f(a))for(i=8;i--;)printf(i?"%x:":"%x\n",a[i]);} \$\endgroup\$
    – Dennis
    Commented Oct 7, 2015 at 16:53
  • \$\begingroup\$ You are clearly much better at this than me. Some of those I am very surprised works ,but I should really have thought of the i-- condition check. \$\endgroup\$
    – Random832
    Commented Oct 7, 2015 at 17:11
  • \$\begingroup\$ When you tested this, did you run it all the way until the carry? My machine puts a random value in a[0] after rolling over in your version. \$\endgroup\$
    – Random832
    Commented Oct 7, 2015 at 17:16
  • \$\begingroup\$ Yes. The first 6,553,601 IP addresses are printed correctly on my machine. \$\endgroup\$
    – Dennis
    Commented Oct 7, 2015 at 17:18
  • \$\begingroup\$ Ideone doesn't like it either, but it works on codepad: wrap in a[0] and wrap in a[1] \$\endgroup\$
    – Dennis
    Commented Oct 7, 2015 at 17:36
5
\$\begingroup\$

AutoIt3, 142 231 Bytes

For $a=0 To 2^32-1
For $b=0 To 2^32-1
For $c=0 To 2^32-1
For $d=0 To 2^32-1
$s=StringFormat("%08x%08x%08x%08x",$a,$b,$c,$d)
For $j=0 To 8
ConsoleWrite(StringMid($s,$j*4+1,4)&($j<7?":":""))
Next
ConsoleWrite(@LF)
Next
Next
Next
Next

Explanation

  • For $a=0 To 2^32-1: Iterate 4 times over 0-2^32 ((2^32)^4=2^128) possible combinations.
  • $s=StringFormat("%08x%08x%08x%08x",$a,$b,$c,$d): Convert the numbers to a hexadecimal string with a length of 32 (4*32).
  • For $j=0 To 8: Iterate over all 8 sections of the string.
  • ConsoleWrite(StringMid($s,$j*4+1,4)&($j<7?":":"")): Extract the next 4 characters from the string and add a colon (:) at the end, if we haven't reached the last section, then output everything to the console
  • Next: End the inner for-loop
  • ConsoleWrite(@LF): Add a line-feed at the end of the line
  • Next: End the outer for-loops

Expected output size: (One line (39 bytes) + line-feed) (=40 bytes) * 2^128 = 1.361* 10^16 YB (yottabytes)

\$\endgroup\$
2
  • \$\begingroup\$ Don’t you mean 4^64 - 1? \$\endgroup\$ Commented Oct 15, 2015 at 4:35
  • \$\begingroup\$ @AndersKaseorg apparently I need to even use 4 loops up to 2^32-1, because AutoIt can parse large values like 4^64, but can't store them for use in a loop, because integers only go up to 2^32-1 in AutoIt. \$\endgroup\$
    – GiantTree
    Commented Oct 15, 2015 at 10:30
5
\$\begingroup\$

Commodore BASIC 2.0, 339 bytes

In order to get lower-case hex digits, this program is written in "shifted mode" (press <SHIFT>+<C=>)

1k=65535:a=0
2fOb=0tok:fOc=0tok:fOd=0tok:fOe=0tok:fOf=0tok:fOg=0tok:fOh=0tok
3x=a:goS6:?":";:x=b:goS6:?":";:x=c:goS6:?":";:x=d:goS6:?":";:x=e:goS6:?":";:x=f
4goS6:?":";:x=g:goS6:?":";:x=h:goS6:?
5nE:nE:nE:nE:nE:nE:nE:nE:a=a+1:ifa<65536tH2
6y=x/4096:goS7:y=x/256aN15:goS7:y=x/16aN15:goS7:y=xaN15:goS7:reT
7?mI("0123456789abcdef",y+1,1);:reT

Simply making this work on the Commodore 64 was a challenge, because of memory, screen size, data size, and other limitations. I considered implementing the abbreviated representation, but other limitations (such as the undocumented inability to use array elements as loop indices) meant it would increase the length of the program by an estimated 1000 bytes.

Line 7 is an implementation of HEX$(), which Commodore BASIC 2.0 is lacking. I can't use a DEF FN for this because those can only return numbers, not strings. Line 6 is a subroutine that applies it to a group of four digits, which would have been considerably shorter if functions could return strings.

Lines 2 and 5 are eight nested loops, implemented as seven "for" loops and a conditional goto because eight "for" loops, when combined with the two "gosubs" for printing out the address, will overflow the C64's tiny stack.

A C64 can print out about 1.2 addresses per second, for an estimated runtime of 1.3*10^31 years.

\$\endgroup\$
4
\$\begingroup\$

PowerShell (v4), 193 166 162 145 103 bytes

TimmyD's no-bonus version at 103 bytes:

$i=[bigint]::Pow(4,64);while($i-gt0){('{0:X32}'-f($i-=1)-replace'0(?=.{32})'-re‌​place'.{4}(?!$)','$0:')}

Previous with-bonus version at 145 * 0.8 = 116 bytes

With help from TimmyD and tomkandy, who points out that 0 -eq $false but ([bigint]0) -eq $true. So all my previous versions won't terminate.

$i=[bigint]::Pow(4,64);while($i-gt0){$i-=1;[IPAddress]::Parse((('{0:X32}'-f$i
)-replace'0(?=.{32})'-replace'.{4}(?!$)','$0:')).IPAddressToString}

Previously at 162, before some regex changes:

$i=[bigint]::Pow(4,64)
while($i){$i-=1;if(($x='{0:X32}'-f$i).Length-eq33){$x=$x.Substring(1)}
[IPAddress]::Parse(($x-replace'.{4}(?!$)','$0:')).IPAddressToString}

"A challenge where PowerShell ought to be reasonably competetive!" - me, before I tried it.

Explanation

# PowerShell (PS) has no IP address arithmetic, e.g. IP + 1
#- PS has no 128 bit integers
#- PS has no automatic bignums

# Start from the top, with the BigInteger specialised Power()
$i = [BigInt]::pow(4,64)

# Loop 4**64 through 1, work with $i-1 for ff... -> ::0
while ($i) {
    # PS has no decrement operator for bignums
    # (no using $i-- in the while loop test)
    $i-=1

    # The Net.IPAddress class can't turn a BigInteger
    # into an IPv6 address directly. And because it mashes
    # IPv4 and IPv6 into one class, there's no obvious way 
    # to make a small number always cast to an IPv6 address.
    # Format the bignum as a string of 32 hex digits.
    $x = '{0:X32}' -f $i

    # The BigInteger often formats as /33/ hex digits, 
    # with a leading zero (to avoid unintentional +/- sign bits)
    # ( https://msdn.microsoft.com/library/dd268287 )
    # So remove the leading 0, if there is one
    if (($x).Length-eq33){$x=$x.Substring(1)}

    # I can't always remove the leading zero, because it 
    # can't parse FFFFF... into an address without colons
    # and this regex replace into groups of 4 with colons
    # would go wrong at length 31. No : after the last group
    # This is still better than split/join ... because there
    # isn't a split-into-groups-of-N that I know of.
    $x = ($x -replace '.{4}(?!$)', '$1:'
   
    # Woo! * 0.8 bonus! 45 characters to save 38! :D
    [IPAddress]::Parse($x).IPAddressToString

}
\$\endgroup\$
6
  • \$\begingroup\$ 95 without bonus. Thanks to you two for introducing me to [bigint], that's rather handy (for things I oughtn't to be doing in posh in the first place...) for($g=[bigint]::pow(2,128);$g;$g-=1){'{0:X32}'-f$g-replace'(?=(.{4})+$)',':'-replace'^0+:',''} \$\endgroup\$
    – tomkandy
    Commented Oct 8, 2015 at 17:29
  • \$\begingroup\$ Sorry should be for($g=[bigint]::pow(2,120);$g;$g-=1){'{0:X32}'-f$g-replace'(?=(.{4})+$)',':'-replace'^0*:',''} \$\endgroup\$
    – tomkandy
    Commented Oct 8, 2015 at 17:58
  • \$\begingroup\$ for($g=[bigint]::pow(2,128);$g-gt0;$g-=1){'{0:X32}'-f$g-replace'(?=(.{4})+$)',':'-replace'^\d*:',''} Yes, the first address is wrong but it's not repeated at the end. Also note that while($i) in yours won't stop at zero - [boolean][bigint]0 evaluates as true \$\endgroup\$
    – tomkandy
    Commented Oct 9, 2015 at 11:30
  • \$\begingroup\$ @tomkandy oh wow, I must have only ever tested the end by setting $i=5 as an int. (Complex 0 isn't $false either ... and nor is empty string. I should pay more attention to "it's not Python"). Thanks! (And this script of yours doesn't get rid of the leading 0: anymore :/ ) \$\endgroup\$ Commented Oct 9, 2015 at 15:37
  • \$\begingroup\$ @TessellatingHeckler Not going for the bonus is sadly 13 bytes shorter -- $i=[bigint]::Pow(4,64);while($i-gt0){('{0:X32}'-f($i-=1)-replace'0(?=.{32})'-replace'.{4}(?!$)','$0:')} at 103... \$\endgroup\$ Commented Dec 14, 2015 at 20:58
3
\$\begingroup\$

AutoIt3, 137 Bytes

For $i=0 To 4^64
$s=StringFormat("%032x",$i)
For $j=0 To 7
ConsoleWrite(StringMid($s,$j*4+1,4)&($j<7?':':''))
Next
ConsoleWrite(@LF)
Next
\$\endgroup\$
3
  • \$\begingroup\$ I know that but I'm new here :( \$\endgroup\$
    – rav_kr
    Commented Oct 8, 2015 at 16:14
  • \$\begingroup\$ Just making sure you know. Thanks. \$\endgroup\$
    – mbomb007
    Commented Oct 8, 2015 at 16:58
  • \$\begingroup\$ Don’t you mean 4^64 - 1? \$\endgroup\$ Commented Oct 15, 2015 at 4:35
3
\$\begingroup\$

Regenerate -a, 24 bytes

(($3:!)[0-9a-f]{4}()){8}

Attempt This Online!

(
    # Match ":" iff we're not on the first iteration
    (
        $3        # Match iff we're not on the first iteration
        :         # Match ":"
    !             # Short-circuiting alternation - only try matching the below
                  # if it was impossible for the above to match.
                  # Do nothing
    )
    [0-9a-f]{4}   # Match 4 hexadecimal digits
    ()            # $3 = signal that we're not on the first iteration anymore
)
{8}               # Repeat the above 8 times

Alternative 24 bytes:

(({#1}:!)[0-9a-f]{4}){8}

Attempt This Online!

It might not be intended that this is possible, but Regenerate allows a quantifier to be preceded by nothing. This is what's happening with {#1} - it's impossible for it to match on the first iteration because $1 hasn't been captured yet and doesn't have a length. On subsequent iterations, a match of nothing gets repeated 4 times (the length of $1).

\$\endgroup\$
2
\$\begingroup\$

Python 2, 95 bytes

def i(p=0):
 while p<4**64:print':'.join(hex(p)[2:].zfill(32)[4*s:4*s+4]for s in range(8));p+=1

Simply goes through every number from 0 to 2^128. First it converts the current number to hexadecimal string, then strips off the '0x' that that function gives. Next it adjusts the string to have 32 zeros in the front and then breaks it up into groups of four. Finally it joins the groups of four with colons, prints that out and adds 1 to the current number. Has the added bonus that you can start it at any value if you give it one, but no input is needed.

\$\endgroup\$
2
  • \$\begingroup\$ If your answer is a function, you don't need to call it :) \$\endgroup\$
    – Beta Decay
    Commented Oct 9, 2015 at 17:30
  • \$\begingroup\$ @BetaDecay Then that was my misunderstanding. Fixed! Thanks. \$\endgroup\$
    – Status
    Commented Oct 9, 2015 at 18:08
2
\$\begingroup\$

Haskell 111

s[]=[[]]
s(a:b)=[y:z|z<-s b,y<-a]
r=replicate
main=mapM putStrLn$s$tail$concat$r 8$":":r 4"0123456789abcdef"

With my own sequence function s it no longer leaks memory, but does not feel golfed any more.

\$\endgroup\$
1
  • \$\begingroup\$ How do you compile it so that it doesn't run out of memory? With my ghc v7.10.2 and std. compile options it leaks memory. \$\endgroup\$
    – nimi
    Commented Oct 9, 2015 at 19:08
2
\$\begingroup\$

CBM BASIC v7.0 (166 characters)

a=65535
fOi=0toa:fOj=0toa:fOk=0toa:fOl=0toa:fOm=0toa:fOn=0toa:fOo=0toa:fOp=0toa:?hE(i)":"hE(j)":"hE(k)":"hE(l)":"hE(m)":"hE(n)":"hE(o)":"hE(p):nE:nE:nE:nE:nE:nE:nE:nE

Mark's answer is for the Commodore 64's BASIC 2.0, which lacks a built-in command for printing numbers in hexadecimal. However, thanks to the HEX$() function in BASIC 7.0, the Commodore 128 version is much shorter. It doesn't fit on a single logical line (which on the C128 is limited to 160 characters) but can still be entered as two separate lines in direct mode.

\$\endgroup\$
2
  • \$\begingroup\$ It looks like you're missing half your loops. An IPv6 address is 128 bits, not 64 bits. \$\endgroup\$
    – Mark
    Commented Oct 16, 2015 at 18:21
  • \$\begingroup\$ @Mark: Thanks for pointing this out! I've fixed the problem. \$\endgroup\$
    – Psychonaut
    Commented Oct 18, 2015 at 8:52
2
\$\begingroup\$

Ruby 75

x=->s,n{n>0?65536.times{|m|x.(s+?:*(8<=>n)+m.to_s(16),n-1)}: p(s)};x.('',8)

This is a recursive solution that takes a each prefix and finds every possible suffix. Recursively.

\$\endgroup\$
1
  • \$\begingroup\$ Get shorter function definitions with lambdas: x=->s,n{...};x['',8] \$\endgroup\$
    – Doorknob
    Commented Oct 7, 2015 at 20:53
2
\$\begingroup\$

Zsh -P, 60 bytes

x=({0..9} {a..f})
eval for\ {1..8}' ($x$x$x$x)<<<${(j/:/)@}'

Attempt This Online! (Warning: if you don't click the kill button fairly quickly, your browser will run out of memory and crash)

eval for\ {1..8} creates 8 nested loops using the loop variables $1, $2, ..., $8. These numbered variables can then be referred to implicitly using $@ (here, they're joined with colons using ${(j/:/)}).

The <<<${...} section is actually repeated for each level of nested loop as well, but (apart from the last one) they all do nothing, because they feed into the next loop, which ignores its input.

The -P option enables an implicit cartesian product on the adjacent expansions $x$x$x$x. This is the shortest way I could find to count up to 0xffff in hex without needing to convert each loop variable to hex individually.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 21 bytes

₇E(nk6τ₅32ε\0*p4ẇ\:j,

Try it Online!

Ugh, we really need a zfill builtin.

\$\endgroup\$
1
\$\begingroup\$

Go, \$243 \times 0.8 = \$ 194.4 bytes

import(."fmt";."net/netip")
func F(){for a,M:=0,1<<32-1;a<M;a++{for b:=0;b<M;b++{for c:=0;c<M;c++{for d:=0;d<M;d++{s:=""
for n,k:=0,Sprintf("%08x%08x%08x%08x",a,b,c,d);n<8;n++{s+=k[4*n:4*(n+1)]+":"}
A,_:=ParseAddr(s[:len(s)-1])
Println(A)}}}}}

Attempt This Online!

Prints to STDOUT. Uses the netip.Addr type from the Golang standard library to do the formatting for me.

Explanation

import(."fmt";."net/netip")
func F(){
for a,M:=0,1<<32-1;a<M;a++{ // first 8 digits
for b:=0;b<M;b++{           // next  8 digits
for c:=0;c<M;c++{           // next  8 digits
for d:=0;d<M;d++{           // final 8 digits
k:=Sprintf("%08x%08x%08x%08x",a,b,c,d)  // join the numbers into a single string
s:=""
for n:=0;n<8;n++{s+=k[4*n:4*(n+1)]+":"} // split into 8 groups of 4, split by colons
A,_:=ParseAddr(s[:len(s)-1])            // parse the address
Println(A)}}}}}                         // print using the default String() method, which formats the address
\$\endgroup\$
0
\$\begingroup\$

Tcl 341 318 301

proc ip6 {p c} {
    set s %x:%x:%x:%x:%x:%x:%x:%x
    set p [scan $p $s]
    while {[set d 7]} {
        $c [format [string map {x 04x} $s] {*}$p]
        while {[set i [lindex $p $d]]==0xFFFF} {
            lset p $d 0
            if {!$d} return
            incr d -1
        }
        lset p $d [incr i]
    }
}
ip6 fFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0000 puts
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.