title: Add a feature to a programming language

text:
# Question
Title: Add a feature to a programming language
Your task is to tack on a feature to a programming language, either by implementing a very clever library, or by processing the input text and/or tweaking the compilation process.

Ideas:

* Add PHP-style presentation interleaving to C (e.g. `<?c printf("Hello,"); ?> world!`).
* Add a null coalescing operator to one of those languages that isn't C#.
* Add macros to PHP.
* Add `goto` to JavaScript.
* Add pattern matching to language X.
* Add namespace support to a language that doesn't have it.
* Make C look like PHP.
* Make Haskell look like Pascal.
* ... (feel free to post ideas in the comment section)

Rules:

* Bring something to the table. Don't just say "Template Haskell" to add metaprogramming facilities to Haskell. This isn't StackOverflow.
* The entire implementation should fit in one screenful (not counting the example).
* Do not host code on an external site specifically for this task.
* The most impressive or surprising feature wins.

Don't worry about implementing the feature 100% correctly. Far from it! The main challenge is figuring out what you want to do and *viciously cutting away details* until your planned undertaking becomes feasible.

## Example:

Add a lambda operator to the C programming language.

Initial approach:

> Okay, I know I'd want to use libgc so my lambdas will solve the upward and downward funarg problems. I guess the first thing I'd need to do is write/find a parser for the C programming language, then I'd need to learn all about C's type system. I'd have to figure out how to make sense of it as far as types go. Would I need to implement type inference, or should I simply require that the formal parameter be typed as given? What about all those crazy features in C I don't know about yet?

It's quite clear that implementing lambda in C *correctly* would be a huge undertaking. Forget about correctness! Simplify, simplify.

Better:

> Screw upward funargs, who needs 'em? I might be able to do something tricky with GNU C's nested functions and statement expressions. I wanted to show off an amazing syntactic transformation on C with terse, hacky code, but I won't even need a parser for this. That can wait for another day.

Result (requires GCC):

```
#include <stdio.h>
#include <stdlib.h>

#define lambda(d,e)({d;typeof(e)f(d){return(e);};f;})

#define map(F,A)({typeof(F)f=(F);typeof(*(A))*a=(A);({int i,l=((int*)(a))[-1]; \
typeof(f(*a))*r=(void*)((char*)malloc(sizeof(int)+l*sizeof(*r))+sizeof(int));  \
((int*)r)[-1]=l;for(i=0;i<l;i++)r[i]=f(a[i]);r;});})

#define convert_to(T) lambda(T x, x)
#define print(T, fmt) lambda(T x, printf(fmt "\n", x))

int main(void)
{
    int *array = 1 + (int[]){10, 1,2,3,4,5,6,7,8,9,10};
    map(print(int, "%d"), array);

    double *array2 = map(lambda(int x, (double)x * 0.5), array);
    map(print(double, "%.1f"), array2);

    long *array3 = map(convert_to(long), array2);
    map(print(long, "%ld"), array3);

    long product = 1;
    map(lambda(int x, product *= x), array);
    printf("product: %ld\n", product);

    return 0;
}

```

That was easy, wasn't it? I even threw in a `map` macro to make it useful and pretty.

# Answer
## OOP syntax in Haskell

```
import Prelude hiding ((.))
a . b = b a

```

Objects can have properties:

```
[1,5,3,2].length -- 4
[1,5,3,2].maximum -- 5
'a'.succ -- 'b'

```

...and methods:

```
"Hello world!".take(5) -- "Hello"
"Hello world!".splitAt(2) -- ("He","llo world!")
"Hello world!".map(toUpper) -- "HELLO WORLD!"

```
> 29 votes

# Answer
## `goto` in JavaScript?

My first thought was a **functional approach** – to add a parameter to the function to indicate where execution should start, using that with a `switch` statement and an outer loop repeatedly calling the function on its own return value. Unfortunately, that would preclude the use of local variables, as they would lose their values with every goto.

I could use a `with` statement and move all variable declarations to the beginning of the function, but there had to be a better way. It eventually came to me to use **JavaScript's exception handling**. In fact, Joel Spolsky said, "I consider exceptions to be no better than "goto's..." – obviously a perfect fit.

The idea was to put an infinite loop inside a function, terminated only by a `return` statement or an uncaught exception. All gotos, treated as exceptions, would be caught within the loop to prevent its termination. Here is the result of that approach:

```
function rewriteGoTo(func) {
    var code = '(';
    code += func.toString()
        .replace(/^\s*(\w+)\s*:/gm, 'case "$1":')
        .replace('{', '{ var $_label = ""; function goTo(label) { $_label = label; throw goTo; } while(true) try { { switch($_label) { case "": ');
    code += '} return; } catch($_e) { if($_e !== goTo) throw $_e; } })';
    return code;
}

```

You can use it like this – **even in ES5 strict mode** – except in Internet Explorer (**demo**):

```
var test = eval(rewriteGoTo(function(before) {
    var i = 1;
    again: print(before + i);
    i = i + 1;
    if(i <= 10) goTo('again');
}));

```

\[Internet Explorer, for some reason, fails to eval the code of an anonymous function, so one would have to give the function a name (before rewriting) and call it using that name. Of course, that would probably break strict mode's rules.\]

This does not allow jumping to a statement located within a block (until such constructs as Duff's device become legal), but we can deal with that (another, self-executing rewritten function), right?
> 25 votes

# Answer
## #define in Java

I thought it would be fun to implement macros in Java.

```
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * defines the use of #define. Usage:
 *
 * #def toReplaceCanHaveNoSpaces replacement can have extra spaces
 *
 * must be at the beginning of the line (excluding starting spaces or tabs)
 *
 * @author Quincunx
 */
public class Define {

    public static void main(String[] args) {
        if (args.length != 1) {
            err("Please provide exactly 1 argument");
        }
        File source = new File(args[0]);
        if (!source.exists()) {
            err("Supplied filepath does not point to an existing file");
        }
        if (!getExtension(args[0]).equalsIgnoreCase(".java")) {
            err("Supplied file is not of type .java");
        }
        ArrayList<String> sourceData = new ArrayList<>();
        ArrayList<String[]> replacements = new ArrayList<>();
        try {
            BufferedReader read = new BufferedReader(new FileReader(source));
            String data;
            while ((data = read.readLine()) != null) {
                sourceData.add(data);
            }
            read.close();
        } catch (IOException ex) {
            Logger.getLogger(Define.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int index = 0; index < sourceData.size(); index++) {
            String line = sourceData.get(index);
            line = line.replaceAll("\t", "    ");
            for (String[] e : replacements) {
                line = line.replace(e[0], e[1]);
            }

            if (line.trim().charAt(0) != '#') {
                sourceData.set(index, line);
                continue;
            }
            while (line.charAt(0) != '#') {
                line = line.substring(1);
            }
            int indexOf = line.indexOf(" ");
            String type = line.substring(1, indexOf);

            switch (type) {
                case "def":
                case "define":
                    String toReplace = line.substring(indexOf + 1, line.indexOf(" ", indexOf + 1));
                    replacements.add(new String[]{toReplace, line.substring(line.indexOf(":") + 1)});
                    break;
                default:
                    err("The source code contains a # in which there is no correct type");
            }
        }

        try {
            BufferedWriter write = new BufferedWriter(new FileWriter(source));
            for (String s : sourceData) {
                write.write(s);
                write.newLine();
            }
            write.close();
        } catch (IOException ex) {
            Logger.getLogger(Define.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void err(String message) {
        System.err.println(message);
        System.exit(1);
    }

    public static String getExtension(String filePath) {
        return filePath.substring(filePath.lastIndexOf("."));
    }

}

```

Sample usage (converts to previously posted code; let's make it weird):

```
#def @ o
#def ~ a
#def $ i
#def ` e
#d`f % m
#d`f ! {
#d`f & d
#&`f _ }
#&`f 2 (
#&`f 7 )
#&`f $%p@rt$@. $%p@rt j~v~.$@.
#&`f $%p@rtu. $%p@rt j~v~.ut$l.
#&`f ps publ$c st~t$c
#&`f Str Str$ng

$%p@rt$@.Buff`r`&R`~&`r;
$%p@rt$@.Buff`r`&Wr$t`r;
$%p@rt$@.F$l`;
$%p@rt$@.F$l`R`~&`r;
$%p@rt$@.F$l`Wr$t`r;
$%p@rt$@.IOExc`pt$@n;
$%p@rtu.Arr~yL$st;
$%p@rtu.l@gg$ng.L`v`l;
$%p@rtu.l@gg$ng.L@gg`r;

#d`f L$st Arr~yL$st
#d`f l@g; L@gg`r.g`tL@gg`r2D`f$n`.cl~ss.g`tN~m`277.l@g2L`v`l.SEVERE, null, `x7;

publ$c cl~ss D`f$n` !

    ps v@$d %ain2Str[] ~rgs7!
        $f 2~rgs.l`ngth != 17 !
            `rr2"Pl`~s` pr@v$&` `x~ctly 1 ~rgu%`nt"7;
        _
        F$l` squrc` = n`w F$l`2~rgs[0]7;
        $f 2!sourc`.`x$sts277 !
            `rr2"Suppli`& f$l`p~th &@`s n@t p@int t@ ~n `x$st$ng f$l`"7;
        _
        $f 2!g`tExt`ns$@n2~rgs[0]7.`qu~lsIgn@r`C~s`2".j~v~"77 !
            `rr2"Suppl$`& f$l` $s n@t @f typ` .j~v~"7;
        _
        L$st<Str> s@urceDat~ = n`w List<>27;
        L$st<Str[]> repl~cem`nts = n`w L$st<>27;
        try !
            Buff`r`&R`a&`r r`a& = new Buff`redRe~&`r2n`w F$l`R`~&`r2s@urc`77;
            Str &~t~;
            wh$l` 22&~t~ = r`~&.r`~&L$n`277 != null7 !
                s@urc`D~t~.~&&2&ata7;
            _
            re~&.cl@se27;
        _ c~tch 2IOExc`ption ex7 !
            log;
        _
        f@r 2$nt $n&`x = 0; $ndex < s@urc`D~t~.s$z`27; $nd`x++7 !
            Str l$n` = s@urc`D~ta.get2index7;
            line = line.r`pl~c`All2"\t", "    "7;
            for 2Str[] ` : r`pl~c`%`nts7 {
                line = line.r`pl~c`2`[0], e[1]7;
            _

            if 2l$ne.tr$%27.ch~rAt207 != '#'7 !
                sourc`D~t~.s`t2$n&`x, l$n`7;
                c@nt$nu`;
            _
            wh$l` 2line.ch~rAt207 != '#'7 !
                l$ne = l$ne.substr$ng217;
            _
            $nt in&`xOf = line.$n&`xOf2" "7;
            Str typ` = line.substring21, indexOf7;

            sw$tch 2type7 !
                c~s` "&`f":
                c~s` "def$n`":
                    str t@R`pl~c` = line.substring2indexOf + 1, line.indexOf2" ", indexOf + 177;
                    r`pl~c`%`nts.~&&2n`w s\Str[]!t@R`place, line.substring2line.indexOf2":"7 + 17_7;
                    br`~k;
                def~ult:
                    err2"Th` s@urc` c@&` c@nt~$ns ~ # $n wh$ch th`r` i$s n@ c@rr`ct typ`"7;
            _
        _

        try !
            Buff`r`&Wr$ter wr$te = new BufferedWriter2new F$l1Wr$t1r2s@urc177;
            for 2Str s : s@urceData7 !
                wr$te.write2s7;
                wr$te.n`wLin`27;
            _
            wr$t`.cl@s`27;
        _ c~tch 2IOExc`pt$@n `x7 !
            l@g;
        _
    _

    ps v@$& `rr2Str m`ss~g`7 !
        Syst`%.`rr.pr$ntln2message7;
        Syst`%.`x$t217;
    _

    ps Str g`tExt`nsi@n2Str fileP~th7 !
        r`turn f$lePath.substr$ng2f$l`P~th.l~stInd`xOf2"."77;
    _

_

```
> 23 votes

# Answer
## Foreach in C

Iterate arrays (works for static arrays, not ones, received through pointer)

```
//syntactic beauty
#define in ,

//syntactic beauty's helper macro
#define foreach(a) _foreach(a)

//the real foreach macro
#define _foreach(e,arr)\
typeof (&arr[0]) e;\
for (e=&arr[0];e!=&arr[sizeof(arr)/sizeof(arr[0])];e++)

```

To test it:

```
int int_arr[3]={10,20,30};
char *strings[]={"Hello","World","Foreach","Test"};

foreach (num in int_arr) {
        printf ("num=%d\n",*num);
}

foreach (str in strings) {
        printf ("str=%s\n",*str);
}

```

result:

```
num=10
num=20
num=30
str=Hello
str=World
str=Foreach
str=Test

```
> 19 votes

# Answer
## Properties in C

Tomasz Wegrzanowski implemented properties in plain C, by **intentionally segfaulting** the program when the property is accessed.

An object with a "property" is set up by creating a `struct` that crosses multiple pages, ensuring that the property's memory address lies in a different page from the real data members. The property's page is marked as no-access, guaranteeing that attempting to access the property will cause a segfault. A fault handler then figures out which property access caused the segfault, and calls the appropriate function to compute the property's value, which gets stored at the property's memory address.

The fault handler also marks the data page as read-only to ensure the computed value remains consistent; when you next try to write to a data member, that triggers a segfault, whose handler sets the data page as read-write and the property page as no-access (indicating that it needs to be recomputed).
> 18 votes

# Answer
# Computed come-from in Common Lisp

I initially implemented come-from. But that wasn't good enough.

Inspired by the computed goto, I decided to implement computed come-from.

```
(defmacro computed-come-from-tagbody (&rest statements)
  (let ((has-comp-come-from nil)
        (comp-come-from-var nil)
        (start-tag (gensym))
        (end-tag (gensym)))

    (let ((current-tag start-tag)
          (come-froms (make-hash-table :test #'eq)))

      (let ((clauses '()))
        (loop for statement in statements do
             (if (symbolp statement)
                 (setf current-tag statement))

             (cond
               ((and (consp statement)
                     (eql 'come-from (car statement)))

                (setf has-comp-come-from t)
                (setf (gethash (cadr statement) come-froms) current-tag))
               (t (push statement clauses))))

        (if (not has-comp-come-from)
            `(tagbody ,@(reverse clauses))
            (let ((res '())
                  (current-tag start-tag))
              (loop for clause in (reverse clauses) do
                   (cond
                     ((symbolp clause)
                      (push clause res)
                      (setf current-tag clause)
                      ;; check all vars for jumps
                      (push
                       `(progn ,@(loop for k being the hash-key of come-froms
                                    for v being the hash-value of come-froms collect
                                      `(when (eql ,k ,current-tag)
                                         (go ,v))))
                       res))
                     (t (push clause res))))
              `(macrolet ((come-from (idx)
                            (declare (ignore idx))
                            (error "Come-from cannot be used with another form.")))
                 (tagbody ,@(reverse res)))))))))

```

## Examples of usage

```
(come-from x) ; whenever we're at the top of a labeled block and the value of x is equal to the label, jump back to this point.

```

For each come-from declaration in the tagbody, it will check at each label if the come-from variable is equal to the current label, and if so, jump to the corresponding come-from declaration.

**Greeter**

```
(let ((x :repeat)
      (y :exit))
   (computed-come-from-tagbody
      :loop              ;; when x == :loop jump to :loop.  when y == :loop jump to :exit
      (come-from x)
      (format t "What is your name? ")
      (let ((name (read-line)))
         (terpri)
         (format t "Hello ~a~%" name)
         (print (string= name "exit"))
         (when (string= name "exit")
             (setf x nil
                   y :repeat)))
       :repeat           ;; when x = :repeat jump to loop, when y = :repeat jump to exit
       :exit             ;; when x = :exit jump to loop, when y = :exit jump to exit
       (come-from y)))

```

**FizzBuzz**

```
(let ((i 0)
      (x nil)
      (y nil))
   (computed-come-from-tagbody
       :loop
       (come-from x)
       (cond
         ((> i 100)  (setf x nil
                           y :end-iteration))
         (t (or (and (zerop (mod i 3)) (zerop (mod i 5)) (print "FizzBuzz"))
                (and (zerop (mod i 3)) (print "Fizz"))
                (and (zerop (mod i 5)) (print "Buzz"))
                (print i))
            (incf i)
            (setf x :end-iteration)))
       :end-iteration
       :end
       (come-from y)
       (print "done")))

```
> 16 votes

# Answer
# "Auto-strings" in Ruby

The code is quite simple:

```
def self.method_missing *a; a.join ' '; end

```

Now you can do

```
print This is an automatic string #=> This is an automatic string
print hooray #=> hooray

x = code golf
print This is + ' ' + x + '!' #=> This is code golf!

```
> 15 votes

# Answer
## Add macros to PHP

We can just use the C preprocessor for this task.

A php script:

```
<?php

#define ERROR(str) trigger_error(#str, E_USER_ERROR)

function test() {
        ERROR(Oops);
}

```

Pipe it though cpp:

```
cpp < test.php

```

Result:

```
<?php

function test() {
 trigger_error("Oops", E_USER_ERROR);
}

```
> 13 votes

# Answer
## Pattern Matching Guards in Python

```
def pattern_match(n, s="__fns"):
 s=n+s;g=globals()
 def m(f):
  def a(*r):
   for f in g[s]:
    if reduce(lambda c,t:c and eval(t[1:],{},dict(zip(f.func_code.co_varnames,r))),filter(lambda x:x and x[0]is"|",map(lambda x:x.strip(),f.func_doc.split("\n")))): return f(*r)
  g[n]=a;g[s]=(g.get(s)or[])+[f]
  return a
 return m

```

The body of the function comes in at 288 characters.

Pattern Matching Guards allow you to use completely different functions depending on argument values. Although it can be easily emulated with a series of `if` statements, ~~pattern matching~~ guards can help separate sections of code, and it's a great excuse to do some crazy metaprogramming.

`pattern_match` is a decorator that creates a new function that implements pattern matching guards. The conditions for each "sub-function" given in each docstring on lines starting with a pipe (`|`). If all conditions evaluate truthily, that version of the function is run. Functions are tested in order until a match is found. Otherwise, `None` is returned.

An example will help clarify:

```
@pattern_match("test1")
def test1_a(a, b, c):
    """
    This guard tests if a and c are positive

    | a > 0
    | c > 0
    """
    return a + b + c

@pattern_match("test1")
def test1_b(a, b, c):
    """
    This pattern only ensures b is positive

    | b > 0
    """
    return b + c

@pattern_match("test1")
def test1_c(a, b, c):
    """
    Final catchall

    | True
    """
    return 0

print test1(1,2,3) # (a) >>> 6
print test1(1,2,0) # (b) >>> 2
print test1(1,0,0) # (c) >>> 0
print test1(0,0,1) # (b) >>> 1

```
> 10 votes

# Answer
Coroutine

I can't take credit for this, so i've marked it CW.

Coroutines in C by Simon Tatham
> 7 votes

# Answer
## Custom operators in Lua

Pogs cleverly abused operator overloading in Lua in order to allow custom infix operators to be defined. I've expanded this to support operator sectioning (partially applying an operator with either operand) and calling the resulting object as if it were a function.

```
---- implementation
function infix(f)
  local function g(self, x)
    return f(self[1] or x, self[2] or x)
  end

  local mt   = { __sub = g, __call = g }
  local self = {}
  return setmetatable(self,
           { __sub = function (lhs,rhs)
                       return rhs == self and setmetatable({ lhs, nil }, mt)
                                           or setmetatable({ nil, rhs }, mt)
                     end })
end

---- testing
local eq   = infix(function (a, b) return a == b end)
local ge   = infix(function (a, b) return a >= b end)

local comp = infix(function (a, b) return a < b and -1
                                       or a > b and  1
                                       or            0 end)

function filter(pred, xs)
  local res = {}
  for i=1,#xs do
    if pred(xs[i]) then table.insert(res, xs[i]) end
  end
  return res
end

print(1  -eq-  1)                                      --> true
print(1 -comp- 0)                                      --> 1
print((4 -ge)(1))                                      --> true
print(table.unpack(filter(ge- 0, {1,-4,3,2,-8,0})))    --> 1   3   2   0

```
> 7 votes

# Answer
# Make C simpler

This code allows you to write C programs that kind of resemble a scripting language a bit more. It features keywords such as 'var', 'is', 'string', 'plus', 'equal' and several others. It works through *a lot* of define statements.

```
// pretty.c

#include<stdio.h>

#define function int
#define var int
#define is =
#define then {
#define do {
#define does {
#define end }
#define equal ==
#define notequal !=
#define greater >
#define less <
#define greaterequal >=
#define lessequal <=
#define display printf
#define otherwise }else{
#define increase ++
#define decrease --
#define plus +
#define minus -
#define times *
#define divide /
#define character char
#define string char*
#define integer int

```

This allows you to write code like:

```
/*
Preprocessor abuse, Yay!
*/

#include "pretty.c"

function main() does
    var myVar is 1;
    if(myVar greater 2)then
        display("Yep\n");
    otherwise
        display("Nope\n");
    end

    for(var i is 0; i less 10; i increase)do
        display("Loop: %d\n", i);
    end

    string myString = "Hello";
    display(myString);
end

```

The above gets expanded to:

```
int main() {
    int myVar = 1;
    if(myVar > 2){
        printf("Yep\n");
    }else{
        printf("Nope\n");
    }

    for(int i = 0; i < 10; i ++){
        printf("Loop: %d\n", i);
    }

    char* myString = "Hello";
    printf(myString);
}

```

Probably not overly useful but I found it quite interesting that you could essentially create an entire programming language through a bunch of `#define`s.
> 7 votes

# Answer
# Multiline strings in javascript

in this elaborate syntax for multiline strings, every multiline string will be preceded with `(function(){/*` and a newline, and followed by a newline and `*/}+'').split('\n').slice(1,-1).join('\n')`.

using this amazing, intuitive syntax, we can finally use multiline strings:

```
var string = (function(){/*
THIS IS A MULTILINE STRING
HOORAY!!!
*/}+'').split('\n').slice(1,-1).join('\n');

console.log(string) // THIS IS A MULTILINE STRING
                    // HOORAY!!!

```

for people who don't like our simple syntax, we have a compiler to our fabulous new language:

```
function compile(code)
{
    return code.replace("#{", "(function(){/*").replace("}#", "*/}+'').split('\n').slice(1,-1).join('\n')")
}

```

the same example, in the compiled-language version:

```
var string = #{
THIS IS A MULTILINE STRING
HOORAY!!!
}#;
console.log(string) // THIS IS A MULTILINE STRING
                    // HOORAY!!!

```
> 7 votes

# Answer
# Goto in PostScript

My first thought was that I'd have to muck about with the exec stack, so this false start digs up the continuation operator for stopped from ghostscript (or xpost).

```
/_stopped_mark
{countexecstack array execstack dup length 2 sub get}
stopped pop def

```

But, it's simpler than that. Because file-position is the same for all duplicates of the file handle (`setfileposition` consumes its argument, so this is the only useful semantics for that function).

```
/LABELS 10 dict def

/: { % n :  define label
    LABELS exch currentfile fileposition put
} def

/goto { % goto label
    currentfile exch LABELS exch get setfileposition
} def

/x 0 def

/here :
    /x x 1 add def

    x 5 ne {
        /here goto
    } if

x =

```

It prints `5`.

There are some limitations with the above. The jump is not immediate, but happens when the if-body returns to the top-level and the interpreter is again reading from the file (instead of reading from the array that contains the if-body). At that point, the file has been repositioned and the 'goto' takes effect.
> 6 votes

# Answer
# Sliceable List in C# (like Python)

I always enjoyed python's slice notation and wish it was available in C#

Usage:

```
SliceList<int> list = new SliceList<int>() { 5, 6, 2, 3, 1, 6 };
var a = list["-1"];     // Grab the last element (6)
var b = list["-2:"];    // Grab the last two elements (1,6)
var c = list[":-2"];    // Grab all but the last two elements (5,6,2,3)
var d = list["::-1"];   // Reverse the list (6,1,3,2,6,5)
var e = list["::2"];    // Grab every second item (5,2,1)

```

Code, far from error proof:

```
public class SliceList<T> : List<T>
{
    public object this[string slice]
    {
        get
        {
            if (string.IsNullOrWhiteSpace(slice))
                return this.ToList();
            int[] values = { 0, Count, 1 };
            string[] data = slice.Split(':');
            for(int i = 0; i < data.Length; i++)
            {
                if (string.IsNullOrEmpty(data[i])) continue;
                int value;
                int.TryParse(data[i], out value);
                if(value < 0 && i < 2)
                    value += Count;
                values[i] = value;
            }
            if (data.Length == 1)
                return this[values[0]];
            int start = Math.Min(values[0], values[1]);
            int stop = Math.Max(values[0], values[1]);
            int step = values[2];
            int sign = Math.Sign(step);
            if (sign < 0)
            {
                var temp = start;
                start = stop-1;
                stop = temp-1;
            }

            SliceList<T> newList = new SliceList<T>();
            for (int i = start; i != stop; i += step)
                newList.Add(this[i]);

            return newList;
        }
    }
}

```
> 6 votes

# Answer
### Tcl

Tcl has no `do ... while` or `do ... until` so...

```
proc do {body op expr} {
    uplevel 1 $body
    switch -exact -- $op {
        while {
            while {[uplevel 1 [list expr $expr]} {
                uplevel 1 $body
            }
        }
        until {
            while {![uplevel 1 [list expr $expr]} {
                 uplevel 1 $body
            }
        }
    }
}

```

Example:

```
do {
    puts -nonewline "Are you sure? "
    flush stdout
    set result [gets stdin]
} while {[string is boolean -strict $result]}

```

`uplevel` executes a script in the callers scope.
> 5 votes

# Answer
## `Symbol#to_proc` with arguments in Ruby

`Symbol#to_proc` is probably one of my favorite tricks for writing really succinct Ruby code. Suppose you have

```
nums = [1, 2, 3, 4]
text = %w(this is a test)

```

and you want to convert the contents of `nums` and `text` to Floats and uppercase words, respectively. `Symbol#to_proc` allows you to shorten code like this:

```
nums.map { |num| num.to_f }
text.map { |word| word.upcase }

```

to this:

```
nums.map(&:to_f)
text.map(&:upcase)

```

Awesome! But what if we want to raise every element in `nums` to the `i`th power, or replace every occurrence of `s` with `*` in `text`? Is there any way to shorten code like this?

```
nums.map { |num| num ** 1i }
nums.map { |word| word.gsub('s', '*') }

```

Alas, there's no easy way to pass arguments when using `Symbol#to_proc`. I've seen it done multiple ways, but probably two of the most clever and usable involve monkey-patching the `Symbol` class \[, \]. I'll illustrate the first way below.

```
class Symbol
  def with(*args, &block)
    ->(caller, *rest) { caller.send(self, *rest, *args, &block) }
  end
end

```

Now you can do things like:

```
nums.map(&:**.with(1i))
text.map(&:gsub.with('s', '*'))
nums.take_while(&:<.with(3))
text.delete_if(&:[].with('is'))

```
> 4 votes

# Answer
# JavaScript foreach

```
var arr = ["Seattle", "WA", "New York", "NY", "Chicago", "IL"];

function foreach(fn, arr) {
  var s = fn.toString();
  var args = s.substring(s.indexOf('(')+1,s.indexOf(')')).split(",");
  var argsLen = args.length;
  var len = arr.length;
  for (var i = 0; i < len; i+=argsLen) {
    var part = arr.slice(i, i+argsLen);
    fn.apply(undefined, part);
  }
}

foreach (function(city, state) {
  console.log(city + ', ' + state);
}, arr);

```

Output

```
Seattle, WA
New York, NY
Chicago, IL

```

Alternate syntax, more like Tcl.

```
// Tcl's foreach loop for javascript.
// Keys in loop are prefixed with "this".
function tclForeach(keys, values, fn) {
  var obj={}, klen=keys.length, vlen=values.length, j, i;
  for (i=0, klen=keys.length; i < klen; i++) obj[keys[i]]=null;
  for(i = 0; i < vlen; i+=klen) {
    for(j=klen; j--;) obj[keys[j]] = values[i+j];
    fn.apply(obj);
  }
}

tclForeach(["city","state"], arr, function() {
  console.log(this.city + ', ' + this.state);
});

```
> 3 votes

# Answer
# Gotos in Haskell

the basic idea is that gotos can be partially simulated using the last statement in `do`-notations. for example:

```
main = do
  loop:
  print 3
  goto loop

```

is equivalent to

```
main = do
  loop
loop = do
  print 3
  loop

```

because execution will jump to the last statement, it is optimal to express gotos.

because the way it is done, gotos only jumps when they are at the `do` block of a toplevel definition directly. it is actually "call x and ignore the rest of the **lexically seen** statements" rather than "all x and ignore the rest of the statements", like a real goto.

the biggest problem is that when there is no way to leave execution from the middle of an IO action - even `return` doesn't; `return` does nothing when it isn't the last statement.

this overcomes this by capturing the rest of the statements by another `do` block.

```
goto loop
print 3

```

becomes

```
const loop $ do
print 3

```

the `print 3` statement is captured by the `do` block, so `loop` becomes the last statement.

this transformation also supports variables being present at the scope of the actions. this is done by remembering the variables which are in scope, and passing them into the actions. for example:

```
printer r = do
  loop:
  putStrLn r
  goto loop
  print "this isn't executed"

```

this simply translates into:

```
printer r = do
  loop r
loop = do
  putStrLn r
  const (loop r) $ do
  print "this isn't executed"

```

some notes:

also, a `return undefined` statement is added to ensure the capturing `do` block is not empty.

because sometimes there is type ambiguity in the capturing `do` block, instead of `const` we use `asTypeOf`, which is the same as `const` but requires both its parameters to have the same type.

**the actual implementation** (in javascript):

```
function makeGoto(code)
{
    var vars = [] // the known variables

    // add the arguments to the functions to scope
    code.split('\n')[0].split('=')[0].split(' ').slice(1).forEach(function(varname){vars.push(varname)})
    return code.replace(/([ \t]*)([a-zA-Z]+):|([ \t]*)goto[ \t]+([a-zA-Z]+)|[ \t]+([a-zA-Z]+)[ \t]*<-/gm, function match(match, labelSpaces, label, gotoSpaces, goto, x)
        {
            if (label != undefined)
                return labelSpaces+label+" "+vars.join(' ')+"\n"+label+" "+vars.join(' ')+"=do ";
            else if(goto != undefined)
                return gotoSpaces+"asTypeOf("+goto+" "+vars.join(' ')+")$do\n"+gotoSpaces+"return undefined";
            else
            {
                vars.push(x);
                return match
            }
        })
}

```

an exampe:

```
main = do
    putSrtLn "a"
    goto label
    putStrLn "b"
    label:
    putStrLn "c"

```

becomes:

```
main = do
    putStrLn "a"
    asTypeOf(label )$do
    return undefined
    putStrLn "b"
    label
label =do
    putStrLn "c"

```

output:

```
a
c

```
> 2 votes

# Answer
# Python Goto

### goto.py

```
import sys, re
globals_ = globals()
def setglobals(g):
    global globals_
    globals_ = g
def goto(l):
    global globals_
    with open(sys.argv[0], "rb") as f:
        data = f.read()
        data_ = data.split('\n')
    if isinstance(l, int):
        l-=1 if l > 0 else 0
    elif isinstance(l, str):
        r=re.search(r"^\s*(#{0}|(def|class)\s+{0})".format(l), data, re.MULTILINE)
        l=len(data_)-(data[r.start():].count("\n")) if r else len(data_)
    if 0 < l < len(data_) or 0 < (l*-1) <= len(data_):
        exec("".join(data_[l:]),globals_)
        sys.exit(1)

```

### Usage

```
setglobals(globals()) #Set the globals to be used in exec to this file's globals (if imports or other variables are needed)
goto(8) #Goto line 8
goto(-8)#Goto 8th last line
goto("label")#Goto first occurrence of #label
goto("funcName")#Goto definition of funcName
goto("className")#Goto definition of className

```

### Sample Test Case

```
import goto, sys
goto.goto(-1)
sys.exit(-1)

print "Asdf"

```

### Sample Test Case Output

```
Asdf

```

Just a little fun with exec(). Can raise a maximum recursion depth error if not used properly.
> 1 votes

# Answer
//import javascript without especifically using the script tag in a HTML page

```
function i(u) {
  document.write("script src=\" + u + \"></script>");
}

i("http://www.mysite.com/myscript.js");

```

It's lame yeah I know. Length: 99
> -2 votes

---
Tags: code-challenge, popularity-contest
---


# Question
Title: Pixel art, episode 2: Display the map 1-1 of Super Mario Bros
Last challenge (Pixel-art, episode 1: display Super Mario) was just a training... (and you completed it in amazing ways, thanks!)

This time, you have to work a little bit more. You have to display all the first overworld map of Super Mario bros on NES, without enemies, and without Mario.

Your program or function must display all the pixels of the following image OR produce an image file similar to it (BMP, PNG, or GIF).

Your program must not access the internet in any way.

The output can be zoomed if you want, and the pixels can be ASCII or HTML elements if you want, as long as they have the right color.

Here's the model you have to follow:

```
Sky blue: #5C94FC
Black: #000000
Pink: #FCBCB0 (for the blocks and the castle)
Brown: #C84C0C (for the blocks and the castle)
Orange: #FC9838 (for the "?" block)
Light green: #80D010 (for bushes, mountains, flagpole, warp)
Dark green: #00A800 (for bushes, mountains, flagpole, warp)
White: #FCFCFC (clouds)
Light blue: #3CBCFC (clouds)

```

Shortest answer wins.

EDIT: There will be two score boards, one where scores are counted in bytes and one where they are counted in characters.

Good luck!

P.S: Here are some notes that could help you optimizing your program:

* Clouds, bushes and mountains have a repeating pattern (every 48 columns)
* Flying blocks are only present on lines 4 and 8
* Each tile or sprite of the map uses at most 4 colors (including blue or transparent, depending on how you see it)
* Bushes are just "top of clouds" with a diferent color palette
* Single, double and triple bushes/clouds can easily be formed using the same mini set of 16x16px pieces. This is also true for the single and triple mountains

# Answer
# x86 Machine Code, 1729 1619 1468 1382 Bytes

How it works: Tiles are generated by a combination of RLE compression, 2bit images, and procedural code. Once the tiles are generated in memory, the program then creates a matrix of tile indexes. This is first loaded with the repeating background. After that, the pipes, floating blocks, pyramids, and the flag pole are drawn procedurally. The pipes, hills, bushes, and pyramids can extend below the ground, but are covered up when the rock tiles get written next. Finally, the castle tile values are simply copied into the correct location. To generate the image file, the BMP header and palette is stored in the file as data, and is written first. The program then runs through the matrix, writing the appropriate row from the corresponding tile for each position.

Usage: Run mario.com, it will generate "m.bmp", a standard BMP image file. The file is created as a hidden file, since that ended up being less bytes.

Download a ZIP file containing the source code and binary, plus output.

Assembly code to generate the executable file:

```
org 100h
code_start:

;xor    cx,cx
mov    cx,12+14+256*3
mov    dx,filename
mov    ah,0x3c
int    21h
mov    bx,ax
push   ax

;mov    dx,BITMAPFILEHEADER
inc    dx
inc    dx
mov    ah,0x40
int    21h

mov di,tiles
xor ax,ax
mov ch,48
rep stosw

cwd             ; load the cloud sprite
mov si,cloud
call    DrawSprite
call    DrawHill

mov dx,14*256+12*16
PipeTileLoop:                ;Pipe
mov si,pipe0
call    DrawSprite
sub dh,2
jns PipeTileLoop

mov dx,32+12*16
call    DrawSprite
mov dh,13
;mov     si,pipe2
call    DrawSprite

cwd
xor ax,ax                ; move to the tile array
mov cl,6
MoveTileLoop:
call    MoveTile
loop    MoveTileLoop
mov word [BITMAPFILEHEADER+5+2],0x607     ; bushes
sub dl,3*16
mov cl,13
jnc MoveTileLoop

mov dx,0x1004
mov si,ball
call    DrawSprite
mov dl,0
call    MoveTile

xor bx,bx       ; pole
pole:
mov word[bx+tiles+22*256+7],0x707
add bl,16;
jnc pole

mov word [BITMAPFILEHEADER+5+2],0x805      ; qbrick
mov si,qbrick
call    DrawSprite
call    MoveTile

mov byte[BITMAPFILEHEADER+5+3],4

mov al,1            ; bricks & castle
call    Clear
mov al,23
call    MoveTile        ; black door
mov dx,0x800+64
BrickLoop:
xor dh,8
mov si,bricks
call    DrawSprite
sub dl,dh
jg  BrickLoop

cwd
call    MoveTile         ; left window
call    MoveTile         ; castle brick
mov al,3
mov di,image+15*256+16
mov cl,16
rep stosb
mov al,26
mov dl,16
call    MoveTile         ; reg brick

;mov     dl,32
;mov     si,door
call    DrawSprite
;mov     dl,32
call    MoveTile        ; door top
mov dx,48 +8*256
;mov     si,cren
call    DrawSprite
push    si

mov dh,0
call    MoveTile        ; crenulation 1
mov si,image+48+256  +4
mov di,image+48+256*9+4
mov bl,7
CrenLoop:
mov cl,7
rep movsb
mov dl,256-7
add si,dx
add di,dx
dec bx
jnz CrenLoop
mov dl,3*16
call    MoveTile        ; crenulation 2

pop si
mov cl,3
jmp skip
LastLoop:
;call    MoveTile        ; right window
                 ; block
                 ; rocks
call    DrawSprite
skip:
call    MoveTile
loop    LastLoop

xor ax,ax              ; Begin drawing world using the tiles
call    Clear
mov bl,48*4
BackgroundLoop:
lea di,[bx+image+2*256]
SmallHill:
mov al,10
mov cl,5
HillRow0:
stosb
inc ax
loop    HillRow0
mov dword [di+256-4],10+11*256+256*256*14
mov byte [di+512-3],15
sub di,256-10
cmp di,image +256
ja  SmallHill

mov si,fluff         ; draw clouds, bushes
FluffDrawLoop:
lodsw
add ax,bx
add ax,image
xchg    ax,di
lodsb
aam
mov cl,ah
;call    DrawFluff;

mov ah,1
FluffLayer2:
stosb
inc ax
push    cx
rep stosb
inc ax
stosb
pop cx
add di,256-2
sub di,cx
inc ax
dec ah
jns FluffLayer2

cmp si,pa
jl  FluffDrawLoop

sub bl,48
jnc BackgroundLoop

;mov     si,pa
mov cl,6
PipePlace:
lodsw
xchg    bx,ax
mov di,image
mov word [bx+di],18+19*256
PipeLoop:
dec bh
mov word[bx+di],16+17*256
jnz PipeLoop
loop    PipePlace

;mov     si,dba          ; draw the various floating blocks
mov cl,4
DrawBlockLoop:
lodsw
xchg    ax,bx
mov ah,bl

cwd
lodsb
BlockLoop:
mov bl,al
mov byte [bx+di],ah
lodsb
cmp al,dh
mov dh,al
ja  BlockLoop

dec si
loop    DrawBlockLoop

mov bx,198+256*12
FlagPole:             ; Draw flag pole
mov byte [bx+di],22
dec bh
jg  FlagPole
inc si
;mov     si,pyr            ; pyramid blocks
mov cl,8
PyrMainLoop:
lodsw
shr ah,1
cwd
inc dx
jnc NoNeg
neg dx
NoNeg:
mov bx,ax
PyrDrawLoop:
mov byte [bx+di],32
dec bh
jnz PyrDrawLoop
add ax,dx
dec ah
jnz NoNeg
loop    PyrMainLoop

;mov     si,ground        ; ground blocks
;mov     di,image
mov bl,4
push    di
GroundLoop:
lodsb
xchg    ax,cx
mov al,33
rep stosb
lodsb
add di,ax
dec bx
jnz GroundLoop
push    si
inc ch
mov si,di
sub si,cx
rep movsb

pop si
pop di
;push    di
;mov     si,copy         ; Draw Castle, flag ball
mov  dl,6
CopyLoop:
lodsw
mov cx,ax
shr cx,12
and ah,15
xchg    bx,ax
CopyRun:
lodsb
mov [bx+di],al
inc bx
loop    CopyRun
dec dx
jnz CopyLoop;

xor ax,ax
xor bx,bx ; image y        ; write the image to file
pop dx
WriteTileStep:
WriteRowStep:
mov bl,0 ; image x
WriteStep:
pusha
mov ah,[bx+di]
mov bx,dx
add ax,tiles
xchg    ax,dx
mov cl,16
mov ah,0x40
int 21h
popa
inc bx
cmp bl,212
jb  WriteStep
add al,16
jnz WriteRowStep
inc bh
cmp bh,14
jl  WriteTileStep
;pop     bx
;mov     ah,0x3e
;int     21h

;ret

DrawSprite: ; dx=x,y, si=data
pusha
mov bp,dx
;cwd
mov di,temp
push    di
lodsw              ; w, h*2+flag
mov dl,al
shr ah,1
pushf
mul ah
xchg    bx,ax
popf
jc  BitMapped
RunLoop:
lodsb
mov cl,al
shr cx,2
sub bx,cx
rep stosb
jnz RunLoop
jmp MoveData
BitMapped:
BitMappedLoop:
lodsb
mov cl,4
BitMappedQuad:
stosb
shr ax,2
loop    BitMappedQuad
sub bx,4
jg  BitMappedLoop
MoveData:
mov bx,sp
mov [bx+4],si
pop si
mov bx,di
lea di,[bp+image]
mov dh,-1
MoveLoop:
mov cl,dl
rep movsb
sub di,dx
cmp si,bx
jl  MoveLoop
popa
ret

MoveTile: ; dx =x,y, bx=y,ax=t-1
inc ax
pusha
mov si,dx

xchg    al,ah
xchg    di,ax
mov ax,16
TileLoop:
mov cl,16
TileTrans:
mov bx,[si+image]
inc si
and bx,3
mov bl,[bx+BITMAPFILEHEADER+5]
mov [di+tiles],bl
inc di
loop    TileTrans
add si,256-16
dec ax
jnz TileLoop
popa
add dl,16
ret

Clear: ; al = value
mov di,image
mov ch,48
rep stosb
ret;

DrawHill:
pusha
xor bx,bx
mov al,3
HillLoop:
lea di,[bx+image+6*16]
inc byte [di]
inc di
mov cl,16*5-2
sub cl,bl
sub cl,bl
rep stosb
inc byte [di]
inc bx
inc bh
cmp bl,16
jl  HillLoop

mov si,spot
push    si
mov dx,32-7+256*8 + 6*16
call    DrawSprite
mov dl,32+17    + 6*16
pop si
call    DrawSprite
mov dx,5*16      + 6*16
call    DrawSprite
popa
ret

filename: db 'm','.'
BITMAPFILEHEADER: db 66,77,'P',0,12,0,1,2,3,0,26,3,0,0
BITMAPCOREHEADER: db 12,0,0,0,64,13,224,0,1,0,8,0
colors: db 252,148,92,0,0,0,252,252,252,252,188,60,176,188,252,12,76,200,0,168,0,16,208,128,56,152,252

;pal: db 0,1,2,3
cloud: db 88,32,224,5,114,5,228,5,122,5,224,5,122,5,228,9,106,5,6,5,240,102,5,4,5,240,5,86,5,10,5,252,5,78,5,4,5,6,5,252,4,13,66,5,8,5,92,13,16,17,136,5,14,7,42,5,100,9,14,5,4,9,18,9,4,9,120,5,14,11,14,7,18,5,96,5,26,5,34,5,10,9,116,5,26,7,22,5,84,13,18,11,18,15,34,9,108,5,38,5,6,5,84,5,22,27,6,23,38,108,5,34,5,4,5,88,5,10,19,14,19,18,7,34,5,108,9,26,5,92,5,10,7,10,7,26,7,54,5,120,5,18,5,92,5,10,7,46,7,46,5,128,17,56
spot: db 6,17,245,95,255,245,93,87,127,245,87,127,245,223
hilltop: db 16,3*2,0x9, 0x33, 0x9, 0x8, 0xD, 0x1B, 0xD, 0x1C, 0x19, 0x14
ball: db 8,17,80,5,244,31,253,127,253,127,249,127,249,127,228,31,80,5
pipe0: db 64,5,0x90,0xFA,0xAA,0xAE,0xFF,0xFF,0xBB,0x06,0xA9,0xAF,0xAA,0xEB,0xFF,0xFF,0xBB,0x6B,0x90,0xFA,0xAA,0xAE,0xFF,0xFF,0xAE,0x06,0xA9,0xAF,0xAA,0xEB,0xFF,0xFF,0xEF,0x6A
pipe1:db 32,4,8,113,8,129
pipe2:db 32,12,5,23,26,79,9,122,133
qbrick: db 16,33,85,85,85,85,254,255,255,127,222,255,245,119,254,191,246,127,254,191,254,127,254,255,245,127,254,191,246,127,254,191,86,127,254,215,106,127,254,218,107,127,254,218,107,127,254,90,233,127,254,171,250,127,222,255,255,119,254,255,255,127,168,170,170,42
bricks: db 8,16,33,14,5,30,5,30,5,18,33,30,5,30,5,30,5
door: db 16,32,0xFD, 0xFD, 0x89, 0x6, 0x39, 0xA, 0x39, 0x6, 0x41, 0xA, 0x31, 0x6, 0x5, 0xE, 0x29, 0xA, 0x5, 0x16, 0x19, 0x12, 0x5
cren: db 16,17,213,255,255,85,234,0,192,170,234,0,192,170,234,0,192,170,234,0,192,170,234,0,192,170,234,0,192,170,255,0,192,255
block: db 16,32,61,6,7,53,6,5,11,45,6,9,15,37,6,13,19,34,17,19,34,17,19,34,17,19,34,17,19,34,17,19,34,17,19,34,17,19,34,17,15,6,35,17,11,6,43,13,7,6,51,9,6,59,5
rocks: db 16,33,86,149,87,149,171,106,171,90,171,127,171,106,251,85,171,106,95,170,173,106,165,170,173,106,171,170,182,106,171,170,182,106,171,170,182,106,171,170,246,127,171,170,102,149,171,170,118,106,171,170,182,106,171,170,182,106,171,170,182,106,254,255,231,191
fluff: db 8,10,11,     19,11,11,       27,10,31,       36,11,21,    11,1,34,    23,1,14,       41,1,24
pa: db 28,3,38,4,46,5,57,5,163,3,179,3
dba:db 21,5
qb: db 16,21,23,78,106,109,112,170, 21,9
qb2: db 22,94,109,129,130, 27,5
ba: db 20,22,24,77,79,94,100,101,118,129,130,168,169,171  , 27,9
ba2: db 80,81,82,83,84,85,86,87,91,92,93,121,122,123,128,131,0
pyr: db 137,5*2+1,140,5*2,151,5*2+1,152,5*2+1,155,5*2,188,9*2+1,189,9*2+1,198,4
ground: db 69,2,86-69-2,3,153-86-3,2,256-153-2,0
copy: db 202,2+5*16,26,26,24,26,26,   202,3+5*16,26,26,28,26,26,    202,4+5*16,29,30,30,30,29,   203,5+3*16,31,26,25,  203,6+3*16,29,29,29, 198,12+16,20
temp: rb 256*48
tiles: rb 34*16*16
image:

```
> 15 votes

# Answer
# Javascript minified (\*): 1285 1258 1253 1205 1186 1171 characters

(\*) Minified using Closure, RegPack and ObfuscaTweet, as suggested by xem

Unicode version is 4549 bytes in size, without ObfuscaTweet (Closure and Regpack only), size is 2251 bytes.

History:

1285 -\> 1258: variable `A` for 48 (thx @hsl), merged some of the `for` loops, merged `mt()` and `mu()`, using tile indices instead of tile strings, optimized png with PNGOUT

1258 -\> 1253: merged some more `for` loops; renamed `mt()` to `r()`; removed unnecessary braces; variable `B` for 16; defining 16 unused CSS sprites (replaces 32 with `A`); showing 1 unused row (replaces 14 with `B`); removed function `e()`; shortened `t()`, `g()`, `c()`; using `for(i=0;i<n;)f(i++)` instead of `for(i=0;i<n;i++)f(i)` where possible

1253 -\> 1205: moved body style to CSS part instead of `<body style=...>`; replaced some `for` loops with `f` calls; optimized functions `r`,`q`; `</head><body>` seems to be unnecessary, `<html><head>` too; function `t(i)` for CSS mapping removed; CSS names `b0`..`b31` instead of `a`..`z`,`aa`..`ff`

1205 -\> 1186: function `n` renamed to `N`; new function `n` that operates on an array with delta coding

1186 -\> 1171: hills and warps can be drawn "big" anytime, the lower parts get overdrawn by stone blocks; use `d` for both clouds and bushes; removed some unnecessary semicolons

This is a procedural attempt. There are patterns everywhere, one of the magic numbers is 48 (tile gap between clouds, bushes and mountains). The tileset is encoded as Base64 data url string and used as a CSS stylesheet. In Javascript, the 212x14 array `m` is filled with tile indices. See the commented unminified version for more details.

Works in Chrome 38 (Ctrl+T for new tab, Ctrl+Shift+J for javascript console, paste code there) and Firefox 33 (if wrapped with HTML javascript tags). There's a JS bin version, too.

There's still some room for optimizations, I'll post updates and it would be nice if some JS/CSS/HTML people could suggest optimizations/corrections.

Minified:

```
eval(unescape(escape('').replace(/uD./g,'')))

```

Unminified and commented:

```
// map width
W=212;
// helper constants
A=48;B=16;
// array containing the map, generated at runtime
m=[];
function w(s){document.write(s)}
// set a map tile
function N(i,s){m[i]="b"+s.toString()}
function n(i,s){z=0;for($ in i){z+=i[$];N(z,s)}}
// set a row of map tiles (index a..a+b)
function f(a,b,s){for(k=a;k<=a+b;)N(k++,s);}
// set a block of map tiles (index a and b define upper left and lower right corner)
function g(i,j,s){for(x=i%W;x<=j%W;x++)for(y=i/W|0;y<j/W|0;)N(y++*W+x,s)}
// helper for clouds and bushes - j=1..3 sets map tiles to XYZ, XYYZ or XYYYZ
function c(i,j,a){N(i,a);f(i+1,j-1,a+1);N(i+j+1,a+2)}
// clouds/bushes
function d(i,j,a){c(i,j,a);c(i+W,j,a+16)}
// hill
function p(i){N(i+2,8);n([i+213,211],23);n([i+214,211,2],24);n([i+215,213],25);N(i+426,9)}
// warps
function q(i){N(i,10);N(i+1,11);l(i+W,3,26);l(i+213,3,27)}
// stairs - d=1 for upwards, d=0 for downwards
function r(i,d){for(j=0;j<4;j++)f(i+d*(3-j)+j*W,j,0);}
// set a column of map tiles (index i, height j)
function l(i,j,s){for(k=0;k<j;k++)N(i+k*W,s);}
// fill map with sky tiles
f(0,2967,7);
// clouds and bushes - some of the bushes will be overdrawn by blocks and the castle, so draw them first
for(i=0;i<4;i++){d(231+i*A,1,4);d(248+i*A,2,4);d(451+i*A,3,4);d(432+i*A,1,4);d(2343+i*A,3,17);d(2355+i*A,1,17);d(2373+i*A,2,17)}d(624,1,4);N(2539,19)
// '?' blocks
n([870,72,15,20,1,734,5,2,55,28,3,3,58],3)
// brick blocks and castle
f(928,7,2);f(939,2,2);f(969,2,2);n([976,3,737,2,2,53,2,15,6,18,11,1,38,1,2],2);N(1900,B);g(2322,2535,B);g(2325,2538,B);f(1687,2,13);N(1899,29);N(1901,31);f(2110,4,13);f(2111,2,15);N(2324,14);N(2536,30)
// big and small hills - one of the big hills will be overdrawn by the stairs, the small hills are big hills overdrawn by stone blocks
for(i=0;i<5;i++){p(1908+i*A);p(2135+i*A)}
// warps (often overdrawn by stone blocks), stairs and block below pole
q(1742);q(1753);q(1946);q(2148);q(2283);q(2299);r(1830,1);r(1844,1);r(1877,1);r(1033,1);r(1836,0);r(1851,0);g(1881,2520,0);l(1037,4,0);l(1848,4,0);l(1885,4,0)
// pole
N(410,12);l(622,9,28);N(2530,0)
// lower row of stone blocks
f(2544,423,1);
// gaps in stone blocks
g(2613,2826,7);g(2630,2844,7);g(2697,2910,7)
// HTML start and CSS spritesheet base (tileset PNG encoded as base64)
w("<style>body{width:3400px}div{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAAgBAMAAAAPh7vXAAAAG1BMVEVclPwAAAAAqADITAyA0BD8/Pz8vLD8mDg8vPyQWLLUAAACwklEQVR4XrWWS4rjMBCGXcxjrfKje23hA4wRJNtJ8AEmi4JsTUD7phd9gkCOPSWN5OiRttsx8xGJ2CKpj99SJcVOW8bdv+miHd+U5VfxRaQDE/y61jz91FqpHrFSPb/rFRctXEW8KF668E31b+w/LG+BAOIXBOoh4uDX1c4ImCLIVFxcmaEKbSMYc4FXWz8QACKxLFBKyQLS02YCaAkEbAQ4CaBj//GuVJgANESnryRgBLqjZLquyxJAh9rx9GIEtGKcwKh6n8H+Q2G1DwQaYgFYJSCP+SPo0VKZ+HkkAj1WgcDre58IUCO2JLAsoPU4J3DmDLYmgI5MIN8Db68qEAA6X+lKp2cTwBfN4IS/DgSyY/je412g4er0RAL++PrvRYe/nheYTgEgOQSsTQCOUiwLWKUXFTcinAQa8ohGrEygNt1gWUAjCwSGJoG9FwCaOJFYlYAXwE8o9IhMjxfbjII+ED6CHxQgxLoESivgs0WHvy40GioursdIgFsxulYMDQWcko0IiGImgSWBES09FzdjWgh/jBqKiAXQFMCZX0OQUswIuOuq5zHywIQlAXR1paPIP25n/IRpd0aZiiRkighXQVpaOd20z+A4BI3w+2/XBzDB3HskUPPnYpozGc55AKULoGwfCHTdUwLImyiL4GoUrlkCIL2AFE8mgI57sYERIo3gdqXbjR3iCMq7QDufAH7CtDtjgcMhESAufzuzAvMn2wElsomYTSA8AeFJeCzQpQ8BkA34ZQMQaQBt2Zp5JoEVAjbCoxHIMjgR0wT1QXoBaRDPJJDtAWB7JhOAhgQSIQQLZSzQrt8DeR/Aevo/nSgApn/MQSaI7X0ABs8B2H6eUia02/tAPUzUSwIgM8TmPjAEHGBdAEy7tQ/gEFKLdQEwYlsfgCFGrAuAaZME/qMAyIeIbX2gjgQOawNg2rV74C9KQ+xyNDQoswAAAABJRU5ErkJggg==)}")
// generate CSS classes 'b0'..'b31' for the 32 used tiles
// this actually generates 16 additional unused classes to reuse the constant A and save 1 byte
for(i=0;i<A;i++){j=(i%B)*-B;k=i>15?-B:0;w(".b"+i.toString()+"{background-position:"+j+"px "+k+"px;width:16px;height:16px;float:left;}")}
// some more HTML
w("</style>")
// body of HTML contains all tiles as <div> elements arranged in <span> columns
for(x=0;x<W;x++){w("<span class=b0>");for(y=0;y<B;){w("<div class="+m[y++*W+x]+"></div>")}w("</span>")}

```
> 13 votes

# Answer
# Javascript, ~~1069~~ ~~1072~~ 1024 characters (1957 bytes)

## RegPacked and Obfuscatweeted

```
eval(unescape(escape('').replace(/uD./g,'')))

```

## Unobfuscated code

I hid this code because there's ungolfed code below.

```
function R(s,t){return new Array(t+1).join(s)}
function b(n){return"<div style='background: url(data:;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAAgBAMAAAAPh7vXAAAAG1BMVEVclPwAAAAAqADITAyA0BD8/Pz8vLD8mDg8vPyQWLLUAAACwklEQVR4XrWWS4rjMBCGXcxjrfKje23hA4wRJNtJ8AEmi4JsTUD7phd9gkCOPSWN5OiRttsx8xGJ2CKpj99SJcVOW8bdv+miHd+U5VfxRaQDE/y61jz91FqpHrFSPb/rFRctXEW8KF668E31b+w/LG+BAOIXBOoh4uDX1c4ImCLIVFxcmaEKbSMYc4FXWz8QACKxLFBKyQLS02YCaAkEbAQ4CaBj//GuVJgANESnryRgBLqjZLquyxJAh9rx9GIEtGKcwKh6n8H+Q2G1DwQaYgFYJSCP+SPo0VKZ+HkkAj1WgcDre58IUCO2JLAsoPU4J3DmDLYmgI5MIN8Db68qEAA6X+lKp2cTwBfN4IS/DgSyY/je412g4er0RAL++PrvRYe/nheYTgEgOQSsTQCOUiwLWKUXFTcinAQa8ohGrEygNt1gWUAjCwSGJoG9FwCaOJFYlYAXwE8o9IhMjxfbjII+ED6CHxQgxLoESivgs0WHvy40GioursdIgFsxulYMDQWcko0IiGImgSWBES09FzdjWgh/jBqKiAXQFMCZX0OQUswIuOuq5zHywIQlAXR1paPIP25n/IRpd0aZiiRkighXQVpaOd20z+A4BI3w+2/XBzDB3HskUPPnYpozGc55AKULoGwfCHTdUwLImyiL4GoUrlkCIL2AFE8mgI57sYERIo3gdqXbjR3iCMq7QDufAH7CtDtjgcMhESAufzuzAvMn2wElsomYTSA8AeFJeCzQpQ8BkA34ZQMQaQBt2Zp5JoEVAjbCoxHIMjgR0wT1QXoBaRDPJJDtAWB7JhOAhgQSIQQLZSzQrt8DeR/Aevo/nSgApn/MQSaI7X0ABs8B2H6eUia02/tAPUzUSwIgM8TmPjAEHGBdAEy7tQ/gEFKLdQEwYlsfgCFGrAuAaZME/qMAyIeIbX2gjgQOawNg2rV74C9KQ+xyNDQoswAAAABJRU5ErkJ)-"+(n%16)*16+"px -"+(n/16|0)*16+"px;width:16px;height:16px;float:left}'></div>"}
function M(s){m="";for(_ in s)m+=b(parseInt(s[_],32));return m}
function C(s,a){c="";for($ in a)c+=$%2?M(a[$]):R(s,a[$]);return c}
O="<body style='width:3392px'>";
O+=R(s=b(7),212);
O+=R(C(s,[19,"456",14,"4556",8]),4);
O+=C(s,[6,"c",13]);
O+=R(C(s,[8,"456",8,"klm77777455567777kllm",8]),4);
O+=C(s,[6,"s7456",9]);
O+=R(C(s,[8,"klm",16,"klllm",16]),4);
O+=C(s,[6,"s7klm",31,"3",57,"222222227772223",14,"3",11,"22277772332",56,"00",8,"s",200,"000",8,"s",199,"0000",8,"s",198,"00000",8,"s7777ddd",22,"377723232",21,"ab",9,"ab",18,"232",14,"2777772777773773773777772",10,"22",6,"0770",10,"00770",12,"2232",12,"000000",8,"s7777tgv",8,"8",35,"ab",6,"qr778",6,"qr",39,"8",37,"007700777787770007700",26,"000000077778777s777dfffd",6,"nop",13,"8",10,"ab",8,"qr",6,"qr7nop",5,"qr",6,"8",31,"nop",13,"8",21,"0007700077nop700007700077787ab",14,"ab700000000777nop77s777ggegg77877no9op",6,"hiiijnop7777hij77qr",8,"qr7hiij7qrno9op7777qrhiiijnop7777hij",15,"hiij777no9op",6,"hiiijnop",4,"hij",12,"0000ii0000no9o00000770000jnopqr77hij",9,"qr00000000077no9op70777gguggjnop7"]);
O+=R(C(b(1),[69,"77",15,"777",64,"77",57]),2);
document.write(O);
```

## Ungolfed code

```
function repeat(s, t) { var S=s; while(t-- > 1) S += s; return S; }
function block(n) { return "<div style='background: url(data:;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAAgBAMAAAAPh7vXAAAAG1BMVEVclPwAAAAAqADITAyA0BD8/Pz8vLD8mDg8vPyQWLLUAAACwklEQVR4XrWWS4rjMBCGXcxjrfKje23hA4wRJNtJ8AEmi4JsTUD7phd9gkCOPSWN5OiRttsx8xGJ2CKpj99SJcVOW8bdv+miHd+U5VfxRaQDE/y61jz91FqpHrFSPb/rFRctXEW8KF668E31b+w/LG+BAOIXBOoh4uDX1c4ImCLIVFxcmaEKbSMYc4FXWz8QACKxLFBKyQLS02YCaAkEbAQ4CaBj//GuVJgANESnryRgBLqjZLquyxJAh9rx9GIEtGKcwKh6n8H+Q2G1DwQaYgFYJSCP+SPo0VKZ+HkkAj1WgcDre58IUCO2JLAsoPU4J3DmDLYmgI5MIN8Db68qEAA6X+lKp2cTwBfN4IS/DgSyY/je412g4er0RAL++PrvRYe/nheYTgEgOQSsTQCOUiwLWKUXFTcinAQa8ohGrEygNt1gWUAjCwSGJoG9FwCaOJFYlYAXwE8o9IhMjxfbjII+ED6CHxQgxLoESivgs0WHvy40GioursdIgFsxulYMDQWcko0IiGImgSWBES09FzdjWgh/jBqKiAXQFMCZX0OQUswIuOuq5zHywIQlAXR1paPIP25n/IRpd0aZiiRkighXQVpaOd20z+A4BI3w+2/XBzDB3HskUPPnYpozGc55AKULoGwfCHTdUwLImyiL4GoUrlkCIL2AFE8mgI57sYERIo3gdqXbjR3iCMq7QDufAH7CtDtjgcMhESAufzuzAvMn2wElsomYTSA8AeFJeCzQpQ8BkA34ZQMQaQBt2Zp5JoEVAjbCoxHIMjgR0wT1QXoBaRDPJJDtAWB7JhOAhgQSIQQLZSzQrt8DeR/Aevo/nSgApn/MQSaI7X0ABs8B2H6eUia02/tAPUzUSwIgM8TmPjAEHGBdAEy7tQ/gEFKLdQEwYlsfgCFGrAuAaZME/qMAyIeIbX2gjgQOawNg2rV74C9KQ+xyNDQoswAAAABJRU5ErkJ)-" + (n%16) * 16 + "px -" + (n/16|0) * 16 + "px; width: 16px; height: 16px; float: left; }'></div>"; }
function blocks(s) { var S=""; for(var i in s) S += block(parseInt(s[i], 32)); return S; }
var output = "<body style=width:3392px>";
output += repeat(block(7), 212);
output += repeat(blocks("777777777777777777745677777777777777455677777777"), 4);
output += blocks("777777c7777777777777");
output += repeat(blocks("7777777745677777777klm77777455567777kllm77777777"), 4);
output += blocks("777777s7456777777777");
output += repeat(blocks("77777777klm7777777777777777klllm7777777777777777"), 4);
output += blocks("777777s7klm777777777");
output += blocks("777777777777777777777737777777777777777777777777777777777777777777777777777777772222222277722237777777777777737777777777722277772332777777777777777777777777777777777777777777777777777777770077777777s7777777777777");
output += blocks("777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777700077777777s7777777777777");
output += blocks("777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777000077777777s7777777777777");
output += blocks("777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777770000077777777s7777ddd777777");
output += blocks("7777777777777777377723232777777777777777777777ab777777777ab7777777777777777772327777777777777727777727777737737737777727777777777227777770770777777777700770777777777777223277777777777700000077777777s7777tgv777777");
output += blocks("77877777777777777777777777777777777777ab777777qr778777777qr7777777777777777777777777777777777777778777777777777777777777777777777777777700770077778777000770077777777777777777777777777000000077778777s777dfffd77777");
output += blocks("7nop777777777777787777777777ab77777777qr777777qr7nop77777qr77777787777777777777777777777777777777nop777777777777787777777777777777777770007700077nop700007700077787ab77777777777777ab700000000777nop77s777ggegg77877");
output += blocks("no9op777777hiiijnop7777hij77qr77777777qr7hiij7qrno9op7777qrhiiijnop7777hij777777777777777hiij777no9op777777hiiijnop7777hij7777777777770000ii0000no9o00000770000jnopqr77hij777777777qr00000000077no9op70777gguggjnop7");
output += repeat( repeat(block(1), 69) + blocks("77") + repeat(block(1), 15) + blocks("777") + repeat(block(1), 64) + blocks("77") + repeat(block(1), 57), 2);
document.write(output);
```

I first made tiles into sprites with @schnaader's minified data URI. `0 ~ v` (which is 0 ~ 31 in base 31) represents each tile.

And I converted map to tiles by hand. This data has 212 characters per row.

Then I replaced repeating character (like `7`(sky) and `1`(ground)) with `repeat()`.

I found `7`s and some blocks, and another `7` pattern is repeated. So I made another function to make it compact. You can see it at unobfuscated source code.

Finally I RegPacked, and Obfuscatweeted my 2341 bytes golfed code.

It was very funny challenge. Thanks! And thanks to @xem for more tricks.
> 8 votes

# Answer
# Python3 ~~1638~~ ~~1576~~ ~~1616~~ 1513 bytes

## 581 code + 932 data

↑ This top is the original level. In the middle is the PPM generated by this script. At the bottom is a diff, and this shows that the palette and the map do not agree exactly! I put this down to data glitch and not my script ;)

```
# 759808 is 212*14 tiles at 16*16 each - the size of the map
import lzma;B,E,R,X=bytearray,759808,range,16
D=B(lzma.decompress(open("i","rb").read())+B(E)) # put canvas on end of data array
i,P=8218,lambda x,y:8622+y*54272+x*X # 8622 is length of decompressed data
def Q(s,n,d): # copies a 16x16 tile from s to d
 # n is the stride for the source s, which is different for put and copy
 for m in R(0,256,X):e=d+m*212;D[e:e+X]=D[s+m*n:s+m*n+X]
for j in R(4): # there are 4 command sections
 j&=1;i+=1+j;r=D[i-1]<<8 # j is now truthy if this is a copy buffer
 for k in R(D[i]): # this many commands
  a,b,w,h,c,d=D[i+1:i+7];q=h>32 # unpack it; put doesn't use all these
  # do the put/copy
  for z in R(((a,h-32)[q],w*h)[j]):Q((D[(i+4+z,i+5)[q]]<<8,P(a+z%w,b+z//w))[j],1+211*j,
      (P(b+z,w),P(c+(z%w,w-z%w-4)[r&1],d+z//w))[j])
  i+=((3+a,5)[q],6)[j&1];r>>=1 # move i to next command
for y in R(2,11):Q(28<<8,1,P(198,y)) # special case flagpole
o=open("o","wb");o.write(b"P6 3392 224 255 ") # header for PPM
for c in D[8622:]:o.write(D[8192+c*3:][:3]) # decompress palette to RGB

```

(bytes counted without comments, line-unwrapped and second level indent being `\t`)

Data (base64-encoded to be pasted; decode and save as a file named "i"):

```
/Td6WFoAAATm1rRGAgAhARwAAAAQz1jM4CGtA2JdAACA3BlsBrac0zEPcSPZ/qSCuw0fuG0WYiRk
w+pNfoBEsFJBEXgxJ73nEPREKmuJUHyg+jhBTQcbUI0gChatJ7D6ZqYmLz4P34OFSxUMWF+aGhm2
+8mNqw264w0JIjW9ogeNpJX6pgH/xJ/D2qlfqfCuz0cOR1sPAAcMrue1y9joUaDauFLj8QhbExen
lQkUMfMePbMnn4nJ0aVXBeHdKIwSeFQdFz/Aj695kKi0x8+MXR84cpNh3U8y7deiyjbNyIgu7vLn
WBxzXhEtrV9svBETfQpxj2qYI11ALgWlhoxdoABE3ocVgE1Qo35dnoJe+1DJFvqIzFOoxSJEckUk
T89RIjARdA/Ax6UTiitivZp4EhM+xczasrm+2XFy68mA96xmEMKp9ZpoApNIU0ShMMW1Im0QFeuS
aCl43qJuintArMAeDiOlmkbDNzMzHZIgDFeklUnnKG9xHXPQXNQVyJ9I7z+rLHh1VtuXDyAErKTL
VY/Xai2pIUlSt5H1KGEExdHu3O17XKbar11Jad6nFjDKOZFwamJQTFIG6dru2UXVJq29pGCnKLlB
lOmls4agigMI7QJ5K/Rzf15ZuIQ7U9MKJ3E4llbwqs6vDk+Ln/ceRVfl5Fm/Ne056ZLDp6VIf5zs
EMWra8UnjndkigqAyp8D62AcrPFIp9EdJmgiGzfhNCQ1JptRN/z01mNomtwVJfqeLTqgvRgXXG3P
8p+6s1rjEfblMBNyMxdYvJZXLE0wgu4xhH3fiNTT+RyTIJ1MZZ1zhksfe2SnNTbL2JYGven1WPfI
YP+M4NI/vpQ4bGsuJ8w//x01GpkTLfMZtZgBhx0dx1Wu9i0JY9hhtoI2/tGR4sNHfpSs7xD/Zz+7
6Z18z3AQn4dC7Trp0WndI/zS8eRZlRgKxA/lb09MZ87ugbXnm2+oBQFSLEtg/rqg398pDWx9W4Zk
6GeRTqvp9CJ9meMagUnGHKnAj+05PE0XgEXO6r9B8ltgoHf0+wXFevBHVupfiH9MApUxd0TPzv16
Y5Rfjxao0wLcIbxKMd3vtI1rCvOdq0OysWNrtcH9EBKjx9+CnGFBFZI9w9kCMJ8ZT0m9nsNYPelL
29KbQMt+K213+7o0MS3DxeGTHSW9CiEOTzNeqvsXN6IcIxjJIkgH8MuAAAAA5PpImPBeeH8AAf4G
rkMAALsE5byxxGf7AgAAAAAEWVo=

```

The data file is a custom format I made for this problem and is stored as LZMA.

First all 32 tiles are serialized. There are 9 colours in the palette, and this takes 8219 bytes uncompressed. (I found that trying to compress the tiles at 4-bits-per-pixel didn't help compression at all. I did not try and brute-force the best ordering of the tiles, and I probably lose a few points here.)

There are 212x14=2968 blocks making up the map.

Then the instructions to recreate the map are now encoded.

First comes a section of "put" commands that place some sequence of tiles from the tile-palette on the map at a particular x,y. As there are 32 tiles, I specify a run of the same tile using a number bigger than 32 rather than the tile index.

Then comes a section of "copy" commands that copy some rectangle of the current map to some other place. There is a special bit-mask that marks if the copy should be mirrored.

A short example command buffer:

```
16, # there are 16 put commands
3, # place 3 tiles
8,2, # dest x, y
4,5,6, # these three tiles
3, # place 3 tiles
8,3, # dest x, y
20,21,22, # these three tiles
5, # place 4 tiles
27,2, # dest x, y
4,5,5,5,6, # these five
...,
8, # bits set if copy reversed (high 8 bits of a uint16 mask)
14, # there are 14 copy commands
8,2, # src x, y
3,2, # w, h to copy
19,1, # dest x, y
1,9, # src x, y
3,2, # w, h to copy
16,10, # dest x, y
...,
13, # there are 13 put commands
...
7, # bits set if copy reversed (high 8 bits of a uint16 mask)
15, # there are 15 copy commands
...

```

(I said *short*, but that's actually nearly half the total command buffer needed to make the whole map; the vast majority of the bytes in the data are the 32 source tiles themselves)

Then comes a second section of "put" commands and finally another section of "copy" commands.

Because these can overwrite each other, I can build copy parts that I later erase or change.

I can doubtless shave a few more bytes off it by - for example - turning *puts* into small *copies* and by doing the `eval` trick on a gzipped source-code, or playing with PNG (which is DEFLATE with image-specific filters) too. But I like things verbose as they are.
> 7 votes

# Answer
# JavaScript: 3620 *(ouch)* 3429 3411

## Updates

* **Update #1:** Removed `var` definitions and put the variable declarations inside brackets of first use. Removed `getElementById()` as it is also available on load as a varaible by ID. Using `cloneNode()` instead of `createElement('CANVAS')`. Renamed *main* from `xMx` to `M`. Removed the scaling feature :(, *(still available in the example.*)

Added some comments to the expanded code. (That code is not updated with the removals. The line below, *("Mini code")*, is.)
* **Update #2:** Removed the main function `M()` as a whole and let the code run at *root*. This would require the code be placed inside a load wrapper or at end of document.
* **Update #3:** Added statistics.

***Mini code:***

```
K=126,f=['#000','#c84c0c','#fc9838','#00a800','#80d010','#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y]4Fa5HWqb2s3s4l>5gA6Nv8k>3W]4Y]5Q]4Ha5DWq>xQc6Lc7W]3Da4FX5HQq@xWc6Nc8I]3Pa4HX5DQq~19Qc9DPhDDhDHhDFhDEhDChDBhDJqu19mqE%Uf@U`>@UJ?@U`?@Uf@U`>@UCXUBP3qF%U@PEUBH3qH%UNIqD%UNAqP%UBQG3bUEQ&&';'B%?KAG@B@@p"%2_>>AC%]>j>H>G2e%O2O1DAY>I>W>6GA9Q>J>:K:%?A?%P>E1E>A>?>A?F>H1D>P1C>20B>@AF1D19,&GC1@>B1%?@1?17,&,6,5,8,4,3,>12,0,1,'.match(/../g).forEach(function(v){X=X[u='split'](String.fromCharCode(K--))[U='join'](v);if(K==92)K=91});eval(X[u]('*')[U]('f')[u](')')[U]('d')[u]('(')[U]('b')[u]('&')[U](']')[u]('%')[U]('[')[u]('#')[U]('}')[u]('!')[U]('{'));function e(k,d,i){for(s[Y]=k,i=0;i<d[L];)s.rect[G](s,d.slice(i,i+=4))}function _(d,i,q){for(S.width=R,i=0;i<d[L];++i,s.fill(),s.beginPath())F[q=d[++i][0]]?F[q](d[i]):e(f[d[i-1]],d[i])}F={$:function(d){_(Å[d[1]].concat(d.slice(2)))},b:function(d){_(Å[d[1]].slice().map(function(i,x){return(x=d[2].indexOf(i))>-1?d[2][++x]:i}))},f:function(d,i){for(s[Y]=f[d[1]],s.moveTo[G](s,d[2]),i=3;i<d[L];++i)s.lineTo[G](s,d[i])},d:function(d,x,y,g){for(s[Y]=f[d[1]],g=d[2],y=g>0?0:15,x=0;x<R;++x,y+=g)s.rect(x,y,1,1)}};R=16;c.width=3392;c.height=224;;S=c.cloneNode();c=c[G='getContext']('2d');s=S[G]('2d');c[Y='fillStyle']='#5c94fc';c.rect[G='apply'](c,[0,0,3392,224]);c.fill();for(k=0;k<Ø[L='length'];++k)for(_(Å[Ø[k]]),t=Ø[++k],i=0;i<t[L];i+=3)for(j=0;j<t[i+2]*R;j+=R)c.drawImage(S,t[i]*R+j,t[i+1]*R)

```

---

## Blah blah:

*– Demo at end of post.*

Using *canvas* as base for an attempt to solve this. Ended up with about 6000 chars, but after some fiddling (and a custom compression - with fiddling and tweaks on that as well) I'm now at the stated number. Still high, but then again the *quality is good ;-).*

It also includes a scale option, first argument to the `xMx()`, aka *"main"* function. 1 = normal size (as in 16 bit tiles). There is not much room for tweaking, so if one use fractions some of the tiles does not fit best together. At whole numbers it should be OK. <sup>\[1\]</sup>

But: warning, going up, quickly eats resources and makes for a huge canvas. (It is all painted in one go.) When the original width is 3392 pixels it quickly becomes huge. <sup>\[1\]</sup>

<sup>*\[1\] As of update #1 this has been removed. It is present in the demo.*</sup>

---

## Stats:

* `Main code          　:  870`
* *`Compression:`*
    + `Main data       : 2176 (17,480 bits)`
    + `Key             :  128 ( 1,024 bits)`
    + `Code            :  236`
    + `Whole she bang : 2540`
    + `Decompressed data : 5608 (44,864 bits)`
* `Total  　: 3410` <sub>\[1\]</sub>

<sub>*\[1\]: +1 byte, ";", between the main / data code.*</sub>

The compressed data is one array, Å, and one *"object"*, Ø. Where Å is holding the tile placement and the Ø holding the data for painting the tiles. The Ø should have been an array as well – but as my code started out with something like this:

```
var tiles = {
   brick: [
        ...
   ],
   qmark: [
        ...
   ],
};

```

It ended up as is. I'll perhaps see if I can't fix this. Ideally I would have made it parseable with JSON instead of eval(), but that would not be a point in a contest like this. As it is now it can not be JSON parsed due to missing quotes around the id parts.

---

## Demo

Here is a fiddle where one can view the code in action. The code in the fiddle I have expanded somewhat (And added some HTML / CSS styling *around* the world – but the image of itself is self sustained.):

### *MARIOS WORLD*

---

## Expanded code:

Expanded and (a bit) rearranged code:

```
var D, C, c, S, s,   // Document, canvas and shadow canvas.
    R, F, Z, X,
    Ø, Å,            // Due to the compression we have a couple of non-ascii
    K,               // variable names.
    L = 'length',
    Y = 'fillStyle',
    u = 'split',
    U = 'join',
    f = [
        '#000',    '#c84c0c', '#fc9838', '#00a800',
        '#80d010', '#fcbcb0', '#3cbcfc', '#fcf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3`?L4I?L4W?N5`?L8J?Q3FPJ4?PJ5?PL5EPJ8BPY3Hm4?m5>DJ5Em8CDR8'+
'EH`8FFN8HEL8DC2G1Pxg>3g>4R>>5lA6Lv7s3Q]4R]5Y]4Fa5HWqb2s3s4l>5gA6Nv8k>3W]4Y]5Q]'+
'4Ha5DWq>xQc6Lc7W]3Da4FX5HQq@xWc6Nc8I]3Pa4HX5DQq~19Qc9DPhDDhDHhDFhDEhDChDBhDJqu'+
'19mqE%Uf@U`>@UJ?@U`?@Uf@U`>@UCXUBP3qF%U@PEUBH3qH%UNIqD%UNAqP%UBQG3bUEQ&&';

K = 126;  // Key pos (char) counting down.
('B%?KAG@B@@p"%2_>>AC%]>j>H>G2e%O2O1DAY>I>W>6GA9Q>J>:K:%?A?%P>E1E>A>?>A?F>H1D>P'+
'1C>20B>@AF1D19,&GC1@>B1%?@1?17,&,6,5,8,4,3,>12,0,1,').match(/.{2}/g).forEach(
    function (v) {
        X = X[u](String.fromCharCode(K--))[U](v);
        if (K === 92)
            K = 91;
    }
);

/*
* Initially JSON, but got lost in compression work + a somewhat bad design
* on the obj. to begin with. Is eval, at least for now.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
try {
    eval(X
    [u]('*')[U]('f')
    [u](')')[U]('d')
    [u]('(')[U]('b')
    [u]('&')[U](']')
    [u]('%')[U]('[')
    [u]('#')[U]('}')
    [u]('!')[U]('{')
    );
} catch (e) {
    console.warn(e);
}

// Scaling function
function z(a) {
    return a.slice().map(
        function (v) {
            return v * Z;
        }
    );
}
// Tile painter "normal tiles"
function e(k, d, i) {
    for (s[Y] = k, i = 0; i < d[L];)
        s.rect.apply(s, z(d.slice(i, i += 4)));
}
//
function _(d, i, q) {
    S.width = R;
    for (i = 0; i < d[L]; ++i) {
        s.beginPath();
        F[q=d[++i][0]] ? F[q](d[i]) : e(f[d[i-1]],d[i]);
        s.fill();
    }
}
F = {
    $ : function (d) {  // Expand a tile (e.g. add border on top
                        // of "normal" bricks as with the "in-air".
        _(Å[d[1]].concat(d.slice(2)));
    },
    b : function (d) {  // Replace colors. (Used for bushes.)
        _(Å[d[1]].slice().map(
            function(i,x){
                return(x = d[2].indexOf(i)) > -1 ? d[2][++x] : i;
            }
        ));
    },
    f : function (d, i) { // Free-paint (fill). E.g. "hill-sides"
        s[Y] = f[d[1]];
        s.moveTo.apply(s, z(d[2]));
        for (i = 3; i < d[L]; ++i)
            s.lineTo.apply(s, z(d[i]));
    },
    d : function (d, x, y, g) { // Diagonal lines.
        s[Y] = f[d[1]];
        g = d[2] * Z;
        y = g > 0 ? 0 : 15 * Z;
        for (x = 0; x < 16 * Z; x += Z, y += g)
            s.rect(x, y, Z, Z);
    }
};
// Main function.
function xMx(h, k, i, j, t) {
    Z = h || 1;
    R = 16 * Z;
    D = document;
    C = D.getElementById('m');              // Doc canvas
    c = C.getContext('2d');
    S = D.createElement('CANVAS');          // Memory canvas
    s = S.getContext('2d');

    C.width  = 3392 * Z;
    C.height =  224 * Z;
    c[Y] = '#5c94fc';
    c.rect.apply(c, z([0, 0, 3392, 224]));  // Blue sky (rest is tiles)
    c.fill();

    for (k = 0; k < Ø[L]; ++k) { // Loop coordinate array. This has fixed
        _(Å[Ø[k]]);              // order.
        t = Ø[++k];
        for (i = 0; i < t[L]; i += 3) {
            for (j = 0; j < t[i+2] * R; j += R)
                c.drawImage(S, t[i] * R + j, t[i+1] * R); // Apply tile.
        }
    }
}

xMx(); // Load main.

```
> 5 votes

# Answer
# Python ~~1331~~ ~~1326~~ ~~1314~~ ~~1272~~ ~~1268~~ 1217 bytes

## (code: ~~219~~ ~~214~~ ~~202~~ ~~186~~ 182, data: ~~362+750=1112~~ ~~363+723=1086~~ 346+689=1035)

The idea here is that I first generate a map one 16th the size in each dimension, where each pixel is colored by the index in the tile map. To ensure that the same pallette was used, I first combined the given tile map and the map into a single image. From this and the tile map (both minified pngs) we can now regenerate the original map. (The code for this process can be seen below the submission code). It should be noted that I saved the tile map as a file "t" and the generated map as an image "m"

EDIT: I started exploring how the ordering of the tiles influenced the compression, and for the sake of exploration I generated 10000 random permutations and compressed them with this result: This was all with the tiles in one line, as that reduced the code quite a bit. I tried similar things with different configurations (2\*16, 4\*8), but none with a better average result.

Assuming that png would compress better with greater continuous similar regions, I built a tool that would let me move the tiles around, and using what I perceived to be a more continuous image I got the tile image down to 723 from 750b.

EDIT2: Afte rmuch (much) more analysis on how png actually works, and a lot of experimenting (still ongoing), the images have now been compressed even further. Link below updated. I'll write more about this analysis later when it is complete.

New images used are here: https://i.stack.imgur.com/gofpw.jpg

Here are the other images generated in the process: https://i.stack.imgur.com/9s1GD.jpg, though some are a bit outdated because of above edit.

```
import PIL.Image as I
O=I.open
t=O("t")
m=O("m").load()
o=I.new("RGB",(3392,224))
s=16
for i in range(2968):y=i/212;x=i%212;v=s*m[x,y];o.paste(t.crop((v,0,v+s,s)),(x*s,y*s))
o.show()

```

The script I wrote to reduce the map is pretty crude, but here goes (also might be outdated because of changes above):

```
from PIL import Image

blockSize = 16
combined = Image.open("combined.png")
tileImage = combined.crop((0,224,256,224+32))
w,h = tileImage.size

tiles = []
for y in range(0,h,16):
    for x in range(0,w,16):
        tiles+=[list(tileImage.crop((x,y,x+blockSize,y+blockSize)).getdata())]

referenceImage = combined.crop((0,0,3392,224))
w,h = referenceImage.size

mapImage = Image.new("RGB", (w/blockSize, h/blockSize))

colorScale = 256/len(tiles)

for y in range(0,h,16):
    for x in range(0,w,16):
        i = tiles.index(list(referenceImage.crop((x,y,x+blockSize,y+blockSize)).getdata()))
        mapImage.putpixel((x/16,y/16),(i*colorScale,i*colorScale,i*colorScale))

mapImage.save("map.png")

```
> 5 votes

# Answer
# Perl 5 + PNG: 689 + 593 = 1282

A failed attempt to replace PNG compressed tile map from Christian's solution with Perl code. In the end it is 65 bytes longer. Takes the same PNG tiles on input and generates a PNG on the output.

Usage: `perl mario.pl <tiles.png >world.png`

```
eval'+s/\d+(<?)(.+?)\1/$2x$&/ge'x2?$x|=$_:
map{/A/&&y'{}['Y';push@l,split"";y/D-IMNOY[|_@-_/EF@EE}JKLY~__@/;
s'@Y'YY'g;s'y@'yy'g;s'@E'DE'g;s'E@'EG'g}($x)x$_
for@z=qw(256@ 1
4<19@MNO14@MNNO8@<6@I 1
5<8@MNO16@M3NO16@< 2
22@X57@8T3@3TX14@X11@3T4@TXXT56@YY 3
203@3U 1
16@X3@TXTXT21@[|9@[|18@TXT14@T5@T5@X2@X2@X5@T10@2T6@Y2@y10@YY2@y12@TTXT31@QPS 1
5<2@H45@< 38@[|162@U3VU 1
5<17@H30@< 28@[|133@[|14@[|21@PPWPP 1
4<11@A3BC7@ABC15@ABBC3@<10@PPRPPC48@2<69Z2@15Z3@64Z2@101Z< 1);
$p=GD::Image;use GD;$f=new$p(3392,224);$k=newFromPng$p('-');
copy$f($k,map$_*16,$i%256,$i++>>8,31&ord,0,1,1)for@l;print$f->png

```
> 3 votes

# Answer
# JS: ~~4012~~ 3921 chars

JS is used to unpack many Unicode characters and write an img tag which src is the image given in the OP, saved as PNG, compressed, encoded in base64 and trimmed.

```
document.write(unescape(escape('').replace(/uD./g,'')))

```

Demo: http://jsbin.com/fojidejoco/1/

PS: I know it's a little bit cheating (even if the rules allow it), but at least, it gives us a goal: do it in less than 4012 chars.

Tools used:
> 1 votes

---
Tags: code-golf, kolmogorov-complexity, graphical-output
---