[Question]
  [
Let's define a *self-contained number* as a positive integer, whose digits appear in runs of length equal to themselves only. In other words, any decimal digit **d** (excluding **0**) occurs only in runs of length exactly **d**.
# Task
You can choose any of the three methods listed below:
* Given an integer **n**, output the **n**th (either 0 or 1-indexed) self-contained number.
* Given an integer **n**, output the first **n** self-contained numbers.
* Print the sequence indefinitely.
# Examples
* **133322** is a self-contained number because **3** appears in a run of three **3**'s, **1** is single and **2** occurs in a run of two **2**'s.
* On the other hand, **35553355** isn't, because, although **5** and **3** occur five and three times respectively, they do not form runs of adjacent digits.
* **44422** is not self-contained, because **4** only occurs three times.
* **12222333** isn’t either, because **2** appears in a run of four **2**'s, and it cannot be treated as two separate runs of two **2**'s.
Not surprisingly, this is [OEIS A140057](https://oeis.org/A140057), and its first few terms are:
```
1, 22, 122, 221, 333, 1221, 1333, 3331, 4444, 13331, 14444, 22122, 22333, 33322, 44441, 55555, 122122, 122333, 133322, 144441, 155555
```
---
You can take input and provide output through any of [the standard methods](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods), in any [programming language](https://codegolf.meta.stackexchange.com/questions/2028/what-are-programming-languages), while noting that [these loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden by default. This is code golf, so the shortest code in bytes (in every language) wins.
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~104~~ ~~94~~ 83 bytes
-10 bytes thanks to Mr. Xcoder  
-11 bytes thanks to Jonathan Allan
```
i=0
while 1:
 if`i`==''.join(d*int(d)for c,d in zip(`-i`,`i`)if d!=c):print i
 i+=1
```
[Try it online!](https://tio.run/##DcxLCoAgFAXQuat4jdR@VEPh7UXIpBuhIkLU5s354aS3nDFstYIX8Zy4D1qNIHgLyyzlfEUE5XqEopz2MdM@OkKgD0nZCXZsUMOT63jXJuUGCS0YeK31Bw "Python 2 – Try It Online")
[Answer]
# Mathematica, 66 bytes
Prints the sequence indefinitely
```
Do[##&&Print@t&@@(#==Tr[1^{##}]&@@@Split@IntegerDigits@t),{t,∞}]
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78/98lP1pZWU0toCgzr8ShRM3BQUPZ1jakKNowrlpZuTYWKOAQXJCTWeLgmVeSmp5a5JKZnllS7FCiqVNdovOoY15t7P//AA "Wolfram Language (Mathematica) – Try It Online")
In TIO you have to terminate the execution in order to see the result but in Mathematica works fine.
-12 bytes from Martin Ender
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
Returns the nth term of the sequence, 1-indexed.
```
µNÔNγ€gJQ
```
[Try it online!](https://tio.run/##MzBNTDJM/f//0Fa/w1P8zm1@1LQm3Svw/39DIwA "05AB1E – Try It Online")
**Explanation**
```
µ           # loop over increasing N until counter equals input
 NÔ         # push N with consecutive equal elements deduplicated
   Nγ       # push N grouped into runs of consecutive equal elements
     €g     # get the length of each run
       J    # join to a number
        Q   # check for equality
            # if true, implicitly increment counter
```
[Answer]
# JavaScript (ES6), ~~76~~ ~~71~~ 68 bytes
Returns the *n-th* term of the sequence, 0-indexed.
```
f=(n,k)=>+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||n--?f(n,-~k):k
```
*NB*: As always with recursive functions, the input range depends on Tail Call Optimization support and the stack size of your engine.
### Demo
```
f=(n,k)=>+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||n--?f(n,-~k):k
for(n = 0; n <= 10; n++) {
  console.log('a(' + n + ') = ' + f(n))
}
```
---
# Alt. version, 65 bytes
Takes no input and prints the results with `alert()`, one at a time.
```
f=k=>f(-~k,+(k+'').replace(/(.)\1*/g,s=>s.length^s[0])||alert(k))
```
[Try it online!](https://tio.run/##HcZBDoMgEADAe1/hzd2qaM8NfsS2CcGFtmzAsKQn069j4pzma35GbP5sZYhppVqdDnp2MPxD30Ho2hZVpo2NJRhB4eN2HX0vehbFFH15v2SZnrjvhikXCIj1XKMbm6IkJsXJ3y8OsB4 "JavaScript (Node.js) – Try It Online") (Stops as soon as the maximum stack size is exceeded.)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
DŒrZEµ#
```
[Try it online!](https://tio.run/##y0rNyan8/9/l6KSiKNdDW5X//zc0AAA "Jelly – Try It Online")
-4 bytes thanks to [Mr. Xcoder](https://codegolf.stackexchange.com/users/59487/mr-xcoder)'s early suggestion.  
-1 thanks to [Jonathan Allan](https://codegolf.stackexchange.com/users/53748/jonathan-allan).
Takes input from STDIN.
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 20 bytes
```
1{_Abe`::=:*{_p}&)}h
```
[Try it online!](https://tio.run/##S85KzP3/37A63jEpNcHKytZKqzq@oFZNszbj/38A "CJam – Try It Online")
Explanation:
```
1                       push 1
 {                }h    while TOS is truthy (i.e. forever):            example iteration: 14444
  _                       duplicate                                                       14444 14444       
   Ab                     convert to base 10 (get decimal digits)                         14444 [1 4 4 4 4]
     e`                   run-length encode (array of two-element arrays)                 14444 [[1 1] [4 4]]
       :                  map over the array:
        :                   fold between the two array elements with:
         =                    equality                                                    14444 [1 1]
          :               fold between the array elements with:
           *                multiplication (a.k.a. logical AND for 1 or 0)                14444 1
            {  }&         if this yields a result of 1:
             _              duplicate the number and                                      14444 14444
              p             print it                                                      14444 (output 14444)
                 )        increment the number                                            14445
```
[Answer]
# [Haskell](https://www.haskell.org/), 70 bytes
```
import Data.List
filter(all(\s->read[s!!0]==length s).group.show)[1..]
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 10 bytes
```
≜ℕẹḅ⟨l=h⟩ᵐ
```
[Try it online!](https://tio.run/##ATMAzP9icmFjaHlsb2cy/37ihrDigoHhuoniiqX/4omc4oSV4bq54biF4p@obD1o4p@p4bWQ//8 "Brachylog – Try It Online")
Infinitely [generates](https://codegolf.meta.stackexchange.com/a/10753/85334) elements of the sequence through its input variable. (If it actually has to do the printing itself, append `&ẉ⊥`.) This is essentially code to solve the corresponding [decision-problem](/questions/tagged/decision-problem "show questions tagged 'decision-problem'") with a `≜` prepended to brute-force the smallest solutions first:
```
        ᵐ    For every
  ḅ          run of
 ẹ           digits in
             the input variable
ℕ            (which is a non-negative integer),
   ⟨l  ⟩     its length
   ⟨  h⟩     and its first element
   ⟨ = ⟩     are equal.
```
I expected this to only take 9 bytes, but `ḅ` seems to require an explicit `ẹ` to separate the digits of a number into runs.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 10 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
Ç≡∟Öz≈¢αV¢
```
[Run and debug it](https://staxlang.xyz/#p=80f01c997af79be0569b&i=&a=1)
This program filters all positive integers with a filter. The digits are run-length encoded. For each run, the digit must equal the run length.
[Answer]
# JavaScript 4, ~~83~~ 80 bytes
```
for(i=0;;)+(++i+'').replace(/(.)\1*/g,function(x,y){return y^x.length})||alert(i)
```
```
for(i=0;i<1000;)+(++i+'').replace(/(.)\1*/g,function(x,y){return y^x.length})||alert(i)
```
[Answer]
# [Perl 6](http://perl6.org/), 49 bytes
```
{(grep /^((\d)$0*:<?{$/.comb==$0}>)+$/,^Inf)[$_]}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WiO9KLVAQT9OQyMmRVPFQMvKxr5aRV8vOT83ydZWxaDWTlNbRV8nzjMvTTNaJT629r@1QnFipUJuYgHQAB2FOFPr/wA "Perl 6 – Try It Online")
Returns the `n`-th element of the sequence, zero-indexed.
[Answer]
# [R](https://www.r-project.org/), 56 bytes
```
function(n)all((r=rle(el(strsplit(c(n,''),''))))$l==r$v)
```
[Try it online!](https://tio.run/##FYlBCsMwDATvfUYIRAIdapkc/Zg0talBdYzsFvJ6RxkYBnZ1SH7ppidM39g/x7tN@EhhpF/Zez4KFNxEADSoRIgCrWurkjvsUGhZ8NaYJQSd/zjaVquc9j7JOfJ@9atBjpjJmcz36i3MVqSE4wI "R – Try It Online")
Makes use of run length encoding on the split number. Returns true if all the lengths equal the values.
Note: I have loaded the `methods` library in TIO to get `el` to work.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 10 bytes
```
fȯΛ§=L←gdN
```
[Try it online!](https://tio.run/##ASAA3/9odXNr/@KGkeKCgf9myK/Om8KnPUzihpBnZE7///8xMg "Husk – Try It Online")
Returns an infinite list. `↑` is used to show first n elements since it times out.
[Answer]
# Pyth, 12
```
.f.AqMr8jZ10
```
[Test online!](https://pyth.herokuapp.com/?code=.f.AqMr8jZ10&input=9&debug=0)
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 48 bytes
```
++$\=~s|(.)\1*|$1-length$&&&redo|gre for 1..$_}{
```
[Try it online!](https://tio.run/##K0gtyjH9/19bWyXGtq64RkNPM8ZQq0bFUDcnNS@9JENFTU2tKDUlvya9KFUhLb9IwVBPTyW@tvr/f0ODf/kFJZn5ecX/dX1N9QwMDf7rFgAA "Perl 5 – Try It Online")
Returns the n-th element, 1 indexed.
[Answer]
# Java 10, 121 bytes
A lambda from `int` to `int`. The function takes an index *n* and returns the *n*th (1-indexed) sequence value.
```
n->{int x=0,m=1;for(;n>0;n-=m,m=1)for(var p:(++x+"").split("(?<=(.))(?!\\1)"))m=p.length()==p.charAt(0)-48?m:0;return x;}
```
[Try It Online](https://tio.run/##ZY/NasMwEITP9lOoPq1ILGzooURxQikUCi09pLemB8WRHbn6MdI6pIQ8uyuTHAo97fDN7LLTiaPIu/332A87rWpSaxECeRPKknOa3GBAgXEcndoTEy3YoFe2/fwiwreBTsmki4fYgEqzZrA1KmfZi8Xnm15GLVvpV6Qh1Wjz1VlZJKeqmJuq5I3zwO2q4DavzEToRI7Ck34Bs9lplmWUhV4rhAzWywoYpbC@225LmlFqqp5paVs8AK2irg/CPyIUNL9/WJtFwb3EwVty4pcxSXgan938BJSGuQFZH5ugthDpnw4BvRRmarC5Ki9sK6Gck7KgUzRhRvQf7n3XQXMDtdNa1gj/rjxdDecDQ/eqAgKdVihPk0t6GX8B)
## Ungolfed
```
n -> {
    int x = 0, m = 1;
    for (; n > 0; n -= m, m = 1)
        for (var p : (++x + "").split("(?<=(.))(?!\\1)"))
            m = p.length() == p.charAt(0) - 48 ? m : 0;
    return x;
}
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 15 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Returns the `n`th term, 1-indexed.
```
Èì òÎeÈÎ¶XÊÃ}iU
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=yOwg8s5lyM62WMrDfWlV&input=OA)
```
Èì òÎeÈÎ¶XÊÃ}iU     :Implicit input of integer U
È                   :Function taking an integer X as argument
 ì                  :  To digit array
   ò                :  Partition between elements where
    Î               :    The sign of their difference is truthy (not 0)
     e              :  All true
      È             :  When passed through the following function as X
       Î            :    First element
        ¶           :    Is equal to
         XÊ         :    Length of X
           Ã        :  End function
            }       :End function
             iU     :Get the Uth integer that returns true
```
]