title: Crash your favorite compiler

text:
# Question
Title: Crash your favorite compiler
Write a perfectly legal code in a decent language of your choice whose compiling will either crash the compiler or send it into an infinite loop (infinite compile time).

**Restrictions:**

* Use a standard language that is used in real world.
* Use a standard, well-developed compiler (no answers like "I wrote my C compiler that crashes on everything").
* The code must be legal in the language (so most likely you'll have to exploit a compiler or a language bug).
* Give your compiler version and options used so that others can replicate it.
* Explain why the compiler crashed, if possible.

Have fun :)

# Answer
> 20 votes
I'm pretty sure it's been fixed now, but it used to be that you could crash the Java compiler (or, crash Eclipse) by writing

```
class Foo {
  static double d = 2.2250738585072012e-308;
}

```

http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/

Actually, according to that page, the compiler will just hang, not crash. Still, I thought that was pretty fun.

# Answer
> 49 votes
My favorite solution for GHC:

```
data Bad a = C (Bad a -> a)

xx :: Bad a -> a
xx (x@(C x')) = x' x

omega :: a
omega = xx (C xx)

main = omega

```

For GHC 6.12.1 both `ghci Bad.hs` and `ghc Bad.hs` loop infinitely. GHC 7.4.1 loops infinitely when `ghc -O2 Bad.hs` is executed.

**Explanation:** `omega` is defined using an infinite recursion (the only way it can inhabit any type). Compiler's inliner sees `xx` as a simple, non-recursive function, so it tries to inline it in the definition of `omega`. It results in `(\x@(C x') -> x' x) (C xx)`. Seeing a pattern match on a constructor the compiler tries to reduce it, getting `xx (C xx)` again and loops. The trick is that `xx` is actually recursive, but the recursion is hidden within the data type.

*Note: While writing the puzzle, I forgot I left GHC running in the infinite loop. It took all my memory, crashed Firefox and I barely managed to kill it without hard reset.*

# Answer
> 18 votes
This is easy in any dependently-typed language. Type-checking general dependent types is undecidable since it may require arbitrarily complex computations (Turing-complete). You can simply encode in a dependent type a too-large value. Then the type-checker will use all available memory and crash. For instance, in Coq, ReyCharles gives the example of `Compute 70000.`, which causes the type-checker to construct a giant Peano numeral and crash.

In more common languages that support some sort of macro expansion or metaprogramming, you can do something similar. For example, you can use all available memory in C:

```
#include <stdio.h>
#define a printf("%s", "Hello, world!\n");
#define b a a a a a a a a a a a a a a a a
#define c b b b b b b b b b b b b b b b b
#define d c c c c c c c c c c c c c c c c
#define e d d d d d d d d d d d d d d d d
#define f e e e e e e e e e e e e e e e e
// ...
#define z y y y y y y y y y y y y y y y y
int main() { z }

```

The D programming language allows compile-time function execution. This can be used to compute something at compile time that is too large to fit in memory. Something similar can be achieved using C++ template metaprogramming.

In XML (not a compiled programming language, but an XML processor is analogous to a compiler), expanding entities can make the processor run out of memory:

```
<?xml version="1.0"?>
<!DOCTYPE lolz [
 <!ENTITY lol "lol">
 <!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
 <!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
 <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
...
]>
<lolz>&lol999;</lolz>

```

This is called the billion laughs attack.

# Answer
> 18 votes
### C#

Found this on a stackoverflow question:

```
using System;
using System.Linq;

public class Test
{
    public static void Main()
    {
        Enumerable.Range(0, 1).Sum(a =>
        Enumerable.Range(0, 1).Sum(b =>
        Enumerable.Range(0, 1).Sum(c =>
        Enumerable.Range(0, 1).Sum(d =>
        Enumerable.Range(0, 1).Sum(e =>
        Enumerable.Range(0, 1).Sum(f =>
        Enumerable.Range(0, 1).Count(g => true)))))));
    }
}

```

The compiler eventually will crash.

The issue seems related to type inference and/or lambda generation combined with overload resolution.

# Answer
> 15 votes
# VBA

how about if you can crash the IDE by typing in code?

in any Microsoft Office application, try this:

`ALT`+`F11` to get to the VBA window, then try the following code

```
sub foo()
dim v(1 to 3, 1 to 3)
redim preserve v(,1 to 5)

```

and behold:

*You can simply type `redim preserve v(,1 to 5)` into the immediate window, and it will crash after you press `ENTER` !*

# Answer
> 7 votes
## Perl (15)

```
BEGIN{1while 1}

```

This creates an infinite loop **at compile time**:

> A BEGIN code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed.

(from perlmod)

And that's why Perl isn't able to complete parsing the code. This doesn't terminate:

```
$ perl -MO=Deparse -e 'BEGIN{1while 1}'

```

# Answer
> 6 votes
**TeX**

```
\def\x{\x}\x

```

TeX is a macro-expansion language. Here we define the expansion of the macro `\x` to be `\x` again, and then we add afterwards an invocation of `\x`. TeX gets stuck endlessly replacing `\x` with `\x`.

# Answer
> 6 votes
# Scheme

```
(define-syntax s
    (syntax-rules ()
        ((_ (t) ...) (s (t t) ... (t t) ...))
        ((_ (t u) ...) (s (t) ... (u) ...))))
(s (+))

```

My compiler, Chicken, made the mistake of attempting to expand macros at compile time for "run-time performance" or something. So it payed the price of expanding this one. I've read R5RS. No one said macros had to be expanded at compile time.

Essentially what's going on is the macro expands to an expression of infinite size, constantly doubling in size. Well, to be technical, doubling every other expansion. The compiler's fate is sealed. At least on my system, Chicken caps at 2GB, stalls for a long time trying to garbage collect, then crashes after the garbage collector gives up. It does take a while though because of all the computationally expensive hygienic magic occurring.

Switching between expressions of the form

```
(s (+) (+) (+) (+) ....

```

and

```
(s (+ +) (+ +) (+ +) (+ +) ....

```

seems to very, very dramatically increase the rate of memory consumption as compared to:

```
(define-syntax s
    (syntax-rules ()
        ((_ t ...) (s t ... t ...))))
(s +)

```

I suspect Chicken is a pretty hardy compiler that has some ways of avoiding deep analysis of syntactic expressions when it can get away with it, but my final solution forces the pattern matcher to really dive in.

# Answer
> 5 votes
## J

This segfaults the J interpreter (at least on Linux):

```
15!:1[3#2

```

It tries to read from memory address 2. Interestingly, if you try it with 0 or 1, you get `domain error`.

# Answer
> 4 votes
**Smalltalk** (Squeak dialect, version 4.x)

Very easy, just evaluate this, or accept a method with this literal

```
1.0e99999999999999999999

```

It will try to evaluate the power of 10 in Large Integer arithmetic, just for correctly rounding inf Tsss ;)

**Edit:** how many 9 are necessary?

Since 2^10 is 1024, approximately 10^3, we can roughly approximate 10^n by 2^(10\*n/3). That means that 10^n require 10\*n/3 bits to be represented in binary. We would like to have 10^n not representable.

Assuming 32 bit pointers for the object memory, we know that we cannot address more than 2^32 bytes, that is 2^35 bits. So let's reverse the problem: 2^35 is approximately 32 * 2^30, 32\*10^9. That requires about 11 decimal digits, so with eleven 9, we are sure to generate an error on 32bits Squeak. In 64 bits that would be twenty one 9.

We can also exhaust memory with less 9s, the whole addressable space is not necessarily available, but it's deadly slow to test, the Squeak VM is not optimized for such giant arithmetic unlike GMP.

# Answer
> 4 votes
## Clang++

I just came across this fun bug.

```
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

std::stringstream prog;

constexpr unsigned c_strlen( char const* str, unsigned count = 0 )
{
    return ('\0' == str[0]) ? count : c_strlen(str+1, count+1);
}

template < char t_c, char... tt_c >
struct rec_eval
{
    static void eval()
    {
        rec_eval<t_c>::eval();
        rec_eval < tt_c... > :: eval ();
    }
};
template < char t_c >
struct rec_eval < t_c >
{
    static void eval() {
        switch(t_c) {
            case '+':
                prog<<"++t[i];";
                break;
            case '-':
                prog<<"--t[i];";
                break;
            case '>':
                prog<<"++i;";
                break;
            case '<':
                prog<<"--i;";
                break;
            case '[':
                prog<<"while(t[i]){";
                break;
            case ']':
                prog<<"}";
                break;
            case '.':
                prog<<"putc(t[i],stdout);";
                break;
            case ',':
                prog<<"t[i]=getchar();";
                break;
        }
    }
};

template < char... tt_c >
struct exploded_string
{
    static void eval()
    {
        rec_eval < tt_c... > :: eval();
    }
};
template < typename T_StrProvider, unsigned t_len, char... tt_c >
struct explode_impl
{
    using result =
        typename explode_impl < T_StrProvider, t_len-1,
                                T_StrProvider::str()[t_len-1],
                                tt_c... > :: result;
};

template < typename T_StrProvider, char... tt_c >
struct explode_impl < T_StrProvider, 0, tt_c... >
{
     using result = exploded_string < tt_c... >;
};

template < typename T_StrProvider >
using explode =
    typename explode_impl < T_StrProvider,
                            c_strlen(T_StrProvider::str()) > :: result;

int main(int argc, char** argv)
{
    if(argc < 2) return 1;
    prog << "#include <stdio.h>\n#include <stdlib.h>\nint main(){unsigned char* t=calloc(";
    prog << (1 << sizeof(unsigned short));
    prog << ",sizeof(unsigned short));unsigned short i=0;";

    struct my_str_provider
    {
        constexpr static char const* str() { return "++++[>+++++<-]>+++[[>+>+<<-]>++++++[<+>-]+++++++++[<++++++++++>-]>[<+>-]<-]+++>+++++++++[<+++++++++>-]>++++++[<++++++++>-]<--[>+>+<<-]>>[<<+>>-]<-->>++++[<++++++++>-]++++++++++>+++++++++[>+++++++++++<-]>[[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<.>.[>]>[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<<<.>>>.<<<<.<.<<<<<<<<<<.>>>>>>.[>]<<.[<]>>>>>>>>>.>.>>>>>>>>>.[>]<<.<<<<<<<.[<]>>>>>>>>>.[<]>.>>>>>>>>>.[>]<<.<<<<.<<<<<<<<<<<<<.[>]<<<<<<<<<.[>]<<.[<]>>>>>>>>.[>]<<<<<<.[<]>>>>>..[>]<<.<<<<<<<<<<<<.[<]>>>>.[>]<<.<<<<.[<]>>>>>>.>>>.<<<<<<.>>>>>>>.>>>>>>>>>>.[>]<<<.>.>>>-[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<<[<]>>>>>>>>>>.<.[>]<<.[<]>>>>>>>>>>>.<<.<<<.[>]<<<<<<<<<<.[>]>>]<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]+>[<->-[<+>[-]]]<[++++++++[>+++++++++++++<-]>--.[-]<]<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<<.>>>..>>]"; }
    };

    auto my_str = explode < my_str_provider >{};
    my_str.eval();

    prog << "}";

    std::ofstream ofs(argv[1]);
    if(!ofs) return 2;
    ofs << prog.str() << std::endl;
    ofs.close();

    return 0;
}

```

The goal is to translate Brainfuck into C, using template meta-programming to do most of the work. This code works for smaller Brainfuck programs, such as Hello World, but when I tried to run it with 99 Bottles...

```
$ clang++ -std=c++11 -fconstexpr-depth=1000 bf_static.cpp
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 3.5.2 (tags/RELEASE_352/final)
Target: i386-pc-windows-cygnus
Thread model: posix
clang: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/bf_static-afa982.cpp
clang: note: diagnostic msg: /tmp/bf_static-afa982.sh
clang: note: diagnostic msg:

********************

```

It will successfully compile in GCC (after about 2 minutes), but linking it causes another issue...

```
/usr/lib/gcc/i686-pc-cygwin/4.9.3/../../../../i686-pc-cygwin/bin/as: /tmp/cc0W7cJu.o:
section .eh_frame$_ZN8rec_eval<giant mangled name removed>: string table overflow at offset 10004228
/tmp/cc3JeiMp.s: Assembler messages:
/tmp/cc3JeiMp.s: Fatal error: can't close /tmp/cc0W7cJu.o: File too big

```

Oops.

# Answer
> 3 votes
### Common Lisp

Macros make it easy:

```
(defmacro loop-forever ()
  (loop for x from 0 collecting x))

(defun compile-me ()
  (loop-forever))

```

Compiling `compile-me` calls `loop-forever`, which exhausts heap memory during its expansion and crashes the compiler. If you just want to make the compiler hang indefinitely, then this definition of `loop-forever` will do it:

```
(defmacro loop-forever ()
  (loop))

```

This should work using any CL implementation, unless yours is extremely clever and can detect simple infinite loops, but I seriously doubt any do this. Full protection against this is impossible, of course.

# Answer
> 3 votes
This is my original and concise method to crash GolfScript:

```
{1.}do

```

What this does is set up a forever loop that keeps on pushing 1 onto the stack until memory runs out.

In C/C++, I believe that this original piece of code would crash the compiler:

```
#define a bb
#define b aa
int main(){a}

```

This would get the compiler stuck doubling the amount of a's and turning them into b's and vice versa, so the compiler would pretty soon run out of memory and crash.

Another one is for batch on Windows, if completely freezing the computer rather than just the batch script itself counts. You should type in the following:

```
:a
start %0
goto a

```

This gets into an infinite loop of making copies of itself, which make copies of themselves and so on. This would most likely eventually crash your computer if you ran this little bit of code.

One last one is a VBS bomb. It is another bomb, like the last one, but it instead opens an infinite amount of dialog boxes.

```
set oshell = wscript.createobject("wscript.shell")
do
oshell.run "wscript " & wscript.scriptname
msgbox "blah"
loop

```

This continuously creates a copy of itself and opens up a message box in an infinite loop, which the clones do as well. Running these last two programs is not recommended, as they can freeze your computer and cause you to have to hard boot your computer.

Note that I came up with all of these programs myself.

# Answer
> 3 votes
# Perl

This defines operator overloading at compile time, and runs code at compile time which adds the instances of class together.

(by the way, normally infinite recursion would eat all the memory, but with overloading, it just crashes)

```
package MyAmazingClass;
use 5.010;

use overload '+' => sub {
    my ($first, $second) = @_;
    return $first + $second;
};

sub new {
    my $self = shift;
    return bless {}, $self;
}

# BEGIN runs code at compile time
BEGIN {
    my $instance = MyAmazingClass->new;
    my $sum = $instance + $instance;
    say $sum;
}

```

Output:

```
fish: Job 1, 'perl' terminated by signal SIGSEGV (Address boundary error)

```

# Answer
> 3 votes
## D

(DMD32 D Compiler v2.067.1, Windows build)

```
enum x = "mixin(x);";
mixin(x);

```

Note that this will send the compiler into an infinite loop *and* crash it.

> Error: out of memory

Mechanical snail suggested that compile-time programming features in D could be abused for this purpose, but the solution is perhaps simpler than the kind of techniques he had in mind.

---

For those who aren't familiar with 'string mixins', it's a fairly straightforward macro feature. When the compiler encounters `mixin("asdf")`, it substitutes it with the contents of the string, `asdf`, and tries to compile it again.

The solution above will be expanded like:

```
mixin(x);  ->  mixin("mixin(x);");  ->  mixin(x);

```

So unless the compiler tries to detect this expand-to-same case, it'll enter an infinite loop of expansion.

# Answer
> 3 votes
# Simplex v.0.5, 2 bytes

Too bad this isn't a :

```
2Q

```

Let me explain. From the docs:

> \[`Q`\] Adds program source code to outer program, from the beginning (exclusive of ! characters, if the current byte is not zero) If byte is 2, duplicates current command as well.

So:

```
2 ~~ Manhattan adds 2 to the current byte: 10*0 + 2 = 2.
Q ~~ Adds program source code to the outer program, with Q

```

The outer program is a neat little feature in Simplex: it is evaluated at the end of the program. So, if we keep track...:

```
P1  P2  P3  P4  ...
2Q->2Q->2Q->2Q->...

```

Eventually, memory will run out and the world will end.

# Answer
> 3 votes
# **x86 asm**

"nasm -v" returns "NASM version 2.11.08 compiled on Feb 21 2015" (I'm running it under win7)

The assembler has been running for 1:12:27 so far on an i7, totally saturating one of the cores. The output file is sitting at 0 bytes, memory consumption has been stable at 1,004K - it seems safe to say I've beaten nasm up, rather than just giving it a really, really long task. :)

The key to the trick is the repeat value in the macro - 0xFFFFFFFF. Though, I'm not familiar enough with Nasm's internals to know why exactly it's choking on this. I expected to get a ~16GB output an hour ago.

```
%MACRO INVOKE 1-*
;  %REP    %0 - 1
  %REP     0xffffffff
    %ROTATE   -1
    PUSH    DWORD %1
  %ENDREP
  %ROTATE -1
  CALL    %1
%ENDMACRO

[section .text]
bits 32
org 0x10000

EntryPoint:
    INVOKE dword 666
    ret

```

EDIT: Just checked the task manager, Nasm has been running for 7:40:41 and the memory is now up to 1,016K

# Answer
> 3 votes
# Gnu assembler, generating *huge* output files

This macro attempts to fill the output file with garbage (usually null bytes) until a 4 GB boundary is reached, adds an int to get past that boundary, and recursively calls itself to keep filling the output with 4 GB chunks of garbage. This will fill your hard drive until it is full, at which point the assembler will likely crash.

```
.macro f n #Define a macro named f, taking argument n.
.p2align 32 #Fill file with 0x00's until current address is divisible by 2^32
.long 0 #Add a long after the current address, throwing it off alignment.
.if \n #If n > 0, recursively tail-call itself, decrementing n.
f "(\n-1)"
.endif
.endm #End macro definition.
f 32 #Expand macro f, with n = 32 (output size 4GB*32 = 128GB)

```

Note that infinite recursion cannot be used, as the assembler will catch that special case and stop expanding the macro.

Compilation can be done with `as -o crash.out crash.s` on most Linux distributions.

# Answer
> 2 votes
**PHP 5.3.1** (Segfaults interpreter) (Bug 50261, fixed in 5.3.3)

```
   class testClass
   {
       function testClass ()
       {
           echo 'Output string!';
       }
   }

   class testClass2 extends testClass
   {
       function __construct ()
       {
           call_user_func(array('parent', '__construct'));
       }
   }

   new testClass2;

```

This one was a bit of a problem, because the code above was common in a lot of the code I was working with, making this a fairly widespread issue for us.

(If I recall correctly, at one point this was the only way to call parent constructors in PHP.)

# Answer
> 2 votes
# Common Lisp, 8 bytes

Shorter than the other Common Lisp answer :-)

```
#.(loop)

```

Loop while reading your form.

The Common Lisp standard has no mention of a portable way to make it crash, so I guess we need to have an implementation-defined way. Still 8 bytes:

```
#.(quit) ; ccl

```

... or,

```
#.(exit) ; sbcl

```

When you call `(compile-file "crash.lisp")`, the environments mysteriously "crash".

Joking apart, I am still trying to find a way to really crash the environment (and shortly), but it is really hard. All I get is a nice interaction with the debugger.

# Answer
> 2 votes
# Common Lisp, 29 bytes

Implementation: Clozure CL

**WARNING:** Be careful when running this code, it may kill processes that you don't want it to!

```
#.(run-program"pkill"'("cl"))

```

This runs the shell command `pkill cl` on compile time, which will kill the Lisp process doing the compiling. Not technically a crash, but it has the same effect.

Example usage:

```
$ cat /tmp/t.lisp
#.(run-program "pkill" '("cl"))
$ ccl -n
Welcome to Clozure Common Lisp Version 1.11-dev-r16513-trunk  (LinuxX8664)!

? (compile-file "/tmp/t.lisp")
#P"/tmp/t.lx64fsl"
NIL
NIL
?
zsh: terminated  ccl -n
$

```

# Answer
> 1 votes
# Felix

This doesn't work anymore, but at one point, this code:

```
include "std/control/pchannels";

fun is_square(v: int) => let s = int$ sqrt$ v.float + 0.5f in s*s == v;
fun is_median(v: int) => v % 4 == 0 and (v/4).is_square;

struct Message { value: int; };

proc process(c: int, chan: pchannel[Message]) {
    var i = 0;
    for var b in (c+1)/2 upto c do
        for var a in c - b + 1 upto b do
            if is_median(2*(b*b+c*c)-a*a) or
               is_median(2*(a*a+c*c)-b*b) or
               is_median(2*(a*a+b*b)-c*c) do ++i; done;
        done
    done
    write(chan, Message i);
};

proc main() {
    n := int$ System::argv 1;
    var count = n;
    chan := #mk_pchannel[Message];
    var ntri = 0;

    for var c in 1 upto n perform spawn_pthread { process(c, chan); };

    while count > 0 do
        let v = chan.read in ntri += v.value;
        --count;
    done
    ntri.println;
}

main;

```

This would give a big error:

```
inner_bind_expression raised Not_found [BUG] e=(&((main_mf_60270<60270> ())), (value v))

```

SYSTEM FAILURE bind\_expression' raised Not\_found \[BUG\] Felix compilation "/media/ryan/stuff/felix/build/release/host/bin/flxg" "-q" "--optimise" "--inline=100" "--output\_dir=/home/ryan/stuff/.felix/text" "--cache\_dir=/home/ryan/stuff/.felix/cache" "-I/media/ryan/stuff/felix/build/release/share/lib" "-I/media/ryan/stuff/felix/build/release/host/lib" "--syntax=@/media/ryan/stuff/felix/build/release/share/lib/grammar/grammar.files" "--automaton=/home/ryan/stuff/.felix/cache/media/ryan/stuff/felix/build/release/share/lib/grammar/grammar.files/syntax.automaton" "--import=plat/flx.flxh" "std" "/home/ryan/golf/itri/sl.flx" failed Error 1 in flx: \[strerror\_r\] Failed to find text for error number 1

The issue was here:

```
let v = chan.read in ntri += v.value;

```

`let` expected an expression to follow it, but I put a statement instead. So the compiler freaked out a tad.

More info at https://groups.google.com/forum/m/#!topic/felix-language/J3Hs4j6E0gM.

# Answer
> 1 votes
# JavaScript

```
while (true === true){
console.log(0);
}

```

This sends it into an infinite loop. I used the Codecademy JS compiler and it crashed my browser.

# Answer
> 1 votes
# Javascript

```
function crash(){
  window.location.hash=Math.random(),onhashchange=function(){crash()}
}

```

This one crashes web browsers in a seriously effective fashion. **USE AT YOUR OWN RISK!!!**

# Answer
> 1 votes
# Hassium

File1.has:

```
use "File2.has";

```

File2.has:

```
use "File1.has";

```

This causes Hassium to load and begin compiling File2.has, which tells it to load File1.has, which causes it to load File2.has, and so on.

# Answer
> 0 votes
# LOLCODE 1.2, LOLCODE Common Interpreter/Compiler (lci)

I know this isn't but it's extremely short anyways.

```
OBTW

```

This causes Signal 11:

```
Segmentation fault (core dumped)

```

Why? `HAI1.2` denotes the start of the program, and `OBTW` initiates a multiline comment. But the compiler expects a `KTHXBYE` to close the `HAI`, and a `TLDR` to close the multiline comment.
Note that this will still work to cause segfault with anything other than `TLDR` after `OBTW`.
(By wikipedia's standards, LOLCODE is a just a Weirdlang, not actually esoteric.)
You can grab the interpreter from git/justinmeza/lci.

---
Tags: popularity-contest, compile-time
---


title: Stuffing primes in a box

text:
# Question
Title: Stuffing primes in a box
Your task is write a program or function that can fill a given rectangle with prime numbers. The `width` and `height` of the rectangle will be the input. The output must be a list of `height` strings consisting of `width` digits and spaces. Each horizontal (left to right) and vertical (top to bottom) digit sequence (delimited by space or rectangle borders) of length 2 or greater must be a prime number. Each prime number may be used only once. Leading zeros are not allowed. A trailing new line is optional on the output.

### Example:

```
With input (5, 3) a valid output would be:
11 13
7  0
 173

which scores 11, 13, 173, 17, 103 for a total of 5 points.

```

### Score:

The rectangle size for scoring will be `80, 60`. Each horizontal or vertical prime number of length 2 or more in the rectangle scores one point. The answer with the most points wins. In the event of a tie, the earliest answer will win.

### Rules:

* Standard loopholes are prohibited.
* Your program must not be designed for the `80, 60` size. If I suspect an answer is optimised for this size, I reserve the right to change the rectangle size up to `100, 100`.
* The primes used must be real primes (not probabilistic or pseudoprimes). Your program may compute or check numbers at run time, or have them hard coded. The method of finding the primes is not an important part of the challenge.
* Your answer should include the output text and your code. If your program is too large, you may just include some core algorithm code, with a little explanation.

*Edit: Clarified that real primes needed. Added maximum rectangle size.*

*Edit 2: Clarified what code needs to be posted.*

# Answer
# Clingo with Python, ~~1600~~ ~~1689~~ 1740 primes

## Approach

I generate a giant constraint satisfaction problem and solve it using an industrial strength satisfiability solver. Lots of magic has gone into making satisfiability solvers relatively fast (even though the problem is NP-complete in general), but fortunately I don’t have to worry about that part.

Specifically, I fix the positions of the digits in a pattern designed for close packing of 4 and 5 digit numbers, with occasional 2 and 3 digit numbers on the boundary, and add constraints saying that each number is a distinct prime. The current packing uses a large fraction of all 4 digit primes (854 of 1061).

## Code

```
#script (python)

import re

def primesto(n):
    p = [False]*2 + [True]*(n - 2)
    for k in xrange(n):
        if p[k]:
            yield k
            p[k*k::k] = [False]*(-(-n//k) - k)

def main(prg):
    width = prg.get_const('width')
    height = prg.get_const('height')

    primes = list(primesto(30000))
    counts = {4: 4, 5: 4}

    def get_bands(counts):
        if counts:
            for k, v in counts.iteritems():
                counts1 = counts.copy()
                if v == 1:
                    del counts1[k]
                else:
                    counts1[k] = v - 1
                for p in get_bands(counts1):
                    yield (k,) + p
        else:
            yield ()

    def get_img(bands, offset):
        tile = ''.join('#'*k + ' ' for k in bands)
        tile = tile[offset:] + tile[:offset]

        def get_edge(it, corner):
            if not next(it): return True
            if not next(it): return corner
            if not next(it): return True
            if not next(it): return True
            if not next(it): return True
            return False

        def get_cell(x, y):
            return \
                (x == 0 and get_edge((get_cell(i, y) for i in xrange(1, width - 1)), not 1 <= y < height - 1)) or \
                (y == 0 and get_edge((get_cell(x, j) for j in xrange(1, height - 1)), not 1 <= x < width - 1)) or \
                (x == width - 1 and get_edge((get_cell(i, y) for i in xrange(width - 2, 0, -1)), not 1 <= y < height - 1)) or \
                (y == height - 1 and get_edge((get_cell(x, j) for j in xrange(height - 2, 0, -1)), not 1 <= x < width - 1)) or \
                (1 <= x < width - 1 and 1 <= y < height - 1 and tile[(x + y)%len(tile)] == '#')

        img = [[get_cell(x, y) for x in xrange(width)] for y in xrange(height)]

        def get_spaces():
            for y, row in enumerate(img):
                for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in row)):
                    yield [(x, y) for x in range(m.start(), m.end())]
            for x, col in enumerate(zip(*img)):
                for m in re.finditer('#{2,}', ''.join(' #'[cell] for cell in col)):
                    yield [(x, y) for y in range(m.start(), m.end())]
        spaces = list(get_spaces())

        return img, spaces

    img, spaces = max((get_img(p, o) for p in get_bands(counts) for o in xrange(p[0] + 1)), key=lambda (img, spaces): len(spaces))

    def data():
        for p in primes:
            yield 'prime({}, {}).\n'.format(p, len(str(p)))
            for j, d in enumerate(str(p)):
                yield 'primedigit({}, {}, {}).\n'.format(p, j, int(d))
        for i, space in enumerate(spaces):
            yield 'space({}, {}).\n'.format(i, len(space))
            for j, c in enumerate(space):
                yield 'cell({}, {}, {}).\n'.format(i, j, c)

    def on_model(model):
        digit = {}
        for atom in model.atoms():
            if atom.name() == 'digit':
                c, d = atom.args()
                digit[c] = d
        for y in xrange(height):
            print ''.join(str(digit.get((x, y), ' ')) for x in xrange(width))

    prg.add('data', [], ''.join(data()))
    prg.ground([('base', []), ('data', [])])
    prg.solve_async(on_model=on_model).wait()

#end.

#program base.

1 {loc(P, S) : prime(P, L)} 1 :- space(S, L).
:- prime(P, L), 2 {loc(P, S) : space(S, L)}.
digit(C, D) :- loc(P, S), cell(S, J, C), primedigit(P, J, D).
:- cell(S, J, C), 2 {digit(C, D) : D=0..9}.

#show digit/2.

```

## Usage

```
clingo -q2 -c width=80 -c height=60 stuff.lp

```

Warning: at 80×60, this takes about 8 minutes and uses 3 GB of memory. You may want to start with smaller sizes if you just want to see it running.

## Output (80×60, 1740 primes):

```
13   293 4153 4217 9137   239 3169   223   127   281 9281 9173 4253   257 9239 3
719 28349 2039 7159 4603 17491 6299 17207 19463 19793 3301 4483 5657 20639 10061
 7 26417 7307 2687 9479 14767 3571 10957 21341 14057 5813 6689 2383 19553 2647 3
  15679 6679 6217 1303 19037 5821 11551 18797 28723 3457 6581 5309 12043 4441 2
 14327 4783 5881 6131 21107 2549 12953 24001 12113 7247 9973 8287 16573 5519 149
20177 4967 5647 1907 24439 7237 28979 24781 26083 9859 6691 3701 25237 9041 1637
2953 6781 2851 7079 10139 4001 20981 20341 23531 1913 5081 7639 25169 7457 29437
743 3907 6949 1237 10937 3203 19213 23971 18367 5227 5879 8191 17827 4561 16927
 9 1613 8369 3911 23327 1571 25741 15889 19417 3083 3049 4421 25111 8147 10391
9 9151 3019 3499 11839 5851 23669 16823 14891 9829 9403 1069 26099 8737 21911 2
17389 3217 7963 13337 5779 19841 16007 10313 7673 7703 5897 23911 4409 26737 131
2711 9697 8563 29611 2699 20231 27631 15823 7127 7937 8171 23623 6067 14489 1307
751 2179 7877 12263 7331 17923 26153 11443 7603 1429 8861 28871 8291 20599 20129
 3 3251 5087 28949 4519 22501 15667 28837 2543 3617 2837 15217 4129 20357 29879
2 7507 4127 27479 2521 11423 17497 12409 6599 7297 1249 14813 6301 10501 24151
21487 1777 20143 7109 12239 22063 23761 9931 1579 8929 26641 3221 18251 29429 1
3511 2389 27103 9767 27763 23399 18481 5869 8431 2141 15259 6367 29131 17789 137
941 6719 29269 1811 12829 14929 11953 4817 9787 6469 21377 2473 17033 11311 1453
 3 4549 11491 2777 11351 28879 21061 3607 5441 5861 15493 8941 15101 14437 28579
3 3229 18719 7841 18701 23087 23561 8597 6637 7487 19681 6229 25321 16417 14717
15131 13151 6997 27947 26669 24179 5981 4297 5683 18979 4937 16787 24421 20089
2531 13127 5507 10973 13121 25759 8353 9413 6343 17713 7211 21523 12143 10477 1
107 19381 6871 18803 20117 20849 9091 4051 2333 11399 5987 21617 13147 12281 251
 3 21713 4027 15299 15031 14389 7789 2617 4993 18493 1103 27983 22739 26987 2339
  22853 1297 15959 19531 12799 2269 4943 3917 10093 4657 25999 16603 11617 12919
 14923 1021 15313 11287 14969 1009 2503 9907 25247 4523 16411 12979 22397 8819 7
18191 5039 21019 13619 29947 2087 6793 3529 29671 2861 22259 20921 17291 6703 4
1523 4919 21379 23071 20051 9133 8761 6029 11813 7517 14503 28927 17707 8311 701
311 2939 28183 12347 10357 6143 5437 6011 13789 7573 23629 21773 28703 5059 8609
 7 4799 17333 28069 17921 9803 8543 9601 28151 9059 10457 12119 15661 3593 21937
1 7759 13399 19207 21407 3433 7187 5717 22571 7949 19489 12251 10271 8363 2879 3
13883 10663 20023 12583 1193 5011 1889 20147 4723 25447 15887 29921 4679 7459 4
2887 14341 14759 20593 2267 1567 9901 14411 1229 12041 20483 10889 6163 3517 947
983 24799 13099 13763 4733 9029 5281 17231 6947 29311 22709 11657 2113 8167 2081
 1 24359 29399 11117 2111 9341 9187 25931 7433 20347 16481 17293 5021 8681 17419
  21323 14737 18311 1559 1279 2273 20509 7283 21739 28549 22193 8011 9887 2293 3
 21011 19231 16127 6197 5107 7151 21013 2551 22943 28057 13229 9277 7229 1721 1
19927 22549 20641 8053 6529 5737 29021 1483 23027 24977 17623 3347 6791 1213 157
8573 23447 24499 4273 8963 6257 21881 3343 27329 17627 28307 2393 4493 3613 1117
167 15991 17029 2803 6673 3797 28219 4621 22277 12071 13043 6661 6323 8297 19433
 7 24517 11777 1609 9941 2437 26189 8831 10771 11467 24091 2371 3331 7607 14947
  22727 13967 1471 7451 1367 11113 7477 11527 24007 23557 1277 4969 6829 28097
 12073 11887 3181 8999 5641 13411 7321 22901 28001 20939 3889 5231 8863 26357 3
20011 23671 5669 1291 9421 12689 4903 21647 19483 12157 4861 5381 9067 11471 283
2663 22123 2833 1663 8677 19891 8039 19141 23321 17417 2027 9949 6389 23459 7351
967 15901 1831 1013 5527 12697 5573 16547 14029 23333 3623 9007 6047 12739 18539
 7 14369 9719 6199 7499 20663 3413 23339 25097 12973 9791 9643 2459 18353 9371 1
  16843 8329 1451 5279 13441 9433 27647 28643 12007 4463 6269 7523 22861 6899 1
 18143 5743 2591 4211 27551 9461 27689 11279 14533 2731 3001 6857 13757 3259 277
22483 3943 9011 8933 18593 6569 26891 22381 22109 4721 1733 2377 11549 5387 1459
6203 5351 9851 6133 29573 4513 28517 14083 15773 3109 9967 5113 29063 1427 17257
911 8713 8783 9613 27253 3851 25537 16993 17683 3037 4007 5651 12487 6779 26251
 1 7243 3023 4751 10459 4931 25639 25867 12101 6373 4201 8123 15227 7589 10601
1 9631 1061 1549 10159 1381 18047 26687 20411 2129 2011 1583 12899 9749 26339 1
25127 3389 4013 15647 1669 25933 24443 21557 7853 3449 6089 28771 1847 21491 263
8501 2309 2797 16189 2003 22637 15973 28697 2741 2729 1361 25667 2131 23633 2297
359 9551 6553 11597 6451 20123 10259 29287 5923 8233 1423 10133 1373 23773 11633
 7 5119 7829 24019 7691 12479 14143 20873 5323 1493 3709 18947 5701 12413 27997
4 3697 5261 17359 1657 26371 20443 11239 4093 7013 5303 15767 4259 13499 13597 5
31 3919 7393 199   9397 379   971   193   3319 1979 1931 173   3391 937   397 19

```
> 11 votes

# Answer
## Smalltalk, 1098 primes

First off, nice hard question - as evidenced by the lack of non-trivial responses thus far.

I had a solution cooking at work, but had to wait for the long weekend to have time to clean it up a bit. Even so, it's quite "heavy", so excuse the length of this answer - thankfully this isn't a code-golf question. There were plenty of little gotchas along the way, but I'm pretty sure it's bug-free now, though there is plenty of room for improvement and fine-tuning.

## Language

My language of choice for this sort of thing is **Smalltalk** \- the superior debugging and the ability to test-as-you-code beats anything else I know. Plus, even non-coders can read it and understand the basics of what's going on. I've limited the code below to the interesting part, because I thought the approach and results would be more interesting than the boilerplate code, but a file-out of the full code is pasted here if anyone wants to try it out (just save as a `*.st` and file it in from a file browser in Pharo). I used Pharo 4.0, which is FOSS and can be downloaded from pharo.org for Win/Mac/Linux. Also happy to paste the full code here, but I took the "should include the output text and your code" as a suggestion - let me know if I'm wrong.

## Approach

So, my thinking was, what if you could start in the middle, stuff as many long primes as you could in the box, and work your way outwards from there? Let's start with a `Box` object, containing a `matrix` of characters, and then use a `Stuffer` object to try to fill it up by finding the best `Insertion` (another object) for each point in the matrix and for each prime (starting with the long ones). Pharo has nice `Matrix` and `Point` classes with a bunch of useful methods (though the matrix way of using row-major notation versus using x-y-coordinates of points can get a little confusing if you're not careful).

The basic steps of my algorithm are:

1. Create a `Stuffer` instance for given dimensions.
2. Have the stuffer initialise its box to size 5x5 (unless dimensions are smaller).
3. Ask the Stuffer to stuff its box.
    * Iterate over a list of primes from largest to smallest.
    * Iterate over all points in the matrix.
    * Find the best spot to insert the prime, and insert if possible.
4. While the box can "grow" (until the target dimensions are reached), increase it by one row/column on each side and copy the old contents into the middle of the bigger one, then go to step 3.

Configurable:

* The dimensions
* The largest prime to start from
* The "rating" of insertions (when choosing from multiple possible places to insert a prime)

For the matrix below, I used 80x60, primes below 20,000 (since the combined length of those is just under 10,000 characters, i.e. the maximum for a stuffed, though invalid, 100x100 box), and, after some experimentation, the following rating for `each` insertion:

```
(each accidentalPrimes size * 1) + (each numberOfOverlaps * 2)

```

where `accidentalPrimes` are the perpendicular primes "accidentally" created when inserting between neighbouring primes, and `numberOfOverlaps` is how many characters are overwritten (e.g. when adding a `3` at the end of `11` to insert `113`). I initially had it weighted more towards `accidentalPrimes`, but got a few more primes in the box by doing it this way - not entirely sure why (feel free to test/tweak).

## Result

```
| 1  3449 8    19489 8 7 9277 2 7 419  6   9533   19211    19373 4  79379 9 6 1 1|
|8861 1 7 26701 6   19219 6  97577 6 319211  84631   3 9649  8  19207 6 7 10399 9|
| 7 8 98669    19469 5 0  19301 2 6997 4   9787   19441  7 18899 9  2 47297 1 0 0|
|8839 8 7  1613 5 6  149837  0 97213 319289  6 8291  915991 431  909113 1 3382373|
| 7 7607 1 9 9 19543  1 8  3779   5743  6 19813  1946981    19423  139 9 9431 1 1|
|  6911 8933 2 9  9 1 4198013  9497   19447    1093 9 1194899 8  569 4 487749433 |
|  1 1 1 3 0 5653 19583 5  101429 4691  4 119813 9923  9 5 9  1811 47293 3533 1 1|
| 8849 8 86959 0 1  0 4 195071 7589 19429 5 1  4    3863 219763  9 2 1 9391 87833|
|  9 813613 7  719 19471   6 19727 809 7 19759 619867 6 988069 1 5 11 191 9 1 9 8|
|541 0 7 3547 1  70919 93715816451 19609 3  6  3 41  757219717 9803 9 9 9283  4 7|
|  3 1 9  1  691 6 4 177493124562997 7 1 719483  152993 8 9 6173 7 4871 0 7 15797|
|5171 373 9 1 4  318191 263 19843  19919  8 19889953 4  169639 9679 6 8887  9    |
| 8 8 1  5449 2 9 9399647333  7907  8 8 677   2 6 6 9967 101 8111 7 7 3 7 1907 1 |
| 7 67901 2 2 757 8 5 9 119759 7499 19853 19751 19913  59879 0  6469 171179 6 487|
|8747 0 9 78167 4 8 78839  6 99317  1 3 9 9 9   9  16993 7 8117 3373 9 9  8093 9 |
| 3 913457  31193 9 107 7 198439   9 19861 19697  99125471 1   16979 2819 0  9 1 |
|9 1951 0 1879 1 7 1 1 6569 853 18397  83 8 656269159547193329 97871 7 2  1 4337 |
|14831 9157 6449 0 9 9613 8 15619 148436196977821 19597 9 9 6907 8 383 16573 1  1|
|7 9 8   4518803 15647 9 941 19661 19963 149 19937  43913 2393 1433   1 1 1 9 9 8|
|3 1 3613719596827 8 2 6 5163998629 467 6271    13967 29399 51673 9 15973 87473 9|
| 9127 9  927191 19157 0 8 66938969 176597 19949 5779513 1319 6 92779 14717 3 7 1|
| 1  98849 8  9  9  8  3371771169929  169937 4 65519 79769 9 906473 58699 9 78173|
| 8    0 6 9907 682771709 9  9 3319763 947387669 479978309 8597 35393 369731 5   |
| 6211 1823 1 318691 96918839 9 19 918481 1997911733 58271 6 2 9 1  1  92219 661 |
| 7 8 7349  9 369739 7297 1 8641 4 12983    6 61991 619793 7 3 8297  15791 0 3   |
| 9 8 1 1 1 5  619 8 510589 396932999 7 5897537 997379 6   39313 4 439 69911   1 |
|1  64969 9 54139 7069 739 99639719 58693 4951 977   9 9151 8 9 6689 257 4 37199 |
|8  9 0 37529 9 4 91193  6 6 9 7  6 8 11117 3970331  11969963 661911661 727  9 3 |
|3613 0 8 5   3 2 8 9 1 499787 9137 71 19997 971939 13999 787 0 9 9  701 7 151 8 |
|7 8 6961 97607 1 15569 9 8 1 1 1171 99929 99733 1759 9817931 3 8941  193  968819|
|9 8 0 6    0 99767 8 769 88969 9859 979871393 1733 9377113 95111 5  8 2 94033  1|
| 9041 6 14519   1063 6 937 8 8 8999 92993171 11519 313 89417 7 317 5233 4 3  9 8|
|433  8179 6113 8971  3191 7559 17 63313 939793919 971 198769 5693 9 7 14657  6 7|
| 9  1  725189 1 7591139 9 93911 3919 9257 33739 9293 29 1973 2  9161 9511  17239|
|349 87151 99349 57529 7 36637 6359 81999119371933 4 98573 7549 6317 6619 8 9 3 3|
| 3  8 99371 8 8 374718269 1144935789717977  83177 9 233  1499 8 1571 4 8 6803 9 |
| 9 15641  3 1607  3 8 761 393388937 9    3967  9 9719 13997 1259 7 9639079 0 443|
|  1 9 2 14969 1109 8313931 8  198174119963 5 997 9 9 991 8 1217 52579  1 3911 1 |
|5281 277    1979 32909 9 986983 19991  61399991 127997 9 82939 907 571847 4 9 3 |
|  7 8  9 617 8 4 21613 2 95792383  919979 4951997317 93719 4 641379379 1 12487  |
|1 9 66959 8  8 4 3339313613 5119963  189967 359 337361 25933 83891   8 8191 7  1|
|8 7 8 61516883 1619 839 799859 2 7 19927 5 19777 6737 9769 37799 9 19079 0 9 1 9|
|57331 2 4 9 1 1  7691 9697 61 19577 6 9949 9 9 31979399 1 1 8737 509 1 49871 8 8|
|9  1 6397 109 99133 3 313888397 59419699  1986133  8387 9 988319 5 47237 7 8 9 0|
|3  62131 1  38629 8699 9 8599 953 397   736903  1991933 877 3 4799 1 1     77171|
| 1 8 8 9091 7 86491  627673 593 1  19853377 9781  8  3 76919  7  49739 1511  9  |
| 8 76991 0 938747 9 924629743 193873 2 699823 8719801 907871 19181   4 9 9 93371|
|6959 7 88867 6 7757 792383  8 9  0  1981337 3947  3 3917 2089979 9 92831 0 4 2 8|
| 4  5953 1 3739   69473 7 19709 99016787  379 9619759 196991 6 56393 9 3 82373 9|
|273613  1163  13883 919531 8  7433 6337 8893 6917   9 9 1693 8 0 7 709 93199 9 1|
|631 97159 8  181 9 6   0 0 7819753 673  9    9  9901 19699   18119 7  1 1 111779|
|9 8 0 9 0 2 7919 4419559 98179  6  9  19553 9851 4 9 9067 9857 4957 14939 9397  |
| 8753 0 3631 4 4 18973 9 7  89923 19793 1 8  3491954199497 53 2969  9 0 0 0 0 6 |
|1 9 1 5 1    5 24337 9  19603 7 9 3  4 195312373619977 9 119489 8 96293 643 0 4 |
|8 3   18461  7577 1 19541 8 3929 19463 6  3 49937 3 9 98297 71347 3 4 7 9 1 9 1 |
|87541   9 92707 9817  2 58237 1 4 6  919423 5    19403 77383 194179 9601 1 1 1 1|
|6 9 38923 4 261631 61937389 19387 390739 9 19319 6 495617 9 5  9  7  93979 8 8 8|
|9 449 5 4658147899073 7  3   60293 5  7 8231 2641930937 7 1950139511 6 0 26993 7|
|  0 0 2 34789739 1 3619501   61930959767 7 19237 7 9 9829   7    7   27773 5 7 5|
|823 7211 7 3 3 919231     9631 337 1  3    7 9 3      1 3943 19141 653 3 15971 7|

```

This matrix contains 1098 primes, which is a "load" of about 70%.

These are the primes (`box primesUsed asSortedCollection printString`):

```
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 107 109 113 127 131 137 139 149 151 163 167 173 179 181 191 193 197 199 233 257 263 269 271 277 283 293 307 311 313 317 331 337 347 349 359 367 373 379 383 389 397 419 431 433 439 443 449 467 479 487 491 499 509 541 547 557 563 569 571 577 587 593 599 601 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1063 1093 1109 1117 1153 1163 1171 1181 1217 1259 1319 1361 1399 1433 1499 1511 1571 1607 1613 1619 1637 1663 1667 1693 1697 1699 1733 1759 1811 1823 1879 1907 1913 1933 1949 1951 1973 1979 1987 1993 1997 1999 2393 2459 2617 2819 2927 2939 2969 3191 3229 3343 3373 3389 3449 3533 3539 3547 3613 3617 3631 3643 3733 3739 3769 3779 3793 3797 3821 3863 3911 3917 3919 3929 3931 3943 3947 3967 3989 4337 4409 4493 4691 4799 4871 4951 4957 4967 4999 5039 5099 5101 5171 5233 5281 5347 5399 5449 5479 5653 5693 5743 5939 5953 6079 6113 6131 6173 6197 6199 6211 6271 6317 6337 6359 6397 6449 6469 6569 6571 6599 6607 6619 6653 6661 6689 6737 6779 6781 6793 6803 6823 6829 6899 6907 6911 6917 6949 6959 6961 6967 6977 6997 7069 7079 7109 7193 7211 7219 7229 7297 7307 7349 7393 7433 7499 7549 7559 7577 7589 7607 7621 7691 7727 7757 7883 7907 7919 7949 7951 8093 8111 8117 8171 8179 8191 8219 8231 8291 8293 8297 8387 8419 8537 8539 8563 8597 8599 8641 8647 8681 8689 8693 8699 8719 8737 8747 8753 8779 8831 8839 8849 8861 8863 8887 8893 8929 8933 8941 8951 8963 8969 8971 8999 9007 9041 9067 9091 9109 9127 9137 9151 9157 9161 9173 9181 9187 9199 9257 9277 9281 9283 9293 9371 9377 9391 9397 9413 9419 9421 9431 9437 9461 9463 9467 9473 9479 9491 9497 9511 9521 9533 9539 9547 9551 9587 9601 9613 9619 9623 9629 9631 9643 9649 9661 9677 9679 9689 9697 9719 9721 9733 9739 9743 9749 9767 9769 9781 9787 9791 9803 9811 9817 9829 9833 9839 9851 9857 9859 9871 9883 9887 9901 9907 9923 9929 9931 9941 9949 9967 9973 10399 10979 11117 11519 11939 11959 11981 12487 12983 13313 13339 13463 13499 13763 13877 13883 13907 13931 13963 13967 13997 13999 14519 14657 14717 14831 14939 14969 15091 15569 15619 15641 15647 15791 15797 15971 15973 16573 16699 16937 16979 16993 17239 17333 17519 17761 17891 18119 18371 18379 18397 18461 18593 18691 18719 18757 18773 18787 18803 18839 18859 18869 18899 18911 18913 18917 18919 18959 18973 18979 19001 19009 19013 19031 19037 19051 19069 19073 19079 19081 19087 19121 19139 19141 19157 19163 19181 19183 19207 19211 19213 19219 19231 19237 19249 19259 19267 19273 19289 19301 19309 19319 19333 19373 19379 19381 19387 19391 19403 19417 19421 19423 19427 19429 19433 19441 19447 19457 19463 19469 19471 19477 19483 19489 19501 19507 19531 19541 19543 19553 19559 19571 19577 19583 19597 19603 19609 19661 19681 19687 19697 19699 19709 19717 19727 19739 19751 19753 19759 19763 19777 19793 19801 19813 19819 19841 19843 19853 19861 19867 19889 19891 19913 19919 19927 19937 19949 19961 19963 19973 19979 19991 19993 19997 21613 24239 24337 25933 26449 26701 26959 26993 27773 27919 29399 31193 32909 33739 35393 36637 36761 37199 37529 37799 38333 38629 38839 38861 38923 39293 39313 39733 43913 43943 45281 47237 47293 47297 47933 47939 48311 48761 49123 49199 49739 49871 49937 49999 51193 51673 52379 52579 54139 55313 56393 56983 57173 57331 57529 58237 58271 58693 58699 59743 59879 60293 61979 61991 62131 62873 63313 64969 65519 66491 66959 67339 67901 67993 69337 69473 69623 69697 69911 69941 70919 71347 71777 71933 75347 76163 76733 76919 76991 77171 77213 77383 77591 78167 78173 78839 78941 79379 79613 79631 79769 79967 80933 81919 82373 82939 83177 83891 84631 86491 86959 87151 87473 87541 87833 88499 88867 88951 88969 89413 89417 89519 89923 91193 91813 91997 92219 92593 92647 92707 92779 92831 93199 93371 93719 93911 93913 93971 93979 94033 94397 94399 95111 96233 96293 96587 96911 96959 96979 97159 97213 97577 97607 97787 97871 97919 98179 98297 98299 98573 98669 98837 98849 99133 99233 99317 99349 99371 99497 99733 99767 99907 99929 101429 111149 111779 119489 119759 119813 127997 149837 152993 167747 168863 169639 169937 171179 176597 179099 179111 183797 187973 188693 189473 189967 190811 191837 193813 193873 194179 195071 195319 196817 196871 196991 197273 197599 198013 198017 198439 198673 198769 199373 199679 199739 219763 261631 273613 277793 316879 318191 318691 319031 319069 319211 319289 319427 319727 319973 337361 369731 369739 390739 391753 414347 419801 439883 495617 499787 510589 519031 571847 601189 615749 616367 618719 619543 619583 619753 619763 619793 619813 619867 627673 639631 669923 698339 699823 716987 719009 719441 719483 719801 719813 725189 736903 792383 796553 799859 809339 813613 815569 819373 819457 829733 906473 907871 909113 912797 913457 914813 915641 915991 917159 918481 918679 918793 919183 919231 919381 919421 919423 919531 919559 919913 919979 927191 935639 938747 939439 943913 962971 968819 971939 976777 986983 988069 988319 991733 997319 997379 1194899 1518379 1678739 1691189 1867993 1899907 1903703 1923151 1942771 1946981 1950149 1981337 1986133 1988999 1991933 2089979 2919031 3110651 3190871 3193913 3197633 3319763 3382373 3619501 3970331 4198013 4419559 4518803 5119963 5195077 5319289 5619139 5779513 5897537 6119801 6129587 6189137 6194891 6198139 6649871 7219427 7591139 7819753 7937939 7982969 8019727 8313931 8419381 8719801 8783371 9119909 9189797 9323173 9377113 9619759 9639079 9679799 9717317 9721301 9721991 9817931 9911789 9923369 9967759 11969963 15619867 16981331 19669931 19717099 19793479 19853377 19889953 21968773 27519469 31979399 34789739 40699733 59419699 61399991 61516883 61897937 61937389 63519427 66938969 69719801 74969767 91938191 92993171 93369931 95792383 96918839 99016787 99125471 99639719 126919421 179319979 199733981 199739717 313888397 342719753 374718269 393388937 396932999 439314797 479978309 487749433 577519913 613799299 631819381 641379379 661911661 682771709 689194217 757219717 924629743 936195479 939793919 947387669 979871393 991579637 1903149343 1921376857 1945706473 1950139511 1960317529 1997911733 2641930937 3339313613 4951997317 4952523041 5163998629 7017198679 7981707577 9013119991 9119993297 9399647333 9945519919 18919323977 19031318947 19919788793 31879359403 61930959767 61960309907 91734751987 92774649739 93715816451 95479219889 98371979359 99551395387 191830198139 198012194899 198017187931 198174119963 213963819763 419801418947 819853319867 1937327699987 3189735598139 3371771169929 3491954199497 3613719596827 4658147899073 81999119371933 148436196977821 177493124562997 195312373619977 1144935789717977 656269159547193329 735547997331319937 11572771955941937959

```

As you can see, some of the "accidental" primes get quite long... 20 digits for the longest one. :-)

## Details

To understand how it works, here is the output from doing the first few steps - inserting `19997` first (if you look in the middle of the big matrix, you'll see `19997` there as well), then down the list where there's space in the 5x5 box (`>` means insert going right, `v` means going down; anything in `{}` are accidentally added primes:

```
Insertion at: (1@1) > {} of 19997
Box 5x5 (1 prime, load: 20%):
|19997|
|     |
|     |
|     |
|     |

Insertion at: (1@1) v {} of 19993
Box 5x5 (2 primes, load: 36%):
|19997|
|9    |
|9    |
|9    |
|3    |

Insertion at: (3@1) v {} of 9973
Box 5x5 (3 primes, load: 48%):
|19997|
|9 9  |
|9 7  |
|9 3  |
|3    |

Insertion at: (1@4) > {} of 9931
Box 5x5 (4 primes, load: 56%):
|19997|
|9 9  |
|9 7  |
|9931 |
|3    |

Insertion at: (1@3) > {89, 11} of 9871
Box 5x5 (7 primes, load: 64%):
|19997|
|9 9  |
|9871 |
|9931 |
|3    |

Insertion at: (5@1) v {98713, 99317} of 7937
Box 5x5 (10 primes, load: 76%):
|19997|
|9 9 9|
|98713|
|99317|
|3    |

Insertion at: (4@3) v {} of 113
Box 5x5 (11 primes, load: 80%):
|19997|
|9 9 9|
|98713|
|99317|
|3  3 |

11 old primes: 19997, 19993, 9973, 9931, 9871, 89, 11, 7937, 98713, 99317, 113
8 new primes: 19997, 98713, 99317, 19993, 89, 9973, 113, 7937
Insertion at: (4@1) v {} of 19973
Box 7x7 (9 primes, load: 42%):
|   1   |
| 19997 |
| 9 9 9 |
| 98713 |
| 99317 |
| 3  3  |
|       |

```

In the insertion immediately above, the box becomes a 7x7, and is filled with the 5x5 contents before the next insertion happens. I also re-calculate the primes used at this point, because some are "freed up" due to overwriting (`11` was inserted, but then overwritten by `113`).

## Code

Here are the most relevant parts of the code:

**Stuffer class**

*Class side*

```
forDimensions: aPoint
    ^self new
        width: aPoint x;
        height: aPoint y;
        initializeBox;
        yourself

```

*Instance side*

```
initializeBox
    box := Box width: (width min: 5) height: (height min: 5)

stuffBox
    self stuffCurrentBox.
    [self canGrow] whileTrue: [
        box growMatrixByOneUpTo: width @ height.
        self stuffCurrentBox
        ]

canGrow
    ^width > box width or: [height > box height]

stuffCurrentBox
    self class primes reverseDo: [:eachPrime |
        | string insertion |
        (box primesUsed includes: eachPrime) ifFalse: [
            string := eachPrime asString.
            (insertion := box bestInsertionFor: string) ifNotNil: [
                Transcript crShow: insertion printString, ' of ', string.
                box doInsertion: insertion of: eachPrime.
                Transcript crShow: box
                ]
            ]
        ].
    box updatePrimesUsed.

```

**Box class**

*Instance side*

```
bestInsertionFor: aString
    | insertions |
    insertions := OrderedCollection new.
    self pointsDo: [:eachPoint |
        insertions
            addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: true);
            addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: false)
        ].
    insertions removeAllSuchThat: [:each |
        self isInsertion: each invalidWith: aString asInteger
        ].
    ^insertions detectMax: [:each |
        (each accidentalPrimes size * 1) + (each numberOfOverlaps * 2)
        ]

insertionOf: aString at: aPoint isHorizontal: isHorizontal
    | point insertion |
    point := aPoint.
    (self canFitEndsAt: point length: aString size isHorizontal: isHorizontal)
        ifFalse: [^nil].
    (self isAlreadyThere: aString at: aPoint isHorizontal: isHorizontal)
        ifTrue: [^nil].
    insertion := Insertion at: point isHorizontal: isHorizontal.
    1 to: aString size do: [:eachIndex |
        | eachCharacter |
        eachCharacter := aString at: eachIndex.
        (self canDoInsertion: insertion of: eachCharacter at: point)
            ifTrue: [
                (self at: point) == eachCharacter
                    ifTrue: [insertion increaseOverlaps].
                point := point + (self offset: isHorizontal)
                ]
            ifFalse: [^nil]
        ].
    ^insertion

isInsertion: anInsertion invalidWith: aPrime
    | otherPrimes |
    otherPrimes := anInsertion accidentalPrimes.
    ^(primesUsed includesAny: otherPrimes)
        or: [(otherPrimes includes: aPrime)
        or: [otherPrimes copy removeDuplicates size < otherPrimes size]]

canDoInsertion: anInsertion of: aCharacter at: aPoint
    "Answer whether anInsertion of aCharacter is valid at aPoint. If so,
    also update anInsertion with any accidental insertions."

    | perpendicularSequence offset front back |
    (self at: aPoint) == aCharacter
        ifTrue: [^true].
    (self isFreeAt: aPoint)
        ifFalse: [^false].
    perpendicularSequence := OrderedCollection with: aCharacter.
    offset := self offset: anInsertion isHorizontal not.
    front := aPoint - offset.
    back := aPoint + offset.
    [self isFreeOrBorder: front] whileFalse: [
        perpendicularSequence addFirst: (self at: front).
        front := front - offset
        ].
    [self isFreeOrBorder: back] whileFalse: [
        perpendicularSequence addLast: (self at: back).
        back := back + offset
        ].
    ^perpendicularSequence size == 1
        or: [
            | string number |
            string := String withAll: perpendicularSequence.
            number := string asInteger.
            (string first ~~ $0 and: [number isPrime])
                ifTrue: [anInsertion addAccidentalPrime: number. true]
                ifFalse: [false]
            ]

doInsertion: anInsertion of: aPrime
    self
        insert: aPrime printString
        at: anInsertion point
        horizontalIf: anInsertion isHorizontal.
    primesUsed
        add: aPrime;
        addAll: anInsertion accidentalPrimes

growMatrixByOneUpTo: aDimension
    | smallMatrix rowOffset columnOffset |
    smallMatrix := matrix copy.
    matrix := Matrix
        rows: (self height + 2 min: aDimension y)
        columns: (self width + 2 min: aDimension x)
        element: Character space.
    rowOffset := (self height - smallMatrix numberOfRows) min: 1.
    columnOffset := (self width - smallMatrix numberOfColumns) min: 1.
    self copy: smallMatrix to: (1 + columnOffset) @ (1 + rowOffset)

copy: aMatrix to: aPoint
    matrix
        atRows: aPoint y
        to: aPoint y + aMatrix numberOfRows - 1
        columns: aPoint x
        to: aPoint x + aMatrix numberOfColumns - 1
        put: aMatrix

```

**Collection**

Oh, and I added one method to the instance side of `Collection`, just for convenience:

```
addIfNotNil: anElement
    anElement ifNotNil: [self add: anElement]

```

## Running it

To run my code, all I need to do is select the following code in a workspace ("playground" in Pharo) or in any text pane, and "do it" from the context menu:

```
(Stuffer forDimensions: 80@60)
    stuffBox

```

The rest of the code is relatively boring.

As I said, room for improvement, but at 80x60, each run takes over 3 minutes on my machine. Clearly, performance hasn't been my concern, but there are a LOT of primes flying around. This sure has been an interesting challenge. :-)
> 19 votes

# Answer
# Javascript, score 659

Almost certainly suboptimal.

```
lenof=function(a){return (""+a).length}
Array.prototype.peek=function(){return this[this.length-1]}
primes=[
11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157,
163, 167, 173, 179, 181, 191,
193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331,
337, 347, 349,401, 409, 419,
421, 431, 433, 439, 443, 449,
457, 461, 463, 467, 479, 487,
491, 499, 503, 509, 521, 523,
541, 547, 557, 563, 569, 571,
577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643,
647, 653, 659, 661, 673, 677,
683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761,
769, 773, 787, 797, 809, 811,
821, 823, 827, 829, 839, 853,
857, 859, 863, 877, 881, 883,
887, 907, 911, 919, 929, 937,
941, 947, 953, 967, 971, 977,
983, 991, 997,1009, 1013, 1019,
1021, 1031, 1033, 1039, 1049,
1051, 1061, 1063, 1069, 1087,
1091, 1093, 1097, 1103, 1109,
1117, 1123, 1129, 1151, 1153,
1163, 1171, 1181, 1187, 1193,
1201, 1213, 1217, 1223, 1229,
1231, 1237, 1249, 1259, 1277,
1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321,
1327, 1361, 1367, 1373, 1381,
1399, 1409, 1423, 1427, 1429,
1433, 1439, 1447, 1451, 1453,
1459, 1471, 1481, 1483, 1487,
1489, 1493, 1499, 1511, 1523,
1531, 1543, 1549, 1553, 1559,
1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619,
1621, 1627, 1637, 1657, 1663,
1667, 1669, 1693, 1697, 1699,
1709, 1721, 1723, 1733, 1741,
1747, 1753, 1759, 1777, 1783,
1787, 1789, 1801, 1811, 1823,
1831, 1847, 1861, 1867, 1871,
1873, 1877, 1879, 1889, 1901,
1907, 1913, 1931, 1933, 1949,
1951, 1973, 1979, 1987, 1993,
1997, 1999, 2003, 2011, 2017,
2027, 2029, 2039, 2053, 2063,
2069, 2081, 2083, 2087, 2089,
2099, 2111, 2113, 2129, 2131,
2137, 2141, 2143, 2153, 2161,
2179, 2203, 2207, 2213, 2221,
2237, 2239, 2243, 2251, 2267,
2269, 2273, 2281, 2287, 2293,
2297, 2309, 2311, 2333, 2339,
2341, 2347, 2351, 2357, 2371,
2377, 2381, 2383, 2389, 2393,
2399, 2411, 2417, 2423, 2437,
2441, 2447, 2459, 2467, 2473,
2477, 2503, 2521, 2531, 2539,
2543, 2549, 2551, 2557, 2579,
2591, 2593, 2609, 2617, 2621,
2633, 2647, 2657, 2659, 2663,
2671, 2677, 2683, 2687, 2689,
2693, 2699, 2707, 2711, 2713,
2719, 2729, 2731, 2741, 2749,
2753, 2767, 2777, 2789, 2791,
2797, 2801, 2803, 2819, 2833,
2837, 2843, 2851, 2857, 2861,
2879, 2887, 2897, 2903, 2909,
2917, 2927, 2939, 2953, 2957,
2963, 2969, 2971, 2999, 3001,
3011, 3019, 3023, 3037, 3041,
3049, 3061, 3067, 3079, 3083,
3089, 3109, 3119, 3121, 3137,
3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973
];
width=+prompt`Width`;
height=+prompt`Height`;
pre=document.createElement("pre");
lis=[];
q=0;
z:while(lis.length<height){
s=lis.length%2?"":"    ";
while(true){
v=primes[q++];
if((s+("   "+v).slice(-4)).length>width)break;
s=s+("   "+v).slice(-4);
if(q>=primes.length){lis.push(s);break z;}
if(s.length<width)s+="    ";
}
lis.push((s+" ".repeat(width)).substring(0,width));
}
document.body.appendChild(pre);
for(z of lis){
pre.innerHTML+=z+"\n";

}

```

Results for 30x30:

```
      11      13      17
  23      29      31      37
      43      47      53
  61      67      71      73
      83      89      97
 103     107     109     113
     131     137     139
 151     157     163     167
     179     181     191
 197     199     211     223
     229     233     239
 251     257     263     269
     277     281     283
 307     311     313     317
     337     347     349
 409     419     421     431
     439     443     449
 461     463     467     479
     491     499     503
 521     523     541     547
     563     569     571
 587     593     599     601
     613     617     619
 641     643     647     653
     661     673     677
 691     701     709     719
     733     739     743
 757     761     769     773
     797     809     811
 823     827     829     839

```

And for the 60x60 case:

```
      11      13      17      19      23      29      31      37      41      43
  53      59      61      67      71      73      79      83      89      97
     103     107     109     113     127     131     137     139     149     151
 163     167     173     179     181     191     193     197     199     211
     227     229     233     239     241     251     257     263     269     271
 281     283     293     307     311     313     317     331     337     347
     401     409     419     421     431     433     439     443     449     457
 463     467     479     487     491     499     503     509     521     523
     547     557     563     569     571     577     587     593     599     601
 613     617     619     631     641     643     647     653     659     661
     677     683     691     701     709     719     727     733     739     743
 757     761     769     773     787     797     809     811     821     823
     829     839     853     857     859     863     877     881     883     887
 911     919     929     937     941     947     953     967     971     977
     991     997    1009    1013    1019    1021    1031    1033    1039    1049
1061    1063    1069    1087    1091    1093    1097    1103    1109    1117
    1129    1151    1153    1163    1171    1181    1187    1193    1201    1213
1223    1229    1231    1237    1249    1259    1277    1279    1283    1289
    1297    1301    1303    1307    1319    1321    1327    1361    1367    1373
1399    1409    1423    1427    1429    1433    1439    1447    1451    1453
    1471    1481    1483    1487    1489    1493    1499    1511    1523    1531
1549    1553    1559    1567    1571    1579    1583    1597    1601    1607
    1613    1619    1621    1627    1637    1657    1663    1667    1669    1693
1699    1709    1721    1723    1733    1741    1747    1753    1759    1777
    1787    1789    1801    1811    1823    1831    1847    1861    1867    1871
1877    1879    1889    1901    1907    1913    1931    1933    1949    1951
    1979    1987    1993    1997    1999    2003    2011    2017    2027    2029
2053    2063    2069    2081    2083    2087    2089    2099    2111    2113
    2131    2137    2141    2143    2153    2161    2179    2203    2207    2213
2237    2239    2243    2251    2267    2269    2273    2281    2287    2293
    2309    2311    2333    2339    2341    2347    2351    2357    2371    2377
2383    2389    2393    2399    2411    2417    2423    2437    2441    2447
    2467    2473    2477    2503    2521    2531    2539    2543    2549    2551
2579    2591    2593    2609    2617    2621    2633    2647    2657    2659
    2671    2677    2683    2687    2689    2693    2699    2707    2711    2713
2729    2731    2741    2749    2753    2767    2777    2789    2791    2797
    2803    2819    2833    2837    2843    2851    2857    2861    2879    2887
2903    2909    2917    2927    2939    2953    2957    2963    2969    2971
    3001    3011    3019    3023    3037    3041    3049    3061    3067    3079
3089    3109    3119    3121    3137    3163    3167    3169    3181    3187
    3203    3209    3217    3221    3229    3251    3253    3257    3259    3271
3301    3307    3313    3319    3323    3329    3331    3343    3347    3359
    3371    3373    3389    3391    3407    3413    3433    3449    3457    3461
3467    3469    3491    3499    3511    3517    3527    3529    3533    3539
    3547    3557    3559    3571    3581    3583    3593    3607    3613    3617
3631    3637    3643    3659    3671    3673    3677    3691    3697    3701
    3719    3727    3733    3739    3761    3767    3769    3779    3793    3797
3821    3823    3833    3847    3851    3853    3863    3877    3881    3889
    3911    3917    3919    3923    3929    3931    3943    3947    3967    3989
4003    4007    4013    4019    4021    4027    4049    4051    4057    4073
    4091    4093    4099    4111    4127    4129    4133    4139    4153    4157
4177    4201    4211    4217    4219    4229    4231    4241    4243    4253
    4261    4271    4273    4283    4289    4297    4327    4337    4339    4349
4363    4373    4391    4397    4409    4421    4423    4441    4447    4451
    4463    4481    4483    4493    4507    4513    4517    4519    4523    4547
4561    4567    4583    4591    4597    4603    4621    4637    4639    4643
    4651    4657    4663    4673    4679    4691    4703    4721    4723    4729
4751    4759    4783    4787    4789    4793    4799    4801    4813    4817
    4861    4871    4877    4889    4903    4909    4919    4931    4933    4937
4951    4957    4967    4969    4973    4987    4993    4999    5003    5009

```
> 1 votes

---
Tags: code-challenge, combinatorics, grid, primes
---


title: Count rectangle intersections

text:
# Question
Title: Count rectangle intersections
# The Challenge

Given an arbitrary amount of rectangles, output the total count of intersections of those when drawn in a 2D plane.

An intersection here is defined as a point `P` which is crossed by two lines which are orthogonal to each other and are both not ending in `P`.

### Example

Each rectangle here is denoted by a 2-tuple with the coordinates of the upper left corner first and the coordinates of the bottom right corner second.

```
[(-8,6),(-4,-2)]
[(-4,9),(4,3)]
[(2,10),(14,4)]
[(1,7),(10,-6)]
[(7,4),(10,2)]
[(5,2),(9,-4)]
[(-6,-4),(-2,-6)]

```

Those rectangles create 6 intersections, which has to be your output.

* As you can see in the image above, touching rectangles will not create intersections here and are not counted.
* You can encode the rectagles in any format you want. Make it clear which format you use.
* If multiple rectangles intersect at the same point, it only counts as one intersection.
* The coordinates will always be integers.
* There won't be any duplicate rectangles in the input.
* You will always get at least one rectangle as input.
* You may not use any builtins which solve this problem directly. Additionally you may not use builtins that solve equations. All other builtins are allowed.
* The output has to be a single integer indicating the intersection count.

# Rules

# Test cases

Same format as in the example above. The rectangles are wrapped in a list.

```
[[(-8,6),(-4,-2)],[(-4,9),(4,3)],[(2,10),(14,4)],[(1,7),(10,-6)],[(7,4),(10,2)],[(5,2),(9,-4)],[(-6,-4),(-2,-6)]] -> 6
[[(-2,2),(6,-4)]] -> 0
[[(-12,10),(-8,6)],[(-14,6),(-10,2)],[(-10,6),(-6,2)]] -> 0
[[(-4,10),(6,2)],[(-2,8),(4,3)],[(1,6),(8,4)],[(2,11),(5,5)]] -> 10
[[(8,2),(12,-2)],[(10,0),(14,-4)]] -> 2
[[(0,2),(2,0)],[(0,1),(3,0)]] -> 1
[[(-10,-2),(-6,-6)],[(-6,-2),(-2,-6)],[(-8,-4),(-4,-8)]] -> 3

```

**Happy Coding!**

# Answer
## JavaScript (ES6), 186 bytes

```
a=>a.map(([a,b,c,d])=>h.push([b,a,c],[d,a,c])&v.push([a,b,d],[c,b,d]),h=[],v=[])|h.map(([d,a,e])=>v.map(([c,f,b])=>a<c&c<e&b<d&d<f&t.every(([a,b])=>a-c|b-d)&&t.push([c,d])),t=[])|t.length

```

Splits each rectangle into its component lines, then intersects the horizontal and vertical lines, building up a list of intersections to avoid duplicates.
> 2 votes

# Answer
# Mathematica 138 bytes

Not finished! This works for all cases except `[[(0,0),(1,2)],[(0,0),(2,1)]]`

---

```
Length@Union[Join@@(Cases[RegionIntersection@@# &/@Subsets[Line[{{#,#2},{#3,#2},{#3,#4},{#,#4},{#,#2}}]&@@@Flatten/@#,{2}],Point@a__:> a])]

```

---

**Example**

```
Length@Union[
Join @@ (Cases[RegionIntersection @@ # & /@ Subsets[
Line[{{#, #2}, {#3, #2}, {#3, #4}, {#, #4}, {#, #2}}] & @@@ Flatten /@ #, {2}],
Point@a__ :> a])] &@{{{-8, 6}, {-4, -2}}, {{-4, 9}, {4, 3}}, {{2, 10}, {14, 4}},
{{1, 7}, {10, -6}}, {{7, 4}, {10, 2}}, {{5, 2}, {9, -4}}, {{-6, -4}, {-2, -6}}}

```

> 6
> 0 votes

---
Tags: code-golf, math, geometry, grid
---


title: Palindromic palindrome checker

text:
# Question
Title: Palindromic palindrome checker
Write a program to test if a string is palindromic, with the added condition that the program be palindromic itself.

# Answer
> 34 votes
## Ruby

```
z=gets;puts *z.reverse==z&&1||0||1&&z==esrever.z* stup;steg=z

```

Prints 1 if the input is a palindrome, 0 if it isn't. Input without linebreak.

Doesn't use any comments, instead it uses 2 tricks:

* Short-circuiting: `0` is true-ish in Ruby (only `nil` and `false` evaluate to false), so `1&&z==esrever.z* stup` isn't evaluated and thus can't raise a runtime exception
* The splat/multiplication operator (`*`): To avoid a syntax error in `z=esrever.z stup`, we force the parser to parse this as `z=esrever.z()*stup` by adding a `*`. On the other side, the `*` is parsed as a splat operator, which in a function call splits an array in a series of parameters. If there's only one element instead of an array, it basically does nothing, so `puts *foo` is equivalent to `puts foo`.

Obvious solution using comments (prints true/false):

```
puts gets.reverse==$_#_$==esrever.steg stup

```

# Answer
> 28 votes
**Python without comment**

```
"a\";w=]1-::[w trinp;)(tupni_war=w;";w=raw_input();print w[::-1]==w;"\a"

```

I'm surprised that no one found that trick yet, it should work in most languages!

# Answer
> 14 votes
## Perl

`perl -nle "$s=$_ eq+reverse;print$s;s$tnirp;esrever+qe _$=s$"`

no comment tricks, just cleverly abusing the substitution operator (hey, perl variables start with a $ too, so what?)

# Answer
> 7 votes
# Python 2.7

```
s=raw_input();print'YNEOS'[s!=s[::-1]::2]#]2::]1-::[s=!s['SOENY'tnirp;)(tupni_war=s

```

# Answer
> 6 votes
## C

```
#include <stdio.h>                   //
#include <string.h>                  //
int main() {                         //
  char str[1024];                    //
  fgets(str, sizeof(str), stdin);    //
  int i = 0, j = strlen(str) - 2;    //
  for (; i < j; i++, j--)            //
    if (str[i] != str[j]) {          //
      printf("no\n");                //
      return 0;                      //
    }                                //
  printf("yes\n");                   //
}                                    //
//                                    }
//                   ;)"n\sey"(ftnirp
//                                }
//                      ;0 nruter
//                ;)"n\on"(ftnirp
//          { )]j[rts =! ]i[rts( fi
//            )--j ,++i ;j < i ;( rof
//    ;2 - )rts(nelrts = j ,0 = i tni
//    ;)nidts ,)rts(foezis ,rts(stegf
//                    ;]4201[rts rahc
//                         { )(niam tni
//                  >h.gnirts< edulcni#
//                   >h.oidts< edulcni#

```

Running example:

```
$ gcc -std=c99 c.c && ./a.out
blahalb
yes

```

# Answer
> 5 votes
# Golfscript

```
.-1%=#=%1-.

```
* just with comment trick
* input without \n at the end
* perform matching char by char (even for punctuation)
* returns 1 for success, 0 for failure

# Answer
> 4 votes
# PHP

```
echo strrev($z)==$z;#;z$==)z$(verrts ohce

```

Facts:

* `$z` string, the input string to check
* `$t` boolean, TRUE if the input string `$z` is palindrome, FALSE otherwise
* Using comments to help me make the code palindrome.
* Outputs `$t`
* Source itself is a palindrome

The reason why it will not be possible to implement palindromic palindrome checker in PHP it's because PHP variables are named starting with a `$`. You cannot end an identifier name with `$` in PHP.

# Answer
> 2 votes
# PHP

```
<?php eval/*/*/(';{$i=fgets(STDIN,2e9);};{$a="strrev";}{var_dump("{$i}"=="{$a($i)}");}/*}{*\{;("{(tupni$)a$}"=="{putni$}")ohce}{;"verrts"==a$};{;(9e2,NIDTS)stegf=i$);');/*\*\eval php?>

```

Uses some odd tricks to avoid the `$` issue, technically isnt a palindrome as I had to sneak a `;` in at the end.

```
 <?php $i = fgets(STDIN,2e9); echo $i == strrev($i);/*\;(i$)verrts == i$ ohce ;(9e2, NIDTS)stegf = $i php?>

```

This is a working one that uses PHP's `/* */` comments and the fact that you don't need the end for them.

# Answer
> 2 votes
# Fuzzy Octo Guacamole, 17 bytes

```
^Cz.=i_;@;_i=.zC^

```

Not exactly sure how the win is defined, but I put the byte count up top.

`^` gets the input and pushes it to the first stack.

`C` copies the first stack to the second.

`z` reverse the top of the stack, so "as" becomes "sa".

`.` shifts the active stack, so the active stack has the input, and the inactive one has the reversed input.

`=` checks for equality, returning `0` for equality.

`i` inverts the ToS, so `0` becomes `1`, and anything else pretty much becomes `False`.

`_` pops and sets the temp variable which the `;` then prints.

`@` ends the program manually, so it doesn't hit the reversed part. This makes the palindrome.

# Answer
> 1 votes
## CoffeeScript

I actually struggled with the reverse spellings of 'split', 'reverse' and 'join' :\

```
p=(s)->s.split('').reverse().join('')==s#s==)''(nioj.)(esrever.)''(tilps.s>-)s(=p

```

# Answer
> 1 votes
## Groovy

```
print args[0]==args[0].reverse()?1:0//0:1?)(esrever.]0[sgra==]0[sgra tnirp

```

# Answer
> 1 votes
# Python 3, 55 bytes

Uses a comment, but is shorter than the other Python one that uses comments.

```
s=input();print(s==s[::-1])#)]1-::[s==s(tnirp;)(tupni=s

```

# Answer
> 0 votes
# Javascript

```
function a(b){return b==b.split('').reverse().join('')}//})''(nioj.)(esrever.)''(tilps.b==b nruter{)b(a noitcnuf

```

Hard to do it without comments...

---
Tags: code-challenge, palindrome
---


title: Numeronyms or N8s?

text:
# Question
Title: Numeronyms or N8s?
A numeronym (also known as a "numerical contraction") is when a word is shortened using numbers. A common contraction method is to use the length of the replaced substring to replace all but the first and last characters. For example, use `i18n` instead of `internationalization`, or `L10n` instead of `localization`. (The `L` is capitalized since a lowercase one looks too similar to the `1`.)

Of course, several words in the same phrase may have the same abbreviated form, so your job is to convert a group of words to their numeronyms, or in the event that there exists some *different* words with the same numeronym, your program should give a result of `A7s R4t`, short for `Ambiguous Result` (yes, I know that this result is itself an ambiguous result.)

## Rules:

* Use a program or function, and print or return the result.
* Input is taken as a single string.
* The output is a single string of space-delimited words.
* Your program need not convert words with length 3, and should not convert shorter words.
* If an `l` (lowercase ell) would occur before a `1` (one), it should be made uppercase.
* If an `I` (uppercase eye) would occur before a `1` (one), it should be made lowercase.
* Input will be printable ASCII and spaces. Words are separated by spaces.
* Shortest code wins.

## Examples:

```
A7s R4t -> A7s R4t (OR: A1s R1t, etc)
Ambiguous Result -> A7s R4t
Billy goats gruff -> B3y g3s g3f
Thanks for the Memories -> T4s f1r the M6s (one possible answer, NOT: Thnks fr th Mmrs)
Programming Puzzles & Code Golf -> P9g P5s & C2e G2f
globalization -> g11n
localizability -> L12y
Internationalization or antidisestablishmentarianism -> i18n or a26m
Internationalization or InternXXXXXalization -> A7s R4t
what is this fiddle and faddle -> A7s R4t
A be see -> A be s1e (OR: A be see)
see sea -> s1e s1a (OR: see sea)
2B or not 2B -> 2B or not 2B (OR: 2B or n1t 2B. 2 letters, don't change, don't count as ambiguous)
this example is this example -> t2s e5e is t2s e5e (same words aren't ambiguous)
l1 -> l1 (2 letters, don't change.)
I1 -> I1 (2 letters, don't change.)

```

Edit: If anyone didn't get the reference: Thnks fr th Mmrs

# Answer
> 1 votes
# J, 92 bytes

```
(' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:))

```

A looong chain of composed calls. The first part is just a fork: a literal, `joinstring`, and a function (in J, `1 (10 + +) 2` is `(10 + (1 + 2))`). The second part is the function. The two `rplc` calls are to replace the I/l when they could be confused. `&` is composition, and composing an operator with a value curries. (so `3&+` gives a function that expects the 2nd argument to be added). Finally, the last part is the function that takes the first, the length-2 (composed with stringification), and the last. the `&>&;:` bit at the end composes this function (which should be applied to every argument) to `>` (unboxing), because `;:` (split words) returns a boxed list (so that every element can have different length).

example:

```
     (' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:)) 'hey you baby Iooooooooneey I1'
hey you b2y i11y I1

```

# Answer
> 4 votes
# Caché ObjectScript, 231 bytes

```
r(a,b) s o=$REPLACE(o,a,b) q
z(s) f i=1:1:$L(s," ") s u=$P(s," ",i),l=$L(u),o=$S(l<4:u,1:$E(u)_(l-2)_$E(u,l)) d:l>3 r("I1","i1"),r("l1","L1") d  g:r z+4
    . i '(l<4!(v(o)=u!'$D(v(o)))) s r=1 q
    . s v(o)=u,t=t_o_" "
    q t
    q "A7s R4t"

```

This would be good-ol' standards-compliant MUMPS if it weren't for that pesky `$REPLACE` call, which isn't part of the standard. Reimplementing it in pure M takes a good 80ish bytes, so I didn't go down that route.

The entry point is `$$z("your string here")`, which returns `"y2r s4g h2e"`, and so forth.

# Answer
> 3 votes
# C#, ~~280~~ 274 bytes

First time golfer here! Been enjoying reading these lately and so I thought I might try out some myself! Probably not the best solution, but oh well!

```
class B{static void Main(string[] a){string[] n=Console.ReadLine().Split(' ');string o="";int j,i=j=0;for(;j<n.Length;j++){int s=n[j].Length;n[j]=((s<4)?n[j]:""+n[j][0]+(s-2)+n[j][s-1])+" ";o+=n[j];for(;i<j;i++)if(n[j]==n[i]){o="A7s R4t";j=n.Length;}}Console.WriteLine(o);}}

```

Same thing ungolfed:

```
class B
{
    static void Main(string[] a)
    {
        string[] n = Console.ReadLine().Split(' ');
        string o = "";
        int j, i = j = 0;
        for(; j < n.Length;j++)
        {
            int s = n[j].Length;

            n[j] = ((s<4) ? n[j] : "" + n[j][0] + (s - 2) + n[j][s - 1]) + " ";
            o += n[j];
            for (; i < j; i++)
            {
                if (n[j] == n[i]) { o = "A7s R4t"; j=n.Length;}
            }
        }
        Console.WriteLine(o);
    }
}

```

Thanks guys!

# Answer
> 2 votes
# Perl, 131 120 bytes

I've added a byte for using the `-p` switch:

```
s/\B(\w+)(\w)/length($1)."$2_$1"/ge;$_="A7s R4t\n"if/(\w\d+\w)(\w+)\b.*\1(?!\2)/;s/_\w+//g;s/I1\w/\l$&/g;s/l1\w/\u$&/g;

```

### Explanation

```
# Replace internal letters with count, but keep them around for the next test.
s/\B(\w+)(\w)/length($1)."$2_$1"/ge;
# Detect ambiguous result
$_ = "A7s R4t\n" if
    # Use negative look-ahead assertion to find conflicts
    /(\w\d+\w)(\w+)\b.*\1(?!\2)/;
# We're done with the internal letters now
s/_\w+//g;
# Transform case of initial 'I' and 'l', but only before '1'
s/I1\w/\l$&/g;
s/l1\w/\u$&/g;

```

# Answer
> 1 votes
# JavaScript (ES6), 165 bytes

```
s=>(r=s.replace(/\S+/g,w=>(m=w.length-2)<2?w:(i=d.indexOf(n=((m+s)[0]<2&&{I:"i",l:"L"}[w[0]]||w[0])+m+w[m+1]))>=0&d[i+1]!=w?v=0:d.push(n,w)&&n,v=d=[]),v?r:"A7s R4t")

```

## Explanation

```
s=>(                         // s = input string
  r=s.replace(               // r = result of s with words replaced by numeronyms
    /\S+/g,                  // match each word
    w=>                      // w = matched word
      (m=w.length-2)         // m = number of characters in the middle of the word
        <2?w:                // if the word length is less than 4 leave it as it is
      (i=d.indexOf(          // check if this numeronym has been used before
        n=                   // n = numeronymified word
          ((m+s)[0]<2&&      // if the number starts with 1 we may need to replace
            {I:"i",l:"L"}    //     the first character with one of these
              [w[0]]||w[0])+ // return the replaced or original character
          m+w[m+1]
      ))>=0&d[i+1]!=w?       // set result as invalid if the same numeronym has been
        v=0:                 //     used before with a different word
      d.push(n,w)&&n,        // if everything is fine return n and add it to the list
    v=                       // v = true if result is valid
      d=[]                   // d = array of numeronyms used followed by their original word
  ),
  v?r:"A7s R4t"              // return the result
)

```

## Test

```
<input type="text" id="input" value="lnternationalization or antidisestablishmentarianism" />
<button onclick='result.textContent=(

s=>(r=s.replace(/\S+/g,w=>(m=w.length-2)<2?w:(i=d.indexOf(n=((m+s)[0]<2&&{I:"i",l:"L"}[w[0]]||w[0])+m+w[m+1]))>=0&d[i+1]!=w?v=0:d.push(n,w)&&n,v=d=[]),v?r:"A7s R4t")

)(input.value)'>Go</button>
<pre id="result"></pre>
```

# Answer
> 1 votes
# JavaScript ES6, 162

```
w=>(v=(w=w.split` `).map(x=>(l=x.length-2+'')>1?((l[0]>1||{I:'i',l:'L'})[x[0]]||x[0])+l+x[-~l]:x)).some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j]))?'A7s R4t':v.join` `

```

**Less golfed**

```
// Less golfed
f=w=>{
  w = w.split` ` // original text splitted in words
  v = w.map(x=> { // build modified words in array v

    l = x.length - 2 // word length - 2
    if (l > 1) // if word length is 4 or more
    {
      a = x[0] // get first char of word
      l = l+'' // convert to string to get the first digit
      m = l[0] > 1 || {I:'i', l:'L'} // only if first digit is 1, prepare to remap I to i and l to L
      a = m[a] || a // remap
      return a + l + x[-~l] // note: -~ convert back to number and add 1
    }
    else
      return x // word unchanged
  })
  return v.some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j])) // look for equals Numeronyms on different words
  ? 'A7s R4t'
  : v.join` `
}

```

**Test**

```
F=w=>(v=(w=w.split` `).map(x=>(l=x.length-2+'')>1?((l[0]>1||{I:'i',l:'L'})[x[0]]||x[0])+l+x[-~l]:x)).some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j]))?'A7s R4t':v.join` `

console.log=x=>O.innerHTML+=x+'\n'

;['A7s R4t','Ambiguous Result','Billy goats gruff','Thanks for the Memories',
'Programming Puzzles & Code Golf','globalization','localizability',
'Internationalization or antidisestablishmentarianism',
'Internationalization or InternXXXXXalization','what is this fiddle and faddle',
'A be see','see sea','2B or not 2B','this example is this example','l1','I1']
.forEach(t=>console.log(t+' -> '+F(t)))
```

```
<pre id=O></pre>
```

# Answer
> 1 votes
# Python 2, 185 bytes

```
d={}
r=''
for w in input().split():
 l=len(w);x=(w[0]+`l-2`+w[-1]).replace('l1','L1').replace('I1','i1')
 if l<4:x=w
 if d.get(x,w)!=w:r='A7s R4t';break
 d[x]=w;r+=x+' '
print r.strip()

```

# Answer
> 1 votes
# Factor, ~~48~~ 35 bytes, noncompeting

It's a lambda that goes on the stack, that doesn't technically meet the *really annoyingly hard* requirements that I originally managed to overlook.

```
[ " " split [ a10n ] map " " join ]

```

Uses the `english` vocabulary.

Or, if we inline the `a10n` library word, then 131 bytes (with auto-imports):

```
: a ( b -- c ) " " split [ dup length 3 > [ [ 1 head ] [ length 2 - number>string ] [ 1 tail* ] tri 3append ] when ] map " " join ;

```

# Answer
> 1 votes
# Python 3, 160

I'd love to find a good way to replace those replace calls.

```
def f(a):y=[(x,(x[0]+str(len(x)-2)+x[-1]).replace('l1','L1').replace('I1','i1'))[len(x)>3]for x in a.split()];return('A7s R4t',' '.join(y))[len(set(y))==len(y)]

```

With some test cases:

```
assert f('Billy goats gruff') == 'B3y g3s g3f'
assert f('Programming Puzzles & Code Golf') == 'P9g P5s & C2e G2f'
assert f('Internationalization or InternXXXXXalization') == 'A7s R4t'

```

---
Tags: code-golf, string
---


title: Wargame Tank Simulation

text:
# Question
Title: Wargame Tank Simulation
Some of you may have heard of the Wargame series of computer based real time strategy game. These games pit teams of players with primarily cold war era units to see how a hot cold war would have played out. The goal of this challenge is to simulate a tank battle in these games.

**Input**

* Two "tanks" (one red and one blue) will be entered into your program or function. Each tank is classified by a rate of fire, accuracy, armor value, and attack power value.

**Challenge**

From the inputs above, simulate the two tanks fighting. You will do this by having each tank fire according to its rate of fire. If it hits (randomly determined by accuracy), a tank will do damage according to the armor value of its target and its own attack power. The formula for damage is `floor[(attackpower - armor)/2]`. Therefore a tank with 10 attack power against a tank with 5 armor would do 2 damage.

Tank crews also have morale, which follows the following rule

* There are four possible morale values; calm, worried, scared, and panicked. Tanks always start calm. These names do not need to be in your code, in the sample below I've used calm = 1, worried = 2, etc.
* Each morale value reduces the accuracy as follows: Calm -\> 100% (no change), worried -\> 75%, scared -\> 50%, panicked -\> 25%. Therefore a panicked tank which normally has 60% accuracy now has 0.25 * 0.6 = 15% accuracy
* Each hit by the opposing tank degrades morale by one level, each miss upgrades the morale by one level.

For example:

```
morale: calm  |  worried  |  calm  |  worried  |  scared
hit:         hit         miss     hit         hit

```

**Rules:**

* Input should be two parameters to repesent each tank (I've used two tuples in the example below). Inputs may be provided in any order, just be sure to state which one is which. Input may be provided by user input via STDIN, read from a file, or parameters passed to a function call.
* Each tank starts with 10 health.
* Rate of fire will either be 8.5, 7.5, 6.5, 6, 5, or 3 seconds between shots.
* Tanks start loaded, so each fires at time = 0. Because Communists are sneaky, red fires first.
* Accuracy must be randomly rolled.
* Ineffective hits (hits which do no damage) have an effect on morale! (because it probably sounds terrifying)
* Naturally since we want to see the action, output will be an update after each shot. The update will contain the time of the shot, whom it was made by (red or blue), health of both tanks, and the morale of both tank crews. Output maybe be presented in any format so long as it contains all of the required information in a human readable fashion (items must be delimited in some way). Similar to input, please describe your output format in the answer.
* Engagements are limited to 100 seconds. If you play the game you know this is because a plane has swooped in by then. For our purposes if both tanks are alive at this point, it is a draw.
* After one tank reaches 0 health (or after 100 seconds), print which tank is victorious (`"Red"` or `"Blue"`) or `"Draw"` if appropriate. I don't care about trailing whitespace or newlines.
* Printing output may be printing to STDOUT or writing to a file
* Shortest answer in bytes wins

**Sample Python Implementation**

This is a little sample I worked up to demonstrate some of the methodology. It takes the two tanks as tuples to the `main()` function, then outputs a nice little table. Your output does not need to be so fancy. For the curious, it's 1,930 bytes. You can do better.

**Sample Tanks:**

Some tanks to use for you, the numbers are ROF, accuracy, armor, and power.

* T-72A: 8.5, 45%, 12, 16
* T-72BU: 8.5, 55%, 22, 23
* T-80: 7.5, 40%, 14, 16
* T-80U: 7.5, 60%, 20, 23
* M1 Abrams: 7.5, 55%, 16, 18
* M1A2 Abrams: 7.5, 60%, 22, 24
* Leclerc: 6, 65%, 20, 20
* STRV-103D: 5, 60%, 18, 16
* Zhalo (I know, not a tank): 3, 60%, 2, 18

# Answer
# Python 3, 465

Saved 8 bytes thanks to wnnmaw.
Saved 34 bytes thanks to DSM.

Expects tanks as lists as follows, `[Rate of Fire, Accuracy, Armor, Attack]`

```
from random import*
def f(r,b):
 r+=10,1;b+=10,1
 for x in range(200):
  if r[4]<1:return"Blue"
  if b[4]<1:return"Red"
  if x%int(r[0]*2)<1:
   if random()>=(r[1]*r[5]):b[4]-=(r[3]-b[2])//2;b[5]-=(b[5]!=0.25)/4
   else:b[5]+=(b[5]<1)/4
   print('r,%f,%d,%f,%d,%f'%(x/2,*r[4:],*b[4:]))
  if x%int(b[0]*2)<1:
   if random()>=(b[1]*b[5]):r[4]-=(b[3]-r[2])//2;r[5]-=(r[5]!=0.25)/4
   else:r[5]+=(r[5]<1)/4
   print('b,%f,%d,%f,%d,%f'%(x/2,*r[4:],*b[4:]))
 return"Draw"

```

Prints the output as `Attacker,TimeOfShot,RedHealth,RedMorale,BlueHealth,BlueMorale`. Morale is represented as a float.
> 2 votes

---
Tags: code-golf
---


title: Partition a list!

text:
# Question
Title: Partition a list!
In this challenge, you need to partition a list, where partitions have a max size, a min size, and a preferred size. I'll be using the notation `(min,pref,max)` to indicate the sizes in this challenge.

For those unfamiliar with partitioning, the following list has been partitioned into parts of 3:
`[0..9] -> [[0,1,2],[3,4,5],[6,7,8]]`

When the list is not evenly divisible, you need the partitions to be as close to the preferred size as possible: `[0..10], (2,4,5) -> [[0,1,2,3],[4,5,6],[7,8,9]]`. This partitioning is preferable to `[[0,1,2,3],[4,5,6,7],[8,9]]`, even though the latter has more of the preferred length. Formally, we need to minimize the sum of `(partitionLength-preferredSize)^2` for each partition.

The order of the partition lengths does not matter: For `[0..5], (2,3,3)`, either `[[0,1,2],[3,4]]` or `[[0,1],[2,3,4]]` works. If no partition is possible, return an empty array: `[0..7], (4,4,5) -> []`.

You can assume that `1<=min<=pref<=max`, and that the array passed to you is a non-empty array of integers. The array will always be the first argument. You can accept min, max, and pref in any order and as a tuple or as separate arguments.

Your program must run under a couple of seconds. Basically, iterating through every possible partition size within the bounds is not allowed.

# Test cases:

```
[1], (1,3,4)         -> [[1]]
[100], (1,2,3)       -> [[100]]
[1,2], (1,1,2)       -> [[1],[2]]
[1,2], (1,2,2)       -> [[1,2]]
[1,2], (1,3,3)       -> [[1,2]]
[1,2,3], (1,2,3)     -> [[1,2],[3]] or [[1,2,3]]
[1,2,3,4], (1,3,4)   -> [[1,2,3,4]]
[1,2,3,4,5], (3,3,4) -> []
[1,2,3,4,5], (2,2,2) -> []
[1,2,3,4,5], (3,3,5) -> [[1,2,3,4,5]]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49], (2,6,6) -> [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29],[30,31,32,33,34],[35,36,37,38,39],[40,41,42,43,44],[45,46,47,48,49]]

```

This is a , so aim for as few bytes as possible in your favorite language!

# Answer
> 2 votes
# Haskell, 152

```
_%0=[]
s%k|(a,b)<-splitAt(div(z s)k)s=a:b%(k-1)
z=length
f s n p x=snd$minimum$(z s*p^2,[]):[(sum[(z x-p)^2|x<-s%k],s%k)|k<-[-div(-z s)x..div(z s)n]]

```

In any partition, if there are two lists which have lengths that differ by two or more, it will always be beneficial to shrink the bigger list and enlarge the smaller list. therefore, we only need to consider partitions with only two list sizes, which simplifies the computation.

the program then runs on all the possible amounts of lists in the partition. given the amount of lists in the partition, the score is uniquely determined. the program computes a fitting partition, and its score.

then it finds the overall minimum, and returns it.

Usage: input like `f [1,2,3,4,5] 1 3 4` (`f` is the function that solves the challenge)

Although it is possible to computing the best option completely numerically and only then partitioning the list accordingly, it took too many bytes. however, my last version of this approach is:

```
_%0=[]
s%k|(a,b)<-splitAt(div(length s)k)s=a:b%(k-1)
f s n p x|l<-length s=(s%)$snd$minimum$(l*p^2,0):[(k*j^2+mod l k*(1-2*j),k)|k<-[1..div l n],k*x>=l,j<-[p-div l k]]

```

# Answer
> 1 votes
# CJam, 70

```
q~:S;_,[0L]a{_S2=e<),S0=>f{[__S1=-_*\]@@-j.+}[esL]a+:e<}j1={/(\e_}/;]p

```

Try it online

The code finds an optimal sequence of partition sizes based on the list size, using dynamic programming (via memoized recursion), then goes ahead and partitions the list.

---
Tags: code-golf, array, set-partitions
---