title: Whose neighbours are hostile?

text:
# Question
Title: Whose neighbours are hostile?
## Introduction

For the purposes of this challenge, we will define the *neighbours* of an element \\$E\\$ in a square matrix \\$A\\$ (such that \\$E=A\_{i,j}\\$) as all the entries of \\$A\\$ that are immediately adjacent diagonally, horizontally or vertically to \\$E\\$ (i.e. they *"surround"* \\$E\\$, without wrapping around).

For pedants, a formal definition of the neighbours of \\$A\_{i,\\:j}\\$ for an \\$n\times n\\$ matix \\$A\\$ is (0-indexed): $$N\_{i,\\:j}=\\{A\_{a,\\:b}\mid(a,b)\in E\_{i,\\:j}\\:\cap\\:(\[0,\\:n)\\:\cap\\:\Bbb{Z})^2\\}$$ where $$E\_{i,\\:j}=\\{i-1,\\:i,\\:i+1\\}\times \\{j-1,\\:j,\\:j+1\\} \text{ \\ } \\{i,\\:j\\}$$

Let's say that the element at index \\$i,\\:j\\$ lives in hostility if it is coprime to *all* its neighbours (that is, \\$\gcd(A\_{i,\\:j},\\:n)=1\\:\forall\\:n\in N\_{i,\\:j}\\$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...

## Task

Enough stories: Given a square matrix \\$M\\$ of positive integers, output one of the following:

* A flat list of elements (deduplicated or not) indicating all entries that occupy some indices \\$i,j\\$ in \\$M\\$ such that the neighbours \\$N\_{i,\\:j}\\$ are hostile.
* A boolean matrix with \\$1\\$s at positions where the neighbours are hostile and \\$0\\$ otherwise (you can choose any other consistent values in place of \\$0\\$ and \\$1\\$).
* The list of pairs of indices \\$i,\\:j\\$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!

Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.

## Example

Consider the following matrix:

$$\left(\begin{matrix} 64 & 10 & 14 \\ 27 & 22 & 32 \\ 53 & 58 & 36 \\ \end{matrix}\right)$$

The corresponding neighbours of each element are:

```
i j – E  -> Neighbours                          | All coprime to E?
                                                |
0 0 – 64 -> {10; 27; 22}                        | False
0 1 – 10 -> {64; 14; 27; 22; 32}                | False
0 2 – 14 -> {10; 22; 32}                        | False
1 0 – 27 -> {64; 10; 22; 53; 58}                | True
1 1 – 22 -> {64; 10; 14; 27; 32; 53; 58; 36}    | False
1 2 – 32 -> {10; 14; 22; 58; 36}                | False
2 0 – 53 -> {27; 22; 58}                        | True
2 1 – 58 -> {27; 22; 32; 53; 36}                | False
2 2 – 36 -> {22; 32; 58}                        | False

```

And thus the output must be one of the following:

* `{27; 53}`
* `{{0; 0; 0}; {1; 0; 0}; {1; 0; 0}}`
* `{(1; 0); (2; 0)}`

## Test cases

```
Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->
    []
    [[0, 0], [0, 0]]
    []
[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
    [38, 19]
    [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
    [(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
    [27, 53]
    [[0, 0, 0], [1, 0, 0], [1, 0, 0]]
    [(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->
    []
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    []
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
    [1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
    [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
    [71, 73, 47, 29]
    [[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
    [(0, 3), (1, 3), (2, 2), (3, 3)]

```

# Answer
# APL (Dyalog), 17 bytes

```
1=⊢∨(×/∘,↓)⌺3 3÷⊢

```

Try it online! (credits to ngn for translating the test cases to APL)

### Brief explanation

`(×/∘,↓)⌺3 3` gets the product of each element with its neighbours.

Then I divide by the argument `÷⊢`, so that each entry in the matrix has been mapped to the product of its neighbors.

Finally I take the gcd of the argument with this matrix `⊢∨`, and check for equality with 1, `1=`

Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.
> 3 votes

# Answer
# JavaScript (ES6), 121 bytes

Returns a matrix of Boolean values, where *false* means hostile.

```
m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))

```

Try it online!

### How?

The method used to isolate the 8 neighbors of each cell is similar to the one I described here.

### Commented

```
m =>                            // m[] = input matrix
  m.map((r, y) =>               // for each row r[] at position y in m[]:
    r.map((v, x) =>             //   for each value v at position x in r[]:
      [...'12221000']           //     we consider all 8 neighbors
      .some((k, j, a) =>        //     for each k at position j in this array a[]:
        ( g = (a, b) =>         //       g is a function which takes 2 integers a and b
            b ?                 //       and recursively determines whether they are
              g(b, a % b)       //       coprime to each other
            :                   //       (returns false if they are, true if they're not)
              a > 1             //
        )(                      //       initial call to g() with:
          v,                    //         the value of the current cell
          (m[y + ~-k] || 0)     //         and the value of the current neighbor
          [x + ~-a[j + 2 & 7]]  //
          || 1                  //         or 1 if this neighbor is undefined
  ))))                          //         (to make sure it's coprime with v)

```
> 2 votes

# Answer
# MATL, 22 bytes

```
tTT1&Ya3thYC5&Y)Zd1=A)

```

Input is a matrix. Output is all numbers with hostile neighbours.

Try it online! Or verify all test cases.

### Explanation with worked example

Consider input `[38, 77, 11; 17, 51, 32; 66, 78, 19]` as an example. Stack contents are shown bottom to top.

```
t         % Implicit input. Duplicate
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
TT1&Ya    % Pad in the two dimensions with value 1 and width 1
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1,  1,  1,  1,  1;
                    1,  38, 77, 11, 1;
                    1,  17, 51, 32, 1;
                    1,  66, 78, 19, 1
                    1,  1,  1,  1,  1]
3thYC     % Convert each sliding 3×3 block into a column (in column-major order)
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [ 1,  1,  1,  1, 38, 17,  1, 77, 51;
                     1,  1,  1, 38, 17, 66, 77, 51, 78;
                     1,  1,  1, 17, 66,  1, 51, 78,  1;
                     1, 38, 17,  1, 77, 51,  1, 11, 32;
                    38, 17, 66, 77, 51, 78, 11, 32, 19;
                    17, 66,  1, 51, 78,  1, 32, 19,  1;
                     1, 77, 51,  1, 11, 32,  1,  1,  1;
                    77, 51, 78, 11, 32, 19,  1,  1,  1;
                    51, 78,  1, 32, 19,  1,  1,  1,  1]
5&Y)      % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [38, 17, 66, 77, 51, 78, 11, 32, 19]
                   [ 1,  1,  1,  1, 38, 17,  1, 77, 51;
                     1,  1,  1, 38, 17, 66, 77, 51, 78;
                     1,  1,  1, 17, 66,  1, 51, 78,  1;
                     1, 38, 17,  1, 77, 51,  1, 11, 32;
                    17, 66,  1, 51, 78,  1, 32, 19,  1;
                     1, 77, 51,  1, 11, 32,  1,  1,  1;
                    77, 51, 78, 11, 32, 19,  1,  1,  1;
                    51, 78,  1, 32, 19,  1,  1,  1,  1]
Zd        % Greatest common divisor, element-wise with broadcast
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1,  1,  1,  1,  1,  1,  1,  1,  1;
                    1,  1,  1,  1, 17,  6, 11,  1,  1;
                    1,  1,  1,  1,  3,  1,  1,  2,  1;
                    1,  1,  1,  1,  1,  3,  1,  1,  1;
                    1,  1,  1,  1,  3,  1,  1,  1,  1;
                    1,  1,  3,  1,  1,  2,  1,  1,  1;
                    1, 17,  6, 11,  1,  1,  1,  1,  1;
                    1,  1,  1,  1,  1,  1,  1,  1,  1]
1=        % Compare with 1, element-wise. Gives true (1) or false (0)
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1, 1, 1, 1, 1, 1, 1, 1, 1;
                    1, 1, 1, 1, 0, 0, 0, 1, 1;
                    1, 1, 1, 1, 0, 1, 1, 0, 1;
                    1, 1, 1, 1, 1, 0, 1, 1, 1;
                    1, 1, 1, 1, 0, 1, 1, 1, 1;
                    1, 1, 0, 1, 1, 0, 1, 1, 1;
                    1, 0, 0, 0, 1, 1, 1, 1, 1;
                    1, 1, 1, 1, 1, 1, 1, 1, 1]
A         % All: true (1) for columns that do not contain 0
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1, 0, 0, 0, 0, 0, 0, 0, 1]
)         % Index (the matrix is read in column-major order). Implicit display
          % [38, 19]

```
> 2 votes

# Answer
# APL (Dyalog Classic), ~~23~~ 22 bytes

-1 byte thanks to @H.PWiz

```
{∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵}⌺3 3

```

Try it online!

doesn't support matrices smaller than 3x3 due to a bug in the interpreter
> 1 votes

# Answer
# Jelly, 24 bytes

Hmm, seems long.

```
ỊẠ€T
ŒJ_€`Ç€ḟ"J$ịFg"FÇịF

```

A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).

**Try it online!** Or see a test-suite.

### How?

```
ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
Ị    - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
  €  - for €ach:
 Ạ   -   all?
   T - truthy indices

ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
ŒJ                  - multi-dimensional indices
    `               - use as right argument as well as left...
   €                -   for €ach:
  _                 -     subtract (vectorises)
      €             - for €ach:
     Ç              -   call last Link (1) as a monad
          $         - last two links as a monad:
         J          -   range of length -> [1,2,3,...,n(elements)]
        "           -   zip with:
       ḟ            -     filter discard (remove the index of the item itself)
            F       - flatten M
           ị        - index into (vectorises) -- getting a list of lists of neighbours
               F    - flatten M
              "     - zip with:
             g      -   greatest common divisor
                Ç   - call last Link (1) as a monad
                  F - flatten M
                 ị  - index into

```
> 1 votes

# Answer
# Python 2, ~~182~~ ~~177~~ 166 bytes

```
lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
from fractions import*
E=enumerate

```

Try it online!

Outputs a list of lists with True/False entries.
> 1 votes

# Answer
# Haskell, 95 bytes

```
m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]

```

Try it online!

The function `?` takes the matrix `m` as a list of lists and the matrix size `n`; it returns the list of entries *in hostility*.
> 1 votes

---
Tags: code-golf, array, arithmetic, integer, matrix
---


title: Ascending matrix

text:
# Question
Title: Ascending matrix
The "ascending matrix" is an infinite matrix of whole numbers (0 included) in which any element is the smallest available element which has not been previously used on the respective row and column:

```
  | 1 2 3 4 5 6 ...
--+----------------
1 | 0 1 2 3 4 5 ...
2 | 1 0 3 2 5 4 ...
3 | 2 3 0 1 6 7 ...
4 | 3 2 1 0 7 6 ...
5 | 4 5 6 7 0 1 ...
6 | 5 4 7 6 1 0 ...
. | ...............

```

Your task is to write a program that will output the element found at the row and column specified by the input. (standard input and output)

Test cases:

```
5 3 -> 6
2 5 -> 5

```

Code Golf rules apply—the shortest code wins.

P.S. Even if this has an algorithmic nature the code can be very, **very** concise.

**EDIT:** I wasn't expecting to see the xor solution so early. I was really hoping to see 10 posts with an algorithmic approach and THEN the xor solution. Now, having in mind that it's not much fun to see how to write xor in different languages I recommend that you also try an algorithmic approach.

So, yes, I think no one can beat the 5 character mark now—therefore I congratulate Ilmari Karonen for the smartest and shortest solution. But there's a new challenge up ahead: **write the shortest algorithmic solution**.

# Answer
## GolfScript, 5 chars

```
~(\(^

```

Indeed, this task is very simple once you recognize the pattern. The only awkward bit is the 1-based indexing — if the input indices were zero-based, this **2-character solution** would suffice:

```
~^

```

To explain this to readers unfamiliar with GolfScript, the `~` command evals the input, leaving the two numbers on the stack. `^` then XORs the two topmost numbers on the stack together, leaving the result for output. To deal with 1-based input, two more commands are needed: `(` decrements the topmost number on the stack by one, while `\` swaps the top two items on the stack.
> 10 votes

# Answer
# Mathematica 10 44

**Edit**

My first response was based on a misunderstanding about the nature of the challenge, as noted by Ilmari. Here's another try.

**Usage**

```
f[n___, 1, n___] := n - 1;
j_~f~k_ := BitXor[j - 1, k - 1]

```
> 2 votes

# Answer
# K, 31

```
{0b/:{(x|y)&~x~y}. 0b\:'-1+x,y}

```

Stole Ilmari Karonen's XOR logic, which I would never have spotted myself.
> 2 votes

# Answer
# PHP, 38

Just a simple implementation of Ilmari Karonen's XOR

`<?php echo --$_GET['a']^--$_GET['b']?>`

Usage:

.../xor.php?a=4&b=7

will print 6
> 2 votes

# Answer
# Python 2, 36

I figure since I'm just starting to learn Python that this would be the perfect time to submit my first answer using it (and no one has answered using Python) and perhaps I could receive some feedback.

Thank you @IlmariKaronen for the very cool shortcut.

Thank you @Gareth for the code below.

```
import sys
print(input()-1^input()-1)

```

---

### Python 3, 56

The original program that I had written.

```
import sys
x=int(input())
y=int(input())
x-=1
y-=1
print(x^y)

```

**IDEONE with 2 and 5**

**IDEONE with 3 and 3**
> 2 votes

# Answer
# MATL, 2 bytes

```
Z~

```

Try it online!

MATL post-dates the challenge by several years, but, hey, natural 1-based indexing and a bitwise xor function makes this nice and neat!
> 2 votes

# Answer
## Haskell 174

Figured I'd make a solution that did not rely on XOR. Too lazy to golf it properly.

```
a 0 0=0
a b c
 |m==n=a(b-m)(c-n)
 |m>n=m+a(b-m)c
 |m<n=n+a b(c-n)
 where{g f=until(>f)(*2)1`div`2;m=g b;n=g c;}
main=do
 [x,y]<-fmap(map read.words)getLine
 print$a(x-1)(y-1)

```

**Edit:** I realized a day later that this is just calculating XOR. Thus if this counts as an algorithmic solution, so should Ilmari Karonen's.
> 1 votes

# Answer
# Perl 5, 12 bytes

```
say<>-1^<>-1

```

Try it online!
> 0 votes

# Answer
## Javascript 13 bytes

```
a=>b=>--a^--b

```

```
f=a=>b=>--a^--b

result = document.getElementById('result')
```

```
<input type="text" onkeyup="result.innerHTML = f(this.value.split(',')[0])(this.value.split(',')[1])" >
<p id="result"></p>
```
> 0 votes

# Answer
# Japt, 5 bytes

```
UÉ^VÉ

```

Try it online!
> 0 votes

---
Tags: code-golf, math, matrix
---


title: Color RGB int to hex

text:
# Question
Title: Color RGB int to hex
Given three integers rgb, output hexadecimal representation as a string.

```
Input:          Output:
72 61 139       #483D8B
75 0 130        #4B0082
0 255 127       #00FF7F

```

Shortest most unorthodox code wins

# Answer
# Ruby: 19 characters

```
$_=?#+"%02X"*3%$F

```

Sample run:

```
bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '72 61 139'
#483D8B

bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '75 0 130'
#4B0082

bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '0 255 127'
#00FF7F

```
> 14 votes

# Answer
## Ruby

I was going more for the "unorthodox" part. ;)

```
10;a=    gets.   split
.  map  {|       x|  #x
"%02X"  %(  x.   to_i)
}; $>   <<   ?#  <<  a[
00  ..   1+2].   join;

```

The font (on my web browser) is a bit tall, so it looks distorted, but it looks better in Courier New:

Sample run:

```
c:\a\ruby>rgb2hex
255 100 0
#FF6400

```
> 18 votes

# Answer
# q/k (3 chars)

Not exactly unorthodox but quite short

```
4h$

```

Example

```
q)4h$ 72 61 139
0x483d8b

```

To match the output format in the question exactly, we can do (for 9 chars):

```
"#",/$4h$

```
> 6 votes

# Answer
### bash ~~22~~ 21 chars

(Thanks @manatwork for 1 char using `\` instead of double-quotes)

```
printf \#%02X%02X%02X 12 12 12
#0C0C0C

```

or reading *STDIN* in a loop: 48 chars:

```
while read f;do printf "#%02X%02X%02X\n" $f;done <<<$'72 61 139\n75 0 130\n0 255 127'
#483D8B
#4B0082
#00FF7F

```

### Added 2015-10-06: bash (more *unorthodox* method) ~~84~~ 83 chars

```
c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 020]};done;done;echo $d

```

I know, it could be 82 chars if `020` where written `16`, but I prefer this... Or maybe `d+=${c[$b$a 0x10]}` which was first post.

```
hexcolor() {
    local a b c=({0..9} {a..f}) d=\#
    for b ;do
        for a in / % ;do
            d+=${c[$b$a 0x10]}
        done
    done
    echo $d
}
hexcolor 72 61 139
#483d8b
hexcolor 75 0 130
#4b0082
hexcolor 0 255 127
#00ff7f

```

### Another approach

```
#!/bin/bash
browser=firefox   #  google-chrome iceweasel
url="data:text/html;charset=UTF-8,<html><head></head><body>"
url+="<script type='text/javascript'>
  function h(i){var h=i.toString(16);if(16>1*i)h='0'+h;
  return h};function C(r,g,b){return'\043'+h(r)+h(g)+h(b)};
  function m(){ var r=1.0*R.value; var g=1.0*G.value; var b=1.0*B.value;
   var fore='black';if(384>r+g+b)fore='white';var c=C(r,g,b);
   s.setAttribute('style','background:'+c+';color:'+fore+';');s.innerHTML=c};
  function w(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)
  q=15;if(e.detail){if(e.detail>0){q=0-q;}}else if(0>e.wheelDelta){q=0-q;};
  val=1*val+q;if(val>255)val=255;if(0>val)val=0;e.target.value=val;m(); };
  function k(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)q=
  15;if(e.keyCode==38){val=1*val+q;if(val>255)val=255;e.target.value=val;m();}
  else if(e.keyCode==40){val=1*val-q;if(0>val)val=0;e.target.value=val;m();}};
  function n(){R=document.getElementById('R');G=document.getElementById('G');
    B=document.getElementById('B');s=document.getElementById('s');
    R.addEventListener('DOMMouseScroll',w);R.addEventListener('mousewheel',w);
    G.addEventListener('DOMMouseScroll',w);G.addEventListener('mousewheel',w);
    B.addEventListener('DOMMouseScroll',w);B.addEventListener('mousewheel',w);
  m();};var R, G, B, s;window.onload=n;
  </script><style>div{display:inline-block;width:10em;}</style>
  <div id='s'> </div>"
input="%s:<input type='text' size='5' value='200'"
input+=" onKeyDown='k(event)' onChange='m()' id='%s' />"
for c in R G B ;do
    printf -v add "$input" $c $c
    url+="$add"
  done
$browser "$url"

```

This will display a browser window, with:

Where you could roll the mousewheel to change values (with shift key holded for step by 15)...
> 5 votes

# Answer
## PowerShell: 71 52 51 41

> Thanks to @manatwork for pointing out `ForEach-Object` can be used in place of a `for` loop.
> Thanks to @Joey for pointing out that I could use `-join` on the loop output, instead of having to put it into variables.

**Golfed code:**

```
'#'+-join(1..3|%{"{0:X2}"-f+(read-host)})

```

**Note:**

There's no error checking here for invalid inputs. The script will happily take 256, 4096, and 65536 as inputs, then output #100100010000 (which obviously won't work as RGB).

**Ungolfed, with comments:**

```
# Put a hashtag in front of the output.
'#'+

# Join the nested code into one string.
-join(

    # Pipe 1..3 to ForEach-Object to run a loop three times.
    1..3|%{

        # Take user input and format it as hex, with minimum two digits in output.
        "{0:X2}"-f+(read-host)
    }
)

```
> 5 votes

# Answer
## Perl, 31 characters

```
perl -nE 'say"#",map{unpack H2,chr}split'

```

It's hard to make such a short program unorthodox, but I think this works.
> 3 votes

# Answer
# \>\<\> ~~103~~ 60

```
"#"or>1[:82*%:}-82*,86*+:"9"v
    ;^?l ]o+*7)"9":+*68o+*7)<

```

Made use of the wasted white space, and moved some code into it

Run with command line inputs:

```
python fish.py generateHex.fish -v 255 36 72

```

output: "#FF2448"
> 3 votes

# Answer
### Perl *24* chars

```
perl -ne 'printf"#"."%02x"x3,split'
13 31 133
#0d1f85

```

Sorry, it's not as sexy than using `unpack`, but shorter!

### More obscure version:

But if you really prefer to use `unpack`, then you could:

```
$==24;s/\d+[\n ]*/{$=-=8;($&<<$=).do{rand>.5?qw<+>[0]:"|"}}/eg;$_=
pack("N",eval($_.587.202.560));say$1,unpack("H6",$2)if/^(.)(.*)$/s

```

For sample, it's not the shorter version, but I like it! ( Note the use of `rand` for *randering* this :-)

```
perl -nE '
    $==24;s/\d+[\n ]*/{$=-=8;($&<<$=).
    do{rand>.5?qw<+>:"|"}}/eg;$_=pack(
    "N",eval($_.587.202.560) );say $1,
    unpack("H"."6",$2) if /^(.)(.*)$/s
  ' <<< $'72 61 139\n75 0 130\n0 255 127'
#483d8b
#4b0082
#00ff7f

```
> 3 votes

# Answer
## Action Script 3 | 43 characters

```
trace("#"+(72<<16|61<<139|b).toString(16));

```

Output: `#483D8B`
> 3 votes

# Answer
## C (67 chars no main boilerplate)

```
int r,g,b;
scanf("%d %d %d",&r,&b,&g);
printf("#%06x",r<<16|g<<8|b);

```

the bog standard printf & bit twiddler usage
> 2 votes

# Answer
## Forth, 62 characters

```
: D 0 <<# # # #> TYPE #>> ; 35 EMIT HEX SWAP ROT D D D DECIMAL

```
> 2 votes

# Answer
# Dc: ~~35~~ 32 characters

```
[#]n16o?ShShSh[Lhd16/n16%n]ddxxx

```

Sample run:

```
bash-4.1$ dc -e '[#]n16o?ShShSh[Lhd16/n16%n]ddxxx' <<< '72 61 139'
#483D8B

bash-4.1$ dc -e '[#]n16o?ShShSh[Lhd16/n16%n]ddxxx' <<< '75 0 130'
#4B0082

bash-4.1$ dc -e '[#]n16o?ShShSh[Lhd16/n16%n]ddxxx' <<< '0 255 127'
#00FF7F

```

## Dc: ~~27~~ 24 characters

(But needs the input numbers on separate lines.)

```
[#]n16o[?d16/n16%n]ddxxx

```

Sample run:

```
bash-4.1$ dc -e '[#]n16o[?d16/n16%n]ddxxx' <<< $'72\n61\n139'
#483D8B

bash-4.1$ dc -e '[#]n16o[?d16/n16%n]ddxxx' <<< $'75\n0\n130'
#4B0082

bash-4.1$ dc -e '[#]n16o[?d16/n16%n]ddxxx' <<< $'0\n255\n127'
#00FF7F

```
> 2 votes

# Answer
**JavaScript, 89 chars**

```
console.log('#' + ('00000' + eval('256*(256*(' + DEC.replace(/ /g, ')+')).toString(16)).slice(-6))

```

Converts `72 61 139` to `256*(256*(72)+61)+139` and evals it.
> 2 votes

# Answer
## PowerShell, 45

```
'#'+-join(-split(read-host)|%{'{0:X2}'-f+$_})

```

Or, if it can be used by piping in the data, you can just use

```
'#'+-join(-split"$input"|%{'{0:X2}'-f+$_})

```

which brings it down to **42**.
> 2 votes

# Answer
## powershell 37

saved one byte thanks to TimmyD

```
cat rgb2hex.ps1
$args|%{$o+="{0:X2}"-f[byte]$_};"#$o"
wc -c rgb2hex.ps1
38 rgb2hex.ps1
powershell -f .\rgb2hex.ps1 72 61 139
#483D8B
powershell -f .\rgb2hex.ps1 0 255 127
#00FF7F

```
> 2 votes

# Answer
# R, 16 bytes

That's it. Use the built in.

```
rgb(r,g,b,m=255)

```
> 2 votes

# Answer
## C, ~~67~~ 73 Characters (65 Chars excluding main)

```
main(){int a,b,c;scanf("%d%d%d",&a,&b,&c);printf("#%02X%02X%02X",a,b,c);}

```

Boring C program - very orthodox.
> 1 votes

# Answer
## Python 2.7 (80 Characters)

```
x=lambda y: ("0"+hex(int(y))[2:])[-2:]
print "#"+''.join(map(x,input().split()))

```

I'm looking for a better way to handle the single digit hex values. Any ideas?
> 1 votes

# Answer
### Befunge-98, 45 characters

Bleh, duplicated code. Oh well. Just a straightforward implementation of radix conversion.

```
"#",v
7*+,>#@&:97+/"0"+:"9"`7*+,97+%"0"+:"9"`

```

Sample run

```
% cfunge tohex.98 <<<'72 61 139'
#483D8B
% cfunge tohex.98
#75 0 130
4800820 255 127
00FF7F

```

(Note: prints '#' before reading input---the task doesn't forbid this; given three numbers on stdin it produces the right output on stdout. It also doesn't bother with newlines, and as you can see it doesn't add '#' properly when run "interactively".)
> 1 votes

# Answer
## Game Maker Language, 175 (Error at position 81)

Prompts for 3 to 9-digit RGB. Returns hexadecimal with GML hexadecimal sign, `$`

```
d=get_string('','')if(d=='')e=""else e="00"h="0123456789ABCDEF"while(d!=''){b=d&255i=string_char_at(h,byte div 16+1)l=string_char_at(h,byte mod 16+1)e+=i+l;d=d>>8}return '$'+e

```

Make this a script. Also, compile with uninitialized variables treated as 0.

Source
> 1 votes

# Answer
# Gema, 93 characters

```
\B=@set{x;0123456789abcdef}\#
<D>=@substring{@div{$1;16};1;$x}@substring{@mod{$1;16};1;$x}
?=

```

Sample run:

```
bash-4.3$ gema '\B=@set{x;0123456789abcdef}\#;<D>=@substring{@div{$1;16};1;$x}@substring{@mod{$1;16};1;$x};?=' <<< '0 255 127'
#00ff7f

```
> 1 votes

# Answer
## Burlsque, 12 bytes (10 bytes for lower-case)

If lower-case is allowed as well then 10 bytes:

```
psb6\['#+]

```

Usage:

```
blsq ) "72 61 139"psb6\['#+]
"#483d8b"

```

If you desperately need upper-case, add `ZZ`:

```
blsq ) "72 61 139"psb6\['#+]ZZ
"#483D8B"

```

If you don't receive the integers as in a string then go with:

```
blsq ) {72 61 139}b6\['#+]
"#483d8b"

```

**Explanation:**

```
ps -- parse string
b6 -- to hex
\[ -- concat
'#+] -- prepend #

```

Try online here.

**Bonus:**

To convert it back use this:

```
blsq ) "#483d8b"[-2cob6
{72 61 139}

```
> 1 votes

# Answer
# Powershell, 28 bytes

```
'#{0:X2}{1:X2}{2:X2}'-f$args

```

Test script:

```
$f = {

"#{0:X2}{1:X2}{2:X2}"-f$args
}

@(
    ,('#483D8B',72, 61, 139)
    ,('#4B0082',75,  0, 130)
    ,('#00FF7F',0 ,255, 127)
) | % {
    $e,$a = $_
    $r = &$f @a
    "$($r-eq$e): $r"
}

```

Output:

```
True: #483D8B
True: #4B0082
True: #00FF7F

```
> 1 votes

# Answer
## JavaScript: 99 chars (~~110~~ Thanks @manatwork):

```
function h(i){x=(i*=1).toString(16);return 16>i?"0"+x:x}function c(r,g,b){return"#"+h(r)+h(g)+h(b)}

```

### Sample:

```
#!/usr/bin/smjs

function h(i){x=(i*=1).toString(16);return 16>i?"0"+x:x}
function c(r,g,b){return"#"+h(r)+h(g)+h(b)}

if (typeof(arguments[2]) != "undefined" )
    print(c(arguments[0],arguments[1],arguments[2]));

var line;
while (line=readline()) {
    var a=line.split(" ");
    print(c(a[0],a[1],a[2]));
};

```

### In action:

```
rgb.js 14 15 16 <<< $'15 16 17\n16 17 18'
#0e0f10
#0f1011
#101112

```

### JS Snippet (216)

```
function h(i){x=(i*=1).toString(16);return 16>i?"0"+x:x}
function c([r,g,b]){return"#"+h(r)+h(g)+h(b)}
function d(){o.innerHTML="";t.value.split("\n").forEach(
function(l){o.innerHTML+=c(l.split(" "))+"<br>"; })};d()
```

```
<textarea id="t" onkeyup="d()">72 61 139
75 0 130
0 255 127</textarea><div id="o"></div>
```
> 0 votes

# Answer
# Python3.3 @ 60 chars

```
print("#"+3*"%.2X"%tuple(int(n) for n in input().split()))

```

Couldn't resist:

```
[dan@danbook:code_golf/int_to_hex]$ python3.3 int_to_hex.py
176 11 30
#B00B1E

```
> 0 votes

# Answer
# javascript (ES6), 55

```
alert('#'+(r<<16|g<<8|b).toString(16).padStart(6,'0'))

```
> 0 votes

# Answer
# Clojure 76 Characters

This might be late, for the sake of completeness:

```
(defn hexy [r g b] (reduce str (cons "#" (map #(format "%02X" %) [r g b]))))

```

Example calls:

```
(hexy 0 255 127)
"#00FF7F"

```
> 0 votes

# Answer
# Mathematica, 40 characters

```
f = StringJoin["#", IntegerString[#, 16, 2]] &

rgbs = {{72, 61, 139}, {75, 0, 130}, {0, 255, 127}}

f /@ rgbs // Column

```

> #483d8b
>
> #4b0082
>
> #00ff7f
> 0 votes

# Answer
## Common Lisp, 39 characters

```
(format()"#~@{~2,'0x~}"#1=(read)#1##1#)

```

Read three integers, return a string.
> 0 votes

# Answer
# Javascript ES6, 63 bytes

```
h=>'#'+h.split` `.map(x=>(x<16?0:'')+(x*1).toString(16)).join``

```
> 0 votes

# Answer
# Python (55 chars)

```
'#'+''.join([hex(int(i))[2:].upper() for i in input()])

```
> 0 votes

# Answer
# Python 2, 40 bytes

```
s='#';exec's+="%02X"%input();'*3;print s

```

Reads the input from three separate lines. Abuses `exec` and string multiplication.

Example usage:

```
$ python2 rgbint.py
72
61
139
#483D8B

$ python2 rgbint.py
75
0
130
#4B0082

$ python2 rgbint.py
0
255
127
#00FF7F

```
> 0 votes

---
Tags: code-golf
---


# Question
Title: Build a multiplying machine using NAND logic gates
Based on my previous question of the same type, Build an adding machine using NAND logic gates, this time you're being asked to multiply instead of add.

Build a diagram of (two-wire) NAND logic gates that will take the input wires `A1`, `A2`, `A4`, `B1`, `B2`, `B4`, representing two binary numbers `A` to `B` from 0 to 7, and return values on the output wires `C1`, `C2`, `C4`, `C8`, `C16`, and `C32`, representing `C`, which is the product of `A` and `B`.

Your score is determined by the number of NAND gates you use (1 point per gate). To simplify things, you may use AND, OR, NOT, and XOR gates in your diagram, with the following corresponding scores:

* `NOT: 1`
* `AND: 2`
* `OR: 3`
* `XOR: 4`

Each of these scores corresponds to the number of NAND gates that it takes to construct the corresponding gate.

Lowest score wins.

# Answer
> 15 votes
# 39 gates

I am quite sure there are no simpler designs than mine here. It was very difficult to make. I make other minimal circuits too.

Transmission delay is indicated by downward position of each NAND gate on the sheet.

Verilog code and testing:

```
// MINIMAL 3 BIT MULTIPLICATOR
//
// The simplest 3 bit multiplicator possible, using 39 NAND gates only.
//
// I have also made multiplicators that are faster, more efficient,
// use different gates, and multiply bigger numbers. And I also do
// hard optimization of other circuits. You may contact me at
// kim.oyhus@gmail.com
//
// This is my entry to win this hard Programming Puzzle & Code Golf
// at Stack Exchange:
// https://codegolf.stackexchange.com/questions/12261/build-a-multiplying-machine-using-nand-logic-gates/
//
// By Kim Øyhus 2018 (c) into (CC BY-SA 3.0)
// This work is licensed under the Creative Commons Attribution 3.0
// Unported License. To view a copy of this license, visit
// https://creativecommons.org/licenses/by-sa/3.0/

module mul3x3 ( in_000, in_001, in_002, in_003, in_004, in_005, out000, out001, out002, out003, out004, out005 );
  input  in_000, in_001, in_002, in_003, in_004, in_005;
  output out000, out001, out002, out003, out004, out005;
  wire   wir000, wir001, wir002, wir003, wir004, wir005, wir006, wir007, wir008, wir009, wir010, wir011, wir012, wir013, wir014, wir015, wir016, wir017, wir018, wir019, wir020, wir021, wir022, wir023, wir024, wir025, wir026, wir027, wir028, wir029, wir030, wir031, wir032;

  nand gate000 ( wir000, in_000, in_005 );
  nand gate001 ( wir001, in_000, in_004 );
  nand gate002 ( wir002, in_000, in_003 );
  nand gate003 ( out000, wir002, wir002 );
  nand gate004 ( wir003, in_004, in_001 );
  nand gate005 ( wir004, wir003, wir003 );
  nand gate006 ( wir005, in_003, in_002 );
  nand gate007 ( wir006, wir000, wir005 );
  nand gate008 ( wir007, in_004, in_002 );
  nand gate009 ( wir008, in_001, in_005 );
  nand gate010 ( wir009, wir008, wir007 );
  nand gate011 ( wir010, in_001, in_003 );
  nand gate012 ( wir011, wir001, wir010 );
  nand gate013 ( wir012, out000, wir004 );
  nand gate014 ( wir013, wir004, wir012 );
  nand gate015 ( wir014, wir011, wir012 );
  nand gate016 ( out001, wir014, wir014 );
  nand gate017 ( wir015, in_002, in_005 );
  nand gate018 ( wir016, wir015, wir015 );
  nand gate019 ( wir017, out000, wir016 );
  nand gate020 ( wir018, wir017, wir013 );
  nand gate021 ( wir019, wir016, wir018 );
  nand gate022 ( wir020, wir019, wir009 );
  nand gate023 ( wir021, wir020, wir017 );
  nand gate024 ( wir022, wir020, wir009 );
  nand gate025 ( wir023, wir022, wir021 );
  nand gate026 ( out005, wir022, wir022 );
  nand gate027 ( wir024, wir016, wir022 );
  nand gate028 ( wir025, wir006, wir018 );
  nand gate029 ( wir026, wir025, wir006 );
  nand gate030 ( wir027, wir025, wir018 );
  nand gate031 ( out002, wir026, wir027 );
  nand gate032 ( wir028, wir004, wir027 );
  nand gate033 ( wir029, wir023, wir028 );
  nand gate034 ( wir030, wir028, wir028 );
  nand gate035 ( wir031, wir030, wir021 );
  nand gate036 ( out004, wir031, wir024 );
  nand gate037 ( wir032, wir029, wir031 );
  nand gate038 ( out003, wir032, wir032 );
endmodule

module mul3x3_test;
   reg  [5:0] AB; // C=A*B
   wire [5:0] C;

  mul3x3 U1 (
  .in_000 (AB[0]),
  .in_001 (AB[1]),
  .in_002 (AB[2]),
  .in_003 (AB[3]),
  .in_004 (AB[4]),
  .in_005 (AB[5]),
  .out000 (C[0]),
  .out001 (C[1]),
  .out002 (C[2]),
  .out003 (C[3]),
  .out004 (C[4]),
  .out005 (C[5])
  );

  initial  AB=0;
  always  #10  AB = AB+1;
  initial  begin
    $display("\t\ttime,\tA,\tB,\tC");
    $monitor("%d,\t%b\t%b\t%b",$time, AB[5:3], AB[2:0],C);
  end
  initial  #630  $finish;
endmodule

// iverilog -o mul3x3_test mul3x3_test.v
// vvp mul3x3_test

```

Kim Øyhus

# Answer
> 27 votes
# ~~60 55 50~~ 48 gates

---

The original (60 gates) was the systematic approach - multiply each digit with each, and then sum them together. Namely, see Wallace trees and Dadda trees

The top half is the multiplication network - multiply each digit with each, and group output digits with the same weight. Some bits have been left inverted to save gates.

The second half is the adder network. Each box represents a single adder - either a half-adder (5 gates - 1x XOR and an inverter), or a full adder (9 gates - 2x XOR and NAND the inverted carry bits). The top are inputs, the bottom output is the sum, the left output is the carry-out. see the previous challenge

The 2x2 multiplier has then been hand-optimised to a custom-built 13-gate network, which is the optimal size as found by @boothby. Thanks!

Pasting it into the low-bit corner and reoptimising the adder tree saves five gates (see revision #2). Pasting it into the high-bit corner as well, however, produces overlap. A little bit of math tells us, however, that dropping the low-bit of the high multiplier solves the overlap and what's left to do is to add the remaining two bits and sum up stuff.

This alone, unfortunately, does not provide any savings, but it does open two optimisations. First, the two multipliers have two gates in common, and can be fused together. At this point, we're back at 55. Second, in the addition network, we don't need a half-adder because we know its carry will be zero. We can replace it with an OR. An OR is a NAND with its inputs inverted. This produces us with two 2-chains of NOTs on each branch, which can then be removed, for a total saving of five gates. Unfortunately, the half-adder at C16 still carries, so we cannot do the same there. Third, a full adder has a useful property: if you invert its inputs and its outputs, it still behaves the same. Since all its inputs are already inverted, we can just as well move the inverters behind it. Twice. We could have done the same in the original, but... oh well. We still have a half-adder with two inverted inputs. I want optimise this part more, but I doubt I can.

Since we're pulling out a NOT from inside a component, we have to signify that somehow. We have obtained a half-adder with inverted carry (AKA tapped XOR) at a cost of four gates.

In the meantime, we have also redrawn the diagram significantly.

---
Tags: logic-gates, atomic-code-golf
---


# Question
Title: Get the lowest possible pylint score within 128 bytes of python
pylint has no lower bound on the scores it will give your code. It uses the following metric to score code, with the maximum score being 10:

```
10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)

```

With python 3.6, try and write a program that:

* Outputs "Hello World" when run from the command line (i.e. `python script.py`), and no other output.
* Gets the lowest possible score from `pylint`, with default settings (i.e. `pylint script.py`)
* Is strictly less than or equal to 128 bytes in size.

# Answer
# -5430

```
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++exit("Hello World")

```

Try it online!
> 17 votes

# Answer
# ~~-3330.00~~ -3540.00

* Lost 210 points thanks to Dennis -- exiting with the required string instead of printing it.

```
exit("Hello World"
)>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_>_,_

```

Try it online!
> 5 votes

# Answer
# -840.0

```
print("Hello World"),0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0,0<0

```

Try it online!

`0<0` seems to be pretty good, for a score of -30 points per each occurrence.
> 2 votes

# Answer
# -335.0

126 bytes

```
print("Hello World");import os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os,os

```
> 1 votes

---
Tags: code-challenge, python, hello-world, error-message
---


title: You failed everything!

text:
# Question
Title: You failed everything!
Your goal is simple: to fail everything. More precisely, you should write the shortest code that triggers a compilation/interpretation error, or a runtime error.

# What does not count

* Failing with symbols that are not covered by the language syntax and are meant to appear only in string literals, comments and other ways to include non-executable data in the code, or are not meant to appear in the code at all
* Warnings and notices that don't prevent successful compilation or execution
* Overflow related errors: infinite loops, recursion limits reaching, stack overflows, out-of-memory errors and so on

This is , so the shortest code in bytes wins!

By the way, this is probably the only challenge where non-esoterical languages can beat esoterical ones ;-)

# Answer
> 16 votes
# Polyglot, 0 bytes

This is a list of languages where an empty program gives an error.

# Answer
> 3 votes
# 60% of all coding languages, 1 byte

`+` or `-` or `/` or `*` or `%` or any valid token etc.

To complement Bubbler's answer.

# Answer
> 2 votes
# Polyglot, 1 byte

```
a

```

This gives `undefined identifier` error in many interpreted languages.

* Python
* Javascript
* APL
* J
* (Add language here)

# Polyglot, 1 byte

```
+

```

This gives `syntax error` in many languages.

* (Add language here)

# Polyglot, 1 byte

```
'

```

This gives `open string literal` error in many languages.

* (Add language here)

# Polyglot, 1 byte

```
[

```

This gives `open bracket` error in many languages. (Feel free to change to `(` or `{` if some languages specifically error with it.)

* (Add language here)

# Answer
> 1 votes
# Racket / Scheme / Lisp / Others?: 1 byte

`)` Will give an error for mismatched parentheses

# Answer
> 1 votes
# Ahead, 1 byte

```
/

```

Tries to do `0/0`. Reference implementation on TIO will fail with:

```
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at ajc2.ahead.Head.doCell(Head.kt:148)
        at ajc2.ahead.Head.step(Head.kt:31)
        at ajc2.ahead.Runner.go(Runner.kt:6)
        at ajc2.ahead.MainKt.main(Main.kt:13)

```

Try it online!

# Answer
> 0 votes
# Attache, 1 byte

```
/

```

Try it online!

Crashes with:

```
AttacheUnimplementedError: (1:1) / is not defined for `NilClass`

```

---
Tags: code-golf, error-message
---


title: Counting Squares

text:
# Question
Title: Counting Squares
We've all seen the pictures captioned "How many squares are in this image? 98% will not get this right!" Well, here's your chance to laugh at that 98%.

## Input

A rectangular block made of only 2 characters. In a general form, this is defined by *m* lines, each containing *n* characters.

Example:

```
0000xxx
00x00xx
00000xx
xxxxx0x

```

This can be represented as a newline-separated string, an array of strings, or a 2D array of characters. Any other input types should be suggested in the comments.

## Output

The output will be an integer representing the total amount of **squares** represented individually by *both* characters (in pseudocode, num\_squares(char1) + num\_squares(char2))

A **square** is defined as a set of instances of the same character that fully outline a geometric square of side length *at least 2*. These squares need not be filled by the character that outlines it. The following examples illustrate this concept:

```
x0
0x    // no squares

xxx
x0x
xxx   // 1 square of side length 3

0000
0xx0
0xx0
0000  // 1 square of side length 2, 1 square of side length 4

```

## Scoring

Your program will be scored by 2 criteria, in descending order of importance:

* Number of characters in your code block that aren't part of a square, as defined above (you should aim to minimize this!) (Note: characters inside of a square are not automatically considered as part of that square) (Note 2: newlines do not count toward this score)
* Number of bytes (you should aim to minimize this!)

The second criterion will act as a tie breaker for the first.

## Challenge Rules

* For the sake of flexibility, you may choose to use any 2 alphanumeric characters to replace the 0 and x from my examples, as long as they are not the same character.
* Each line in the input rectangle must have the same length, and must not contain any character other than the 2 specified for your answer to the challenge (besides the newline character, if applicable)
* Trivial squares (1x1 squares denoted by a single character) do not count toward the output. They suck.
* Your program does not have to handle empty input, but it should handle cases where no squares are possible (when one of the dimensions of the input rectangle is 1)
* You may also choose to specify that you are handling diagonally aligned squares if you relish the challenge. This would be added to the output specified in the challenge specs.

Example of this concept:

```
Input:

00x00
0x0x0
x000x
0x0x0
00x00

Output: 6 (1 diagonally aligned square of side length 3, 5 diagonally aligned squares of side length 2)

```

## Test Cases

The following are some basic test cases that your program should match. The value in parentheses is the output should you choose to count diagonally aligned squares.

```
Input:

xxxx
x00x
x0xx
xxxx

Output: 2 (2)

```

---

```
Input:

x00000xxx0

Output: 0 (0)

```

---

```
Input:

xxx0x0x000xx0xx0
0x000x00x0x0xx00
00xx0xx00xx0000x
x0x0x0x0x00x0xxx
00xx0xxx00xxx0x0

Output: 0 (5)

```

---

```
Input:

0000
0000
0000
0000
0000
0000

Output: 26 (34)

```

---

```
Input:

xxx00
xx000

Output: 2 (2)

```

## General rules

* Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
* Default Loopholes are forbidden.
* If possible, please add a link with a test for your code.
* Adding an explanation for your answer is highly recommended.
* Any questions/corrections/advice for me? Comment below! This is my first question, so I'm excited to learn as much from it as possible.

# Answer
> 5 votes
# Perl, 0 characters not in squares (25430 bytes)

```
##((((((((((((((((((((((((((((((((((((((((((((((
##($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(
##($rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr$(
##($r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,r$(
##($r,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,r$(
##($r,$cccccccccccccccccccccccccccccccccccc$,r$(
##($r,$c,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,c$,r$(
##($r,$c,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@,c$,r$(
##($r,$c,@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@,c$,r$(
##($r,$c,@a))))))))))))))))))))))))))))a@,c$,r$(
##($r,$c,@a)==========================)a@,c$,r$(
##($r,$c,@a)=@@@@@@@@@@@@@@@@@@@@@@@@=)a@,c$,r$(
##($r,$c,@a)=@AAAAAAAAAAAAAAAAAAAAAA@=)a@,c$,r$(
##($r,$c,@a)=@ARRRRRRRRRRRRRRRRRRRRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGGGGGGGGGGGGGGGGGGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGVVVVVVVVVVVVVVVVGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;;;;;;;;;;;;;;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<<<<<<<<<<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<<<<<<<<<<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<''''''''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<''''''''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<'';;;;''<<;VGRA@=)a@,c$,r$(
  ($r,$c,@a)=@ARGV;<<'';##;''<<;VGRA@=)a@,c$,r$(
  ($r,$c,@a)=@ARGV;<<'';##;''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<'';;;;''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<''''''''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<''''''''<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<<<<<<<<<<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;<<<<<<<<<<<<;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGV;;;;;;;;;;;;;;VGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGVVVVVVVVVVVVVVVVGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARGGGGGGGGGGGGGGGGGGRA@=)a@,c$,r$(
##($r,$c,@a)=@ARRRRRRRRRRRRRRRRRRRRA@=)a@,c$,r$(
##($r,$c,@a)=@AAAAAAAAAAAAAAAAAAAAAA@=)a@,c$,r$(
##($r,$c,@a)=@@@@@@@@@@@@@@@@@@@@@@@@=)a@,c$,r$(
##($r,$c,@a)==========================)a@,c$,r$(
##($r,$c,@a))))))))))))))))))))))))))))a@,c$,r$(
##($r,$c,@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@,c$,r$(
##($r,$c,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@,c$,r$(
##($r,$c,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,c$,r$(
##($r,$cccccccccccccccccccccccccccccccccccc$,r$(
##($r,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,r$(
##($r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,r$(
##($rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr$(
##($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(
##((((((((((((((((((((((((((((((((((((((((((((((

##ffffffffffffffffffffffffffffffffffffffffff
##foooooooooooooooooooooooooooooooooooooooof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##for$yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy$rof
##for$y((((((((((((((((((((((((((((((((y$rof
##for$y(000000000000000000000000000000(y$rof
##for$y(0............................0(y$rof
##for$y(0............................0(y$rof
##for$y(0..$$$$$$$$$$$$$$$$$$$$$$$$..0(y$rof
##for$y(0..$rrrrrrrrrrrrrrrrrrrrrr$..0(y$rof
##for$y(0..$r--------------------r$..0(y$rof
##for$y(0..$r-111111111111111111-r$..0(y$rof
##for$y(0..$r-1))))))))))))))))1-r$..0(y$rof
##for$y(0..$r-1){{{{{{{{{{{{{{)1-r$..0(y$rof
##for$y(0..$r-1){<<<<<<<<<<<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<<<<<<<<<<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<''''''''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<''''''''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<'';;;;''<<{)1-r$..0(y$rof
  for$y(0..$r-1){<<'';##;''<<{)1-r$..0(y$rof
  for$y(0..$r-1){<<'';##;''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<'';;;;''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<''''''''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<''''''''<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<<<<<<<<<<<{)1-r$..0(y$rof
##for$y(0..$r-1){<<<<<<<<<<<<{)1-r$..0(y$rof
##for$y(0..$r-1){{{{{{{{{{{{{{)1-r$..0(y$rof
##for$y(0..$r-1))))))))))))))))1-r$..0(y$rof
##for$y(0..$r-111111111111111111-r$..0(y$rof
##for$y(0..$r--------------------r$..0(y$rof
##for$y(0..$rrrrrrrrrrrrrrrrrrrrrr$..0(y$rof
##for$y(0..$$$$$$$$$$$$$$$$$$$$$$$$..0(y$rof
##for$y(0............................0(y$rof
##for$y(0............................0(y$rof
##for$y(000000000000000000000000000000(y$rof
##for$y((((((((((((((((((((((((((((((((y$rof
##for$yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy$rof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##foooooooooooooooooooooooooooooooooooooooof
##ffffffffffffffffffffffffffffffffffffffffff

##ffffffffffffffffffffffffffffffffffffffffff
##foooooooooooooooooooooooooooooooooooooooof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##for$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$rof
##for$x((((((((((((((((((((((((((((((((x$rof
##for$x(000000000000000000000000000000(x$rof
##for$x(0............................0(x$rof
##for$x(0............................0(x$rof
##for$x(0..$$$$$$$$$$$$$$$$$$$$$$$$..0(x$rof
##for$x(0..$cccccccccccccccccccccc$..0(x$rof
##for$x(0..$c--------------------c$..0(x$rof
##for$x(0..$c-111111111111111111-c$..0(x$rof
##for$x(0..$c-1))))))))))))))))1-c$..0(x$rof
##for$x(0..$c-1){{{{{{{{{{{{{{)1-c$..0(x$rof
##for$x(0..$c-1){<<<<<<<<<<<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<<<<<<<<<<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<''''''''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<''''''''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<'';;;;''<<{)1-c$..0(x$rof
  for$x(0..$c-1){<<'';##;''<<{)1-c$..0(x$rof
  for$x(0..$c-1){<<'';##;''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<'';;;;''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<''''''''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<''''''''<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<<<<<<<<<<<{)1-c$..0(x$rof
##for$x(0..$c-1){<<<<<<<<<<<<{)1-c$..0(x$rof
##for$x(0..$c-1){{{{{{{{{{{{{{)1-c$..0(x$rof
##for$x(0..$c-1))))))))))))))))1-c$..0(x$rof
##for$x(0..$c-111111111111111111-c$..0(x$rof
##for$x(0..$c--------------------c$..0(x$rof
##for$x(0..$cccccccccccccccccccccc$..0(x$rof
##for$x(0..$$$$$$$$$$$$$$$$$$$$$$$$..0(x$rof
##for$x(0............................0(x$rof
##for$x(0............................0(x$rof
##for$x(000000000000000000000000000000(x$rof
##for$x((((((((((((((((((((((((((((((((x$rof
##for$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$rof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##foooooooooooooooooooooooooooooooooooooooof
##ffffffffffffffffffffffffffffffffffffffffff

##ffffffffffffffffffffffffffffffffffffffffffffffff
##foooooooooooooooooooooooooooooooooooooooooooooof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##for$ssssssssssssssssssssssssssssssssssssssss$rof
##for$s((((((((((((((((((((((((((((((((((((((s$rof
##for$s(111111111111111111111111111111111111(s$rof
##for$s(1..................................1(s$rof
##for$s(1..................................1(s$rof
##for$s(1..$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$..1(s$rof
##for$s(1..$cccccccccccccccccccccccccccc$..1(s$rof
##for$s(1..$c--------------------------c$..1(s$rof
##for$s(1..$c-$$$$$$$$$$$$$$$$$$$$$$$$-c$..1(s$rof
##for$s(1..$c-$xxxxxxxxxxxxxxxxxxxxxx$-c$..1(s$rof
##for$s(1..$c-$x--------------------x$-c$..1(s$rof
##for$s(1..$c-$x-111111111111111111-x$-c$..1(s$rof
##for$s(1..$c-$x-1))))))))))))))))1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){{{{{{{{{{{{{{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<<<<<<<<<<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<<<<<<<<<<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<''''''''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<''''''''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<'';;;;''<<{)1-x$-c$..1(s$rof
  for$s(1..$c-$x-1){<<'';##;''<<{)1-x$-c$..1(s$rof
  for$s(1..$c-$x-1){<<'';##;''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<'';;;;''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<''''''''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<''''''''<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<<<<<<<<<<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){<<<<<<<<<<<<{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1){{{{{{{{{{{{{{)1-x$-c$..1(s$rof
##for$s(1..$c-$x-1))))))))))))))))1-x$-c$..1(s$rof
##for$s(1..$c-$x-111111111111111111-x$-c$..1(s$rof
##for$s(1..$c-$x--------------------x$-c$..1(s$rof
##for$s(1..$c-$xxxxxxxxxxxxxxxxxxxxxx$-c$..1(s$rof
##for$s(1..$c-$$$$$$$$$$$$$$$$$$$$$$$$-c$..1(s$rof
##for$s(1..$c--------------------------c$..1(s$rof
##for$s(1..$cccccccccccccccccccccccccccc$..1(s$rof
##for$s(1..$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$..1(s$rof
##for$s(1..................................1(s$rof
##for$s(1..................................1(s$rof
##for$s(111111111111111111111111111111111111(s$rof
##for$s((((((((((((((((((((((((((((((((((((((s$rof
##for$ssssssssssssssssssssssssssssssssssssssss$rof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##foooooooooooooooooooooooooooooooooooooooooooooof
##ffffffffffffffffffffffffffffffffffffffffffffffff

##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
##$mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm$
##$m========================================================m$
##$m=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=m$
##$m=$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$=m$
##$m=$a[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[a$=m$
##$m=$a[$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$[a$=m$
##$m=$a[$oooooooooooooooooooooooooooooooooooooooooooooo$[a$=m$
##$m=$a[$o============================================o$[a$=m$
##$m=$a[$o=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=o$[a$=m$
##$m=$a[$o=$cccccccccccccccccccccccccccccccccccccccc$=o$[a$=m$
##$m=$a[$o=$c**************************************c$=o$[a$=m$
##$m=$a[$o=$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$=o$[a$=m$
##$m=$a[$o=$c*$yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy$*c$=o$[a$=m$
##$m=$a[$o=$c*$y++++++++++++++++++++++++++++++++y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$xxxxxxxxxxxxxxxxxxxxxxxxxxxx$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x]]]]]]]]]]]]]]]]]]]]]]]]]]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];;;;;;;;;;;;;;;;;;;;;;;;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$$$$$$$$$$$$$$$$$$$$$$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$kkkkkkkkkkkkkkkkkkkk$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k==================k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0000000000000000=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;;;;;;;;;;;;;;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<<<<<<<<<<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<<<<<<<<<<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<''''''''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<''''''''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<'';;;;''<<;0=k$;]x$+y$*c$=o$[a$=m$
  $m=$a[$o=$c*$y+$x];$k=0;<<'';##;''<<;0=k$;]x$+y$*c$=o$[a$=m$
  $m=$a[$o=$c*$y+$x];$k=0;<<'';##;''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<'';;;;''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<''''''''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<''''''''<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<<<<<<<<<<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;<<<<<<<<<<<<;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0;;;;;;;;;;;;;;0=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k=0000000000000000=k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$k==================k$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$kkkkkkkkkkkkkkkkkkkk$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];$$$$$$$$$$$$$$$$$$$$$$;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x];;;;;;;;;;;;;;;;;;;;;;;;]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$x]]]]]]]]]]]]]]]]]]]]]]]]]]x$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$xxxxxxxxxxxxxxxxxxxxxxxxxxxx$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y+$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+y$*c$=o$[a$=m$
##$m=$a[$o=$c*$y++++++++++++++++++++++++++++++++y$*c$=o$[a$=m$
##$m=$a[$o=$c*$yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy$*c$=o$[a$=m$
##$m=$a[$o=$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$=o$[a$=m$
##$m=$a[$o=$c**************************************c$=o$[a$=m$
##$m=$a[$o=$cccccccccccccccccccccccccccccccccccccccc$=o$[a$=m$
##$m=$a[$o=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=o$[a$=m$
##$m=$a[$o============================================o$[a$=m$
##$m=$a[$oooooooooooooooooooooooooooooooooooooooooooooo$[a$=m$
##$m=$a[$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$[a$=m$
##$m=$a[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[a$=m$
##$m=$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$=m$
##$m=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=m$
##$m========================================================m$
##$mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm$
##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

##ffffffffffffffffffffffffffffffffffffff
##foooooooooooooooooooooooooooooooooooof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##for$tttttttttttttttttttttttttttttt$rof
##for$t((((((((((((((((((((((((((((t$rof
##for$t(11111111111111111111111111(t$rof
##for$t(1........................1(t$rof
##for$t(1........................1(t$rof
##for$t(1..$$$$$$$$$$$$$$$$$$$$..1(t$rof
##for$t(1..$ssssssssssssssssss$..1(t$rof
##for$t(1..$s))))))))))))))))s$..1(t$rof
##for$t(1..$s){{{{{{{{{{{{{{)s$..1(t$rof
##for$t(1..$s){<<<<<<<<<<<<{)s$..1(t$rof
##for$t(1..$s){<<<<<<<<<<<<{)s$..1(t$rof
##for$t(1..$s){<<''''''''<<{)s$..1(t$rof
##for$t(1..$s){<<''''''''<<{)s$..1(t$rof
##for$t(1..$s){<<'';;;;''<<{)s$..1(t$rof
  for$t(1..$s){<<'';##;''<<{)s$..1(t$rof
  for$t(1..$s){<<'';##;''<<{)s$..1(t$rof
##for$t(1..$s){<<'';;;;''<<{)s$..1(t$rof
##for$t(1..$s){<<''''''''<<{)s$..1(t$rof
##for$t(1..$s){<<''''''''<<{)s$..1(t$rof
##for$t(1..$s){<<<<<<<<<<<<{)s$..1(t$rof
##for$t(1..$s){<<<<<<<<<<<<{)s$..1(t$rof
##for$t(1..$s){{{{{{{{{{{{{{)s$..1(t$rof
##for$t(1..$s))))))))))))))))s$..1(t$rof
##for$t(1..$ssssssssssssssssss$..1(t$rof
##for$t(1..$$$$$$$$$$$$$$$$$$$$..1(t$rof
##for$t(1........................1(t$rof
##for$t(1........................1(t$rof
##for$t(11111111111111111111111111(t$rof
##for$t((((((((((((((((((((((((((((t$rof
##for$tttttttttttttttttttttttttttttt$rof
##for$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$rof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##foooooooooooooooooooooooooooooooooooof
##ffffffffffffffffffffffffffffffffffffff

##ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
##foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##for((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((rof
##for($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(rof
##for($tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt$(rof
##for($t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,t$(rof
##for($t,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,t$(rof
##for($t,$cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc$,t$(rof
##for($t,$c**********************************************************c$,t$(rof
##for($t,$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$,t$(rof
##for($t,$c*$tttttttttttttttttttttttttttttttttttttttttttttttttttttt$*c$,t$(rof
##for($t,$c*$t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,t$*c$,t$(rof
##for($t,$c*$t,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,t$*c$,t$(rof
##for($t,$c*$t,$tttttttttttttttttttttttttttttttttttttttttttttttt$,t$*c$,t$(rof
##for($t,$c*$t,$t++++++++++++++++++++++++++++++++++++++++++++++t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$cccccccccccccccccccccccccccccccccccccccccc$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c****************************************c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$ssssssssssssssssssssssssssssssssssss$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$cccccccccccccccccccccccccccccc$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c****************************c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$$$$$$$$$$$$$$$$$$$$$$$$$$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$tttttttttttttttttttttttt$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t++++++++++++++++++++++t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$$$$$$$$$$$$$$$$$$$$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$ssssssssssssssssss$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s))))))))))))))))s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){{{{{{{{{{{{{{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<<<<<<<<<<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<<<<<<<<<<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<''''''''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<''''''''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<'';;;;''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
  for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<'';##;''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
  for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<'';##;''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<'';;;;''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<''''''''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<''''''''<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<<<<<<<<<<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){<<<<<<<<<<<<{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s){{{{{{{{{{{{{{)s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$s))))))))))))))))s$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$ssssssssssssssssss$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t+$$$$$$$$$$$$$$$$$$$$+t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$t++++++++++++++++++++++t$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$tttttttttttttttttttttttt$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c*$$$$$$$$$$$$$$$$$$$$$$$$$$*c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$c****************************c$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$cccccccccccccccccccccccccccccc$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,s$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$ssssssssssssssssssssssssssssssssssss$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$c****************************************c$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$cccccccccccccccccccccccccccccccccccccccccc$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t+$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+t$,t$*c$,t$(rof
##for($t,$c*$t,$t++++++++++++++++++++++++++++++++++++++++++++++t$,t$*c$,t$(rof
##for($t,$c*$t,$tttttttttttttttttttttttttttttttttttttttttttttttt$,t$*c$,t$(rof
##for($t,$c*$t,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,t$*c$,t$(rof
##for($t,$c*$t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,t$*c$,t$(rof
##for($t,$c*$tttttttttttttttttttttttttttttttttttttttttttttttttttttt$*c$,t$(rof
##for($t,$c*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*c$,t$(rof
##for($t,$c**********************************************************c$,t$(rof
##for($t,$cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc$,t$(rof
##for($t,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,t$(rof
##for($t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,t$(rof
##for($tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt$(rof
##for($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(rof
##for((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((rof
##forrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrof
##foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooof
##ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
##$kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk$
##$k++++++++++++++++++++++++++++++++++++++++++++k$
##$k+==========================================+k$
##$k+=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=+k$
##$k+=$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$=+k$
##$k+=$a[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[a$=+k$
##$k+=$a[$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$[a$=+k$
##$k+=$a[$oooooooooooooooooooooooooooooooo$[a$=+k$
##$k+=$a[$o++++++++++++++++++++++++++++++o$[a$=+k$
##$k+=$a[$o+$$$$$$$$$$$$$$$$$$$$$$$$$$$$+o$[a$=+k$
##$k+=$a[$o+$__________________________$+o$[a$=+k$
##$k+=$a[$o+$_]]]]]]]]]]]]]]]]]]]]]]]]_$+o$[a$=+k$
##$k+=$a[$o+$_]eeeeeeeeeeeeeeeeeeeeee]_$+o$[a$=+k$
##$k+=$a[$o+$_]eqqqqqqqqqqqqqqqqqqqqe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$$$$$$$$$$$$$$$$$$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$mmmmmmmmmmmmmmmm$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;;;;;;;;;;;;;;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<<<<<<<<<<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<<<<<<<<<<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<''''''''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<''''''''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<'';;;;''<<;m$qe]_$+o$[a$=+k$
  $k+=$a[$o+$_]eq$m;<<'';##;''<<;m$qe]_$+o$[a$=+k$
  $k+=$a[$o+$_]eq$m;<<'';##;''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<'';;;;''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<''''''''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<''''''''<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<<<<<<<<<<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;<<<<<<<<<<<<;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$m;;;;;;;;;;;;;;m$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$mmmmmmmmmmmmmmmm$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eq$$$$$$$$$$$$$$$$$$qe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eqqqqqqqqqqqqqqqqqqqqe]_$+o$[a$=+k$
##$k+=$a[$o+$_]eeeeeeeeeeeeeeeeeeeeee]_$+o$[a$=+k$
##$k+=$a[$o+$_]]]]]]]]]]]]]]]]]]]]]]]]_$+o$[a$=+k$
##$k+=$a[$o+$__________________________$+o$[a$=+k$
##$k+=$a[$o+$$$$$$$$$$$$$$$$$$$$$$$$$$$$+o$[a$=+k$
##$k+=$a[$o++++++++++++++++++++++++++++++o$[a$=+k$
##$k+=$a[$oooooooooooooooooooooooooooooooo$[a$=+k$
##$k+=$a[$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$[a$=+k$
##$k+=$a[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[a$=+k$
##$k+=$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$=+k$
##$k+=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=+k$
##$k+==========================================+k$
##$k++++++++++++++++++++++++++++++++++++++++++++k$
##$kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk$
##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}}
##}}$nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn$}}
##}}$n++++++++++++++++++++++++++++++++++n$}}
##}}$n+================================+n$}}
##}}$n+=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=+n$}}
##}}$n+=$kkkkkkkkkkkkkkkkkkkkkkkkkkkk$=+n$}}
##}}$n+=$k==========================k$=+n$}}
##}}$n+=$k==========================k$=+n$}}
##}}$n+=$k==4444444444444444444444==k$=+n$}}
##}}$n+=$k==4********************4==k$=+n$}}
##}}$n+=$k==4*$$$$$$$$$$$$$$$$$$*4==k$=+n$}}
##}}$n+=$k==4*$ssssssssssssssss$*4==k$=+n$}}
##}}$n+=$k==4*$s;;;;;;;;;;;;;;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<<<<<<<<<<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<<<<<<<<<<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<''''''''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<''''''''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<'';;;;''<<;s$*4==k$=+n$}}
  }}$n+=$k==4*$s;<<'';##;''<<;s$*4==k$=+n$}}
  }}$n+=$k==4*$s;<<'';##;''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<'';;;;''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<''''''''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<''''''''<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<<<<<<<<<<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;<<<<<<<<<<<<;s$*4==k$=+n$}}
##}}$n+=$k==4*$s;;;;;;;;;;;;;;s$*4==k$=+n$}}
##}}$n+=$k==4*$ssssssssssssssss$*4==k$=+n$}}
##}}$n+=$k==4*$$$$$$$$$$$$$$$$$$*4==k$=+n$}}
##}}$n+=$k==4********************4==k$=+n$}}
##}}$n+=$k==4444444444444444444444==k$=+n$}}
##}}$n+=$k==========================k$=+n$}}
##}}$n+=$k==========================k$=+n$}}
##}}$n+=$kkkkkkkkkkkkkkkkkkkkkkkkkkkk$=+n$}}
##}}$n+=$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=+n$}}
##}}$n+================================+n$}}
##}}$n++++++++++++++++++++++++++++++++++n$}}
##}}$nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn$}}
##}}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}pppppppppppppppppppppppppppp}}}
##}}}prrrrrrrrrrrrrrrrrrrrrrrrrrp}}}
##}}}priiiiiiiiiiiiiiiiiiiiiiiirp}}}
##}}}prinnnnnnnnnnnnnnnnnnnnnnirp}}}
##}}}printtttttttttttttttttttnirp}}}
##}}}print$$$$$$$$$$$$$$$$$$tnirp}}}
##}}}print$nnnnnnnnnnnnnnnn$tnirp}}}
##}}}print$n;;;;;;;;;;;;;;n$tnirp}}}
##}}}print$n;<<<<<<<<<<<<;n$tnirp}}}
##}}}print$n;<<<<<<<<<<<<;n$tnirp}}}
##}}}print$n;<<''''''''<<;n$tnirp}}}
##}}}print$n;<<''''''''<<;n$tnirp}}}
##}}}print$n;<<'';;;;''<<;n$tnirp}}}
  }}}print$n;<<'';##;''<<;n$tnirp}}}
  }}}print$n;<<'';##;''<<;n$tnirp}}}
##}}}print$n;<<'';;;;''<<;n$tnirp}}}
##}}}print$n;<<''''''''<<;n$tnirp}}}
##}}}print$n;<<''''''''<<;n$tnirp}}}
##}}}print$n;<<<<<<<<<<<<;n$tnirp}}}
##}}}print$n;<<<<<<<<<<<<;n$tnirp}}}
##}}}print$n;;;;;;;;;;;;;;n$tnirp}}}
##}}}print$nnnnnnnnnnnnnnnn$tnirp}}}
##}}}print$$$$$$$$$$$$$$$$$$tnirp}}}
##}}}printtttttttttttttttttttnirp}}}
##}}}prinnnnnnnnnnnnnnnnnnnnnnirp}}}
##}}}priiiiiiiiiiiiiiiiiiiiiiiirp}}}
##}}}prrrrrrrrrrrrrrrrrrrrrrrrrrp}}}
##}}}pppppppppppppppppppppppppppp}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
##}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

```

As you can see, every single character is part of a square. The top half of each big square is commented out by line comments, and the bottom half (including the second non-commented line) is escaped by a big here-document beginning in the middle of the square (`<<'';#`) and ending at the empty line after the big square.

This program was automatically generated from the following program:

```
($r,$c,@a)=@ARGV;
for$y(0..$r-1){
for$x(0..$c-1){
for$s(1..$c-$x-1){
$m=$a[$o=$c*$y+$x];$k=0;
for$t(1..$s){
for($t,$c*$t,$t+$c*$s,$c*$t+$s){
$k+=$a[$o+$_]eq$m;
}}$n+=$k==4*$s;
}}}print$n;

```

Which is a program to solve the same challenge, but (very lightly) golfed for bytes and short line lengths, not for score.

It takes as inputs first the height and width of the rectangle, then the individual characters of the rectangle separated by spaces. Example usage: To solve the rectangle

```
xxxyy
xxyyy
xyyxx

```

you call the program like this: `perl theprogram.pl 3 5 x x x y y x x y y y x y y x x`

Anyway, by squaring the code like this, it should be possible to get down to score 0 or 2 with any programming language that has a one-line solution and a line comment character, by transforming the one-liner

```
one-liner

```

like this:

```
##ooooooooooooooooooooo
##onnnnnnnnnnnnnnnnnnno
##oneeeeeeeeeeeeeeeeeno
##one---------------eno
##one-lllllllllllll-eno
##one-liiiiiiiiiiil-eno
##one-linnnnnnnnnil-eno
##one-lineeeeeeenil-eno
##one-linerrrrrenil-eno
##one-liner###renil-eno
  one-liner###renil-eno
##one-liner###renil-eno
##one-linerrrrrenil-eno
##one-lineeeeeeenil-eno
##one-linnnnnnnnnil-eno
##one-liiiiiiiiiiil-eno
##one-lllllllllllll-eno
##one---------------eno
##oneeeeeeeeeeeeeeeeeno
##onnnnnnnnnnnnnnnnnnno
##ooooooooooooooooooooo

```

The only characters not part of a square are the two spaces. Depending on the language, there may also be ways to use clever multiline strings or comments (like Perl's here-docs) or some sort of redundant code to make those two space characters into a square.

As for getting the byte count low, my solution has a bunch of immediate improvements, and I'm sure there are also lots of other ways to refine this technique, but I don't have the patience for that; I just wanted to provide a demonstration so you guys can go wild with it.

# Answer
> 2 votes
# Jelly,  ~~20 1917 1908~~  1905 bytes, Score  ~~20~~  0

```

ẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆ
ẆZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZẆ
ẆZṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡZẆ
ẆZṡLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLṡZẆ
ẆZṡLƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊLṡZẆ
ẆZṡLƊ€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€ƊLṡZẆ
ẆZṡLƊ€ẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZZZZZZZZZZZZZZZZZZZZZZZZZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZUUUUUUUUUUUUUUUUUUUUUUUZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$$$$$$$$$$$$$$$$$$$$$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€€€€€€€€€€€€€€€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎẎẎẎẎẎẎẎẎẎẎẎẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEEEEEEEEEEEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲƲƲƲƲƲƲƲƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€€€€€€€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€SSSSS€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€SqqqS€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€SqqqS€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€SqqqS€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€SSSSS€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€€€€€€€ƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲƲƲƲƲƲƲƲƲEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEEEEEEEEEEEẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎẎẎẎẎẎẎẎẎẎẎẎẎ€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢ€€€€€€€€€€€€€€€ḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢḢƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$ƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬƬ$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZU$$$$$$$$$$$$$$$$$$$$$UZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZUUUUUUUUUUUUUUUUUUUUUUUZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇZZZZZZZZZZZZZZZZZZZZZZZZZƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇƇṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖṖẎ€ƊLṡZẆ
ẆZṡLƊ€ẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎẎ€ƊLṡZẆ
ẆZṡLƊ€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€ƊLṡZẆ
ẆZṡLƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊƊLṡZẆ
ẆZṡLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLṡZẆ
ẆZṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡṡZẆ
ẆZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZẆ
ẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆẆ
⁽⁽ĿĿ
⁽⁽ĿĿ
```

A full program taking a list of lists of `.` and `+` characters. (Works for any characters, even more than 2).

**Try it online!**

~~Any attempt to reduce score by introducing characters seems to just increase the score more. I guess the way to win this challenge is to find the right Lenguage!~~

Since Magma posted a very nice Perl solution scoring \\$0\\$ I decided to attempt similar using my \\$20\\$ byte solution:

```
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€S

```

But Jelly has no comment character, so a little extra creativity is required...

Jelly programs consist of "Links" - code separated by newline characters, pilcrow characters ( \- interpreted as the same byte as newlines) or valid code-page characters lacking a defined behaviour (at present `q` is an example). The latter actually count as a Link each rather than a pure separator. Program execution starts at the bottom-right most Link, the "Main Link" (here `⁽⁽ĿĿ`).

We can call any other Link as a single-argument function (a "monad") by it's \\$1\\$-indexed position in the rest of the Links of the program using the "quick" `Ŀ`. These Links are indexable in a modular fashion; so if there are \\$n\\$ of them then the \\$n^\text{th}\\$ is the same as the \\$0^\text{th}\\$ and the \\$-m^\text{th}\\$ is the same as the \\$(-m\mod n)^\text{th}\\$.

So the program above starts by executing `⁽⁽ĿĿ`, where `⁽⁽Ŀ` is a representation of \\$-27150\\$ so `⁽⁽ĿĿ` calls the \\$-27150^\text{th}\\$ Link which, since there are \\$60\\$ Links (due to the extra lines and the central nine `q`s), is the \\$30 ^\text{th}\\$ Link, `ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€S`, to the left of the middle row of the \\$3\\$-by-\\$3\\$ square of `q`s. This is called with an argument of the program input:

```
ẆZṡLƊ€ẎṖƇZU$ƬḢ€ẎEƲ€S - Link: list of lists of characters, lines
Ẇ                    - contiguous slices (i.e [[line1], [line2], ..., [lineN], [line1, line2], [line2, line3], ..., [lineN-1, lineN], [line1, line2, line3], ...., [lineN-2, lineN-1, lineN], ..., ..., lines])
     €               - for each (slice):
    Ɗ                -   last three links as a monad:
 Z                   -     transpose - i.e. get the columns of the slice
   L                 -     length - i.e. get the number of lines in the slice
  ṡ                  -     overlapping slices of given length - i.e. get slices of columns of same length as lines in the original slice ...filled squares of all sizes (including 1-by-1)
      Ẏ              - tighten - from a list of list of squares of each size to a single list of all squares
        Ƈ            - filter keep if this is truthy:
       Ṗ             -   pop (truthy for all but the 1-by-1 squares)
                  €  - for each (filled square)
                 Ʋ   -   last four links as a monad:
            Ƭ        -     collect up until results are no longer unique:
           $         -       last two links as a monad:
         Z           -         transpose  }
          U          -         upend      } - together a quarter rotation
             Ḣ€      -     head each - i.e. first row of each rotation
               Ẏ     -     tighten - i.e. get the entire perimeter (with repeated corners)
                E    -     all equal? (1 if so, 0 otherwise)
                   S - sum

```

# Answer
> 2 votes
# MATL, ~~18~~ ~~16~~ ~~14~~ 10 characters not in squares (~~92~~ ~~103~~ ~~111~~ 127 bytes)

```
!!!ffqqqqQQQ%OOOOGGGxx44---@@&&lllxxTT  ~~~ ++||7 ss--~~~ssvvss
!!!ffqqqqQQQ"OOOOGGGxx44---@@&&lllxxTTYa~~~Z++||7Mss--~~~ssvvss

```

Input is a character matrix with characters `+` and `-`. Doesn't count diagonal squares.

Try it online!

Or verify all examples and test cases.

---
Tags: code-challenge
---


title: A sorcerer's spellbook

text:
# Question
Title: A sorcerer's spellbook
**Edit**: *I haven't played D&D before so when I initially made this question I didn't properly research it. I apologize for this, and I'm making a few edits that might invalidate answers to stay as true as possible to the dnd 5e rules. Sorry.*

---

A D&D fan from a recent Hot Network Question seems to have some trouble working out whether a sorcerer's chosen spells line up with the possibilities - and I think we should help!

### Introduction

*(all of this is already described in the previously mentioned question)*

A sorcerer knows two level 1 spells from start (level 1): `[1, 1]`

* Every time a sorcerer gains a level (except for levels 12, 14, 16, 18, 19 and 20) they learn a **new** spell (mandatory).
* Additionally, when leveling up one can choose (optional) to **replace** one of the spells with another.

The spells learned and replaced must be a valid spell slot level which is half your sorcerer's level rounded up. See this table:

```
Sorcerer level  Highest spell level possible
1               1
2               1
3               2
4               2
5               3
6               3
7               4
8               4
9               5
10              5
11              6
12              6
13              7
14              7
15              8
16              8
17              9
18              9
19              9
20              9

```

This means at level 3 one can have the spell levels `[1, 1, 2, 2]` like this:

```
Level 1: [1, 1] (initial)
Level 2: [1, 1, 1 (new)]
Level 3: [1, 1, 2 (replaced), 2 (new)]

```

It is not required to pick the highest level spells you have access to.

The spell levels `[1, 1, 1, 1]` are perfectly valid for a level 3.

Lastly, remember that replacing a spell is an optional option *for every level*. This means that some levels could skip the replace, while others make use of it.

### The challenge

Make a program or function that takes an integer (level) between 1 and 20.

It should also take an array of integers (spell levels) with values ranging from 1 to 9 in any order (9 is the maximum spell level).

The output of the program should be a truthy/falsy value validating if the chosen spell levels are valid for a sorcerer of the given level.

### Test cases

```
Level: 1
Spells: [1, 1]
Output: true

Level: 8
Spells: [1, 1, 2, 3, 3, 5]
Ouput: false

Reason: A level 8 can't ever have access to a level 5 spell.

Level: 5
Spells: [1, 1, 1, 2, 2, 2, 3]
Output: false

Reason: A level 5 can't have access to 7 spells

Level: 11
Spells: [3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6]
Output: false

Reason: Too many spell upgrades.
        The highest valid selection for level 11 is
        [3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6]

```

---

This is - fewest bytes wins!

# Answer
> 6 votes
# Java (JDK), 99 bytes

```
s->l->{var r=s.length!=l+1-l/12*(l-10)/2-l/19;for(var z:s)r|=z>(++l>30?9:l+(2<<l/25)>>2);return!r;}

```

Try it online!

## Credits

* Port of Arnauld's JavaScript answer.
However, the `l+1-l/12*(l-10)/2-l/19` part is a shortening in Java for `(l<12?l:l>16?14:l+11>>1)+1` which can't be done in JavaScript because it doesn't round to the floor integer.

---

Previous answer:

# Java (JDK 10), 191 bytes

```
L->S->{int m[]=new int[9],z=0,Z=0,l=0;for(m[0]++;l++<L;z+=--m[z]<1?1:0)m[Z=~-l/2-l/19]+=l<12?2:l>17?1:1+l%2;l=0;for(int s:S){if(--s>Z)l++;Z-=--m[Z>0?Z:0]<1?1:0;}for(int i:m)l|=i;return l==0;}

```

Try it online!

* **Input requirement:** the spell list must be ordered from greatest spell levels to the lowest one.

## Explanations

```
L->S->{                                        // Curried-lambda with 2 parameters: sorcerer-level and spell list
 int m[]=new int[9],                           // Declare variables: m is the max level  of each spell.
     z=0,                                      // z is the minimum spell level of the maximized spell list.
     Z=0,                                      // Z is the maximum spell level for the current level.
     l=0;                                      // l is first a level counter, then a reused variable
 for(m[0]++;l++<L;z+=--m[z]<1?1:0)             // for each level, compute the maximized known spells.
  m[Z=~-l/2-l/19]+=l<12?2:l>17?1:1+l%2;        //
                                               // Now m is the row for level L in the table below.
 l=0;                                          // l now becomes an error indicator
 for(int s:S){                                 // This loop checks if the spell-list matches the spells allowed for that level.
  if(--s>Z)l++;                                // Spell-levels are 1-based, my array is 0-based so decrease s.
  Z-=--m[Z>0?Z:0]<1?1:0;                       // Remove a max level if we've expleted all the spells, avoiding exception.
 }                                             //
 for(int i:m)l|=i;                             // Make sure there are no more values in m.
 return l==0;                                  // Return true if no miscount were encountered.
}

```

**Table 1:** Maximized spell distribution for each sorcerer-level, used from Axoren's answer on the linked question.

## Credits

# Answer
> 2 votes
# Charcoal, 51 bytes

```
Ｎθ≔⁺✂⭆”)⊟⊞<⁴H”×ＩκＩιθ⎇‹θ¹²⊕⊗θ⁺⁶⁺θ⊘⁺‹θ¹⁹θ¹0θ¬ΣＥＳ›ι§θκ

```

Try it online! Link is to verbose version of code. Takes spell levels in ascending order as a string. Explanation:

```
Ｎθ

```

Input the level.

```
≔⁺✂⭆”)⊟⊞<⁴H”×ＩκＩιθ⎇‹θ¹²⊕⊗θ⁺⁶⁺θ⊘⁺‹θ¹⁹θ¹0θ

```

Perform run-length decoding on the string `0544443335` resulting in the string `11111222233334444555566677788899999`. This string is then sliced starting at the level (1-indexed) and ending at the doubled level (if less than 12) or 6+1.5\*, rounded up, except for level 19, which is rounded down. A `0` is suffixed to ensure that there are not too many spells.

```
¬ΣＥＳ›ι§θκ

```

Compare the spell levels against the substring and prints a `-` if none of them are excessive.

# Answer
> 2 votes
# Python 3, 98 bytes

```
v=lambda L,S:(max(S)*2-2<L)&v(L-1,[1]+sorted(S)[:(chr(L*3)in'$*069<')-2])if L>1else(1,1)==tuple(S)

```

Try it Online!

Ungolfed:

```
def v(L, S):
    # recursion base case
    if L <= 1:
        return tuple(S) == (1, 1)
    # if the highest level skill is not valid for the level, then return False.
    if max(S)*2 - 2 < L:
        return False
    # hacky way to determine if the level gets a new skill
    has_new_skill = chr(L*3) in '$*069<'
    sorted_skills = sorted(S)
    # this step removes the highest skill and adds a level 1 skill (replacement)
    # if there is a new skill, then it removes the second highest skill as well
    new_skills = [1] + sorted_skills[:has_new_skill - 2]
    return v(L-1, new_skills)

```

*edit: corrected solution to use correct D&D rules*

# Answer
> 2 votes
# JavaScript (ES6),  ~~79~~  78 bytes

Takes input as `(level)(array)`. Returns \\$0\\$ or \\$1\\$.

```
l=>a=>!a.some(x=>x>(j--,++l>30?9:l+(2<<l/25)>>2),j=l<12?l:l>16?14:l+11>>1)&!~j

```

Try it online!

## Test code

Below is a link to some test code that takes the sorcerer level as input and returns an array of maximum spell levels, using the same logic as the above function.

Try it online!

## How?

### Reference table

```
 Sorcerer level | # of spells | Maximum spell levels
----------------+-------------+-------------------------------
        1       |      2      | 1,1
        2       |      3      | 1,1,1
        3       |      4      | 1,1,2,2
        4       |      5      | 1,2,2,2,2
        5       |      6      | 2,2,2,2,3,3
        6       |      7      | 2,2,2,3,3,3,3
        7       |      8      | 2,2,3,3,3,3,4,4
        8       |      9      | 2,3,3,3,3,4,4,4,4
        9       |     10      | 3,3,3,3,4,4,4,4,5,5
       10       |     11      | 3,3,3,4,4,4,4,5,5,5,5
       11       |     12      | 3,3,4,4,4,4,5,5,5,5,6,6
       12       |     12      | 3,4,4,4,4,5,5,5,5,6,6,6
       13       |     13      | 4,4,4,4,5,5,5,5,6,6,6,7,7
       14       |     13      | 4,4,4,5,5,5,5,6,6,6,7,7,7
       15       |     14      | 4,4,5,5,5,5,6,6,6,7,7,7,8,8
       16       |     14      | 4,5,5,5,5,6,6,6,7,7,7,8,8,8
       17       |     15      | 5,5,5,5,6,6,6,7,7,7,8,8,8,9,9
       18       |     15      | 5,5,5,6,6,6,7,7,7,8,8,8,9,9,9
       19       |     15      | 5,5,6,6,6,7,7,7,8,8,8,9,9,9,9
       20       |     15      | 5,6,6,6,7,7,7,8,8,8,9,9,9,9,9

```

### Number of spells

For a sorcerer of level \\$L\\$, the number of spells \\$N\_L\\$ is given by:

$$N\_L=\begin{cases} L+1&\text{if }L\<12\\ \lfloor(L+13)/2\rfloor&\text{if }12\le L\le 16\\ 15&\text{if }L\>16 \end{cases}$$

In the code, the variable \\$j\\$ is initialized to \\$N\_L-1\\$ and decremented at each iteration while walking through the input array. Therefore, we expect it to be equal to \\$-1\\$ at the end of the process.

### Maximum spell levels

Given a sorcerer level \\$L\\$ and a spell index \\$1\le i \le N\_L\\$, the maximum level \\$M\_{L,i}\\$ of the \\$i\\$-th spell is given by:

$$M\_{L,i}=\begin{cases} \lfloor(L+i+2)/4\rfloor&\text{if }L+i\<25\\ \lfloor(L+i+4)/4\rfloor&\text{if }25\le L+i\le 30\\ 9&\text{if }L+i\>30 \end{cases}$$

Each value \\$x\\$ of the input array \\$a\\$ is compared with this upper bound.

# Answer
> 0 votes
# Groovy, 155 bytes

```
def f(int[]a, int b){l=[1]
b.times{n->l[0]=++n%2?n/2+1:n/2
if(n<18&(n<12|n%2>0))l.add(l[0])
l.sort()}
for(i=0;i<a.size();)if(a[i]>l[i++])return false
true}

```

Generates the best possible spellbook, then checks that the spellbook passed into the method is not better.

Ungolfed, with implicit types made explicit:

```
boolean spellChecker(int[] a, int b) {
    // l will be our best possible spellbook
    List<BigDecimal> l = [1]
    b.times { n ->
        n++ // iterate from 1 to b, not 0 to b-1
        l[0] = n % 2 != 0 ? n / 2 + 1 : n / 2 // update the lowest value to the best permitted
        if (n < 18 & (n < 12 | n % 2 > 0))
            l.add(l[0]) // if permitted, add another best spell
        l.sort() // ensure 0th position is always worst, ready for updating next loop
    }
    for (int i = 0; i < a.size(); i++)
        if (a[i] > l[i]) // if the submitted spell is of a higher level
            return false // also rejects when l[i] is undefined. (too many spells)
    return true
}

```

Try it online!

---
Tags: code-golf, decision-problem
---


title: Divide the work

text:
# Question
Title: Divide the work
There is a job which can be decomposed into `x` equally-sized smaller tasks. You have a team of size `y <= x`, where every member works equally fast on any task. The goal for this challenge is to divide the work as evenly as possible such that every member of your team has at least 1 task to perform. As evenly as possible means that given any member `a`, the number of tasks it must perform may be at most one more than any other member `b`. A single task cannot be further divided or worked on simultaneously by two members.

# Input

Your program/function will take as input two positive integers `x` and `y`. `y` is guaranteed to be less than or equal to `x`. You are free to decide in what order your program will take these inputs. You may take the inputs from any input source desired.

# Output

Your program/function will output a list of positive integers of length `y` representing the number of tasks each member must perform. The list may be in any order. For example, the following outputs are identical:

```
2 1 2 1
1 1 2 2
2 2 1 1

```

Outputs may be to any output sink desired.

# Examples

Each line pair denotes inputs and one possible output. These example inputs specify `x` first.

```
1 1
1

4 1
4

4 2
2 2

4 3
2 1 1

4 4
1 1 1 1

10 3
3 4 3

10 7
1 2 2 2 1 1 1

```

# Scoring

This is code golf; shortest code wins. Standard loopholes apply.

# Answer
> 10 votes
# , 26 bytes

```
function(x,y)table(1:x%%y)

```

Try it online!

Counts the number of occurrence of `1,2,...,y` in `[1...x]` modulus y. 12 bytes golfed by @mnel, and than an additional 6 by @digEmAll.

# Answer
> 6 votes
# JavaScript (ES6), 34 bytes

Takes input as `(y)(x)`.

```
y=>g=x=>y?[k=x/y--|0,...g(x-k)]:[]

```

Try it online!

### Example for x = 10, y = 3

```
 Remaining tasks     | # of tasks for next worker | Workers
---------------------+----------------------------+-------------------------------------
 O O O O O O O O O O | 10 / 3 = 3.333... -> 3     | [   O O O ] [ pending ] [ pending ]
 O O O O O O O - - - |  7 / 2 = 3.5      -> 3     | [   O O O ] [   O O O ] [ pending ]
 O O O O - - - - - - |  4 / 1 = 4        -> 4     | [   O O O ] [   O O O ] [ O O O O ]

```

# Answer
> 6 votes
# Haskell, 25 bytes

```
x#y=map(`div`y)[x..x+y-1]

```

Try it online!

# Answer
> 3 votes
# Python 2, ~~40~~ ~~38~~ 36 Bytes

-2 bytes thanks to Mr. Xcoder

```
lambda x,y:x%y*[x/y+1]+[x/y]*(y-x%y)

```

Try it Online!

# Answer
> 3 votes
# Jelly, 3 bytes

```
sZẈ

```

**Try it online!**

### How?

```
sZẈ - Link: integer tasks, integer workers
    - implicit range(tasks)
s   - split into chunks of length workers
 Z  - transpose
  Ẉ - length of each

```

# Answer
> 2 votes
# Jelly, 3 bytes

```
œsẈ

```

Try it online!

-1 byte thanks to Jonathan Allan, showing me a built-in I haven't seen before. `œs` splits the range \\$\[1 \dots\\:x\]\\$ in \\$y\\$ similarly sized pieces, then  retrieves the length of each chunk.

# Answer
> 2 votes
# C (gcc), 48 bytes

Takes `i` task size, `j` team size and returns tasks to array `k`.

```
l;f(i,j,k)int*k;{for(l=j;l--;)k[l]=i/j+(i%j>l);}

```

Try it online!

# Answer
> 2 votes
# Brain-Flak, 68 bytes

```
({}([{}])<{({}(()))}{}>){({}[()]<({}<{({}<>)<>}>()){<>({}<>)}{}>)}{}

```

Try it online!

Initializes the stack with y ones, then rolls the stack x-y times, each time adding 1 to the former top of the stack.

# Answer
> 2 votes
# 05AB1E, 7 bytes

```
LôζðK€g

```

Try it online or verify all test cases.

```
L          # Create a list in the range [1, first (implicit) input]
           #  i.e. 10 → [1,2,3,4,5,6,7,8,9,10]
 ô         # Split it in chunks of size second (implicit) input
           #  i.e. [1,2,3,4,5,6,7,8,9,10] and 3 → [[1,2,3],[4,5,6],[7,8,9],[10]]
  ζ        # Zip, swapping rows and columns (with space as filling character by default)
           #  i.e. [[1,2,3],[4,5,6],[7,8,9],[10]] → [[1,4,7,10],[2,5,8,' '],[3,6,9,' ']]
   ðK      # Remove all those spaces
           #  i.e. [[1,4,7,10],[2,5,8,' '],[3,6,9,' ']]
           #   → [['1','4','7','10'],['2','5','8'],['3','6','9']]
     €g    # And then take the length of each inner list as result
           #  i.e. [['1','4','7','10'],['2','5','8'],['3','6','9']] → [4,3,3]

```

# Answer
> 2 votes
# MATL, 6 bytes

```
:gie!s

```

Try it online!

```
(implicit input, y)
:                            % range, push 1...x
g                            % convert to logical (all ones)
i                            % push y
e                            % reshape to matrix of y rows, padding with 0s
!s                           % row sums; as a row vector
(implicit output)

```

# Answer
> 1 votes
## Haskell, 41 bytes

```
x#y=[div x y+sum[1|i<=mod x y]|i<-[1..y]]

```

Try it online!

# Answer
> 1 votes
# Java, 55 bytes

By making use of `var` in Java 10 we can reduce by 1 byte.

```
y->x->{var a=new int[y];for(;0<x;a[x--%y]++);return a;}

```

Try it online!

Java 8 Version with 56 bytes

```
y->x->{int[]a=new int[y];for(;0<x;a[x--%y]++);return a;}

```

Works by just iterating over the array representing the workers until no tasks are left.

# Answer
> 1 votes
# Perl 5 `-a`, 31 bytes

```
$a[$_--%$F[1]]++while$_;say"@a"

```

Try it online!

# Answer
> 1 votes
# Groovy, 35 bytes

```
{x,y->o=[0]*y;while(x--)o[x%y]++;o}

```

Try it online!

# Answer
> 1 votes
# Brachylog, 14 bytes

```
⟨{h⟦₁}ġ₎t⟩z₁lᵐ

```

Try it online!

Port of Jonathan Allan's method \- range, split, transpose/zip, length - via the explanation in Kevin Cruijssen's answer. Can also be `tT&h⟦₁;Tġ₎z₁lᵐ` equivalently.

Another neat answer is:

### 19 bytes

```
h~+.ℕ₁ᵐ≜⟨⌉-⌋⟩<2&t~l

```

Try it online!

# Answer
> 1 votes
# UGL 1.0.0, 20 Bytes

```
CÑORCPFÐWC_+_WNINI

```

Port of the Haskell answer. (even though the language transpiles to Python)

Takes 2 inputs via stdin.

## Explanation (Warning: This may get out of hand)(update: it did not)

C takes two arguments, and apart from some other functions, it is (lambda x,y:map(x,y)).

Ñ is the alias of the int() function. (When you pass it as parameter instead of feeding arguments to it)

O takes one function with two arguments, and two anything, and applies the function to them.

R defines a lambda with two arguments: `lambda _,W: <stuff>`

C, again is map.

P is functools.partial

F is flip (lambda f: lambda x,y: f(y,x))

Ð is the alias of Division (/)

W is the second argument of the lambda we defined with R

C is exclusive range this time

_ is the first argument of the lambda we defined with R

+\_W is literally \_+W

NINI is two inputs taken from stdin and converted to integers.

So, this thing is (functions redefined in order to make the last line a bit short):

```
import functools as ft
apply2 = lambda x,y,z: x(y,z)
flip = lambda f: lambda x,y: f(y,x)
div = lambda x,y: x/y
rg = range
p = print
ip = input()
p(map(int,apply2(lambda _,W: map(ft.partial(flip(div),W),rg(_,(_+W))),int(ip()),int(ip()))))

```

# Answer
> 1 votes
# Lua, ~~50~~ 49 bytes

*1 byte saved thanks to DLosc* :)

```
x,y=...repeat print(x//y)x=x-x//y y=y-1until y<=0

```

Try it online!

The output is decimal and in different lines because of the way Lua implements the function print(), if this is a problem I can edit the submission to use io.write() instead.

# Answer
> 0 votes
# Clean, 39 bytes

Not much room for originality in this one.

```
import StdEnv
$x y=[i/y\\i<-[x..x+y-1]]

```

Try it online!

Defines the function `$ :: Int Int -> [Int]`, always returning higher numbers towards the end.

# Answer
> 0 votes
# Charcoal, 19 bytes

```
ＮθＮηＩＥθ⁻÷×⊕ιηθ÷×ιηθ

```

Try it online! Link is to verbose version of code. Based on Bresenham's line algorithm. Explanation:

```
Ｎθ                  Input team size
  Ｎη                Input number of tasks
      θ             Team size
     Ｅ              Map over implicit range
           ι    ι   Current value
          ⊕         Incremented
            η    η  Number of tasks
         ×     ×    Multiply
             θ    θ Team size
        ÷     ÷     Integer divide
       ⁻            Subtract
    Ｉ               Cast to string
                    Implicitly print

```

# Answer
> 0 votes
# APL (Dyalog), 19 bytes

*4 bytes saved thanks to @Adám*

```
{×⍵:(⍺-1)∇⍵-⎕←⌊⍵÷⍺}

```

Try it online!

# Answer
> 0 votes
# Forth (gforth), 47 bytes

```
: f tuck /mod swap rot 0 do 2dup i > - . loop ;

```

Try it online!

### Explanation

Divides the work by the number of people, add 1 to all workers whose index is less than (or equal) to the amount of excess work (the remainder/modulus)

### Code Explanation

```
: f            \ start a new word definition
  tuck         \ stick a copy of the number of workers in the "back" of the stack
  /mod swap    \ get the quotient and remainder, move remainder to the top
  rot          \ grab the copy of the number of workers from earlier and move it to the top
  0 do         \ start counted loop from 0 to #workers - 1
    2dup       \ duplicate the modulus and minimum work
    i >        \ check if the index is greater than the remainder
    -          \ subtract from minimum work (forth uses -1 for "true")
    .          \ output the result
  loop         \ end the counted loop
;              \ end the word definition

```

---
Tags: code-golf, arithmetic
---