[Question]
  [
# Background
The [Look and Say Sequence](https://en.wikipedia.org/wiki/Look-and-say_sequence) is built up by reading off the digits of previous entries in the sequence, counting the number of digits in groups of the same digit. For example:
```
1 => 11 # (one 1)
11 => 21 # (two 1s)
21 => 1211 # (one 2, then one 1)
1211 => 111221 # (one 1, then one 2, then two 1s)
```
This sequence was the subject of various [previous](https://codegolf.stackexchange.com/questions/70837/say-what-you-see) [challenges](https://codegolf.stackexchange.com/questions/41974/look-and-say-conway-revisited). A simple Python 3 implementation of the generating function is:
```
>> from itertools import groupby
>> def look_and_say(obj):
      return "".join(f"{len(list(group))}{digit}" for digit, group in groupby(str(obj)))
```
[Try it online!](https://tio.run/##VY3BCoMwEETvfsWSUwKlkPbWnxHFxK6Nu2GzHoL47SqWHjqngWHey1XfTM99j8IzoAZR5lQA58yiMAovua/NECIk5k/b0dCWrlruJ/dq4IwEXYTAmPvESDaaNQWyCYva6@3ctg44om4GIgtc/fYlA9JPYYvKRXVuz4Kk9s9n/MN7b87xAA "Python 3 – Try It Online")
# Challenge
The purpose of this challenge is to implement an inverse Look and Say function. **Given a Look and Say representation of a non-negative integer, your code should return the *smallest* non-negative integer for which the input is its Look and Say**. Note that this is not the same as general [Run Length Decoding](https://codegolf.stackexchange.com/questions/12902/run-length-decoding), since the output must encode back *precisely* to the input: see the examples below for some of the subtleties involved.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code in bytes wins, with all the standard loopholes forbidden. Input can be passed in (or returned) as either an integer or a string.
# Test cases
```
10 => 0 # (1 zero)
12 => 2 # (1 two)
123 => 333333333333 # (12 threes)
1234 => 2444 # (1 two, 3 fours)
1232 => 222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 # (123 twos, since 2222 would return 42 not 1232)
2019 => 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 # (201 nines, since the only non-negative starting with 0 is 0)
2109 => 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 # (210 nines, since 11 would return 21 not 2109)
123456 => 244466666
123454 => 333333333333444444444444444444444444444444444444444444444 # (12 threes, 45 fours)
124234 => 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222444 (124 twos, 3 fours, which is smaller than the alternatives)
```
# Invalid inputs
Since there are no integers which have the following Look and Say representations, the behaviour for these inputs is undefined:
```
1
3
20
100
1010
10010
```
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 11 bytes
```
∞<ÅΔÅγs.ιJQ
```
[Try it online!](https://tio.run/##yy9OTMpM/f//Ucc8m8Ot56YA8eZivXM7vQL//zc0MjYBAA "05AB1E – Try It Online")
Computes the look and say of all integers until finding one that matches the input. Will timeout for large outputs.
[Answer]
# JavaScript (ES6), 111 bytes
```
f=([x,y,...s],p,o=O='',m='')=>x?y?f([y,...s],p,o,m+=x,+m&&y^p&&f(s,y,o+y.repeat(m))):O||0:!O||O[o.length]?O=o:0
```
[Try it online!](https://tio.run/##lZDBioMwEIbv@xRuD5pgmsY0FRSyPoIPECxIN9oWNVKlKPTd3dhVNm3tYf/DwPB9GWZyTq9pc7ic6nZdqW85DBkHokM9whg3CaqR4jF3HFTqAvlXF/VRBoSBUenyDrmlbff72rYz0OjHyu3xRdYybUEJIQzj242En7rGQuFCVnl7TKKYq5AMB1U1qpC4UDnIwMojKwitezYbi3w8Y2pi@oq3M9d4a2TBZJM6DmKMLRj0zxC6tTrLoU7yLFLiBYao21EMFkSPPIgeeSOOy@38uzot549Z1Nismdey/@R1LJs/5/dwNh0utJwMPw "JavaScript (Node.js) – Try It Online")
### Commented
```
f = (                     // f is a recursive function taking:
  [x, y,                  //   x, y = next two characters from the input string
         ...s],           //   s[]  = array of remaining characters
  p,                      //   p    = previous repeated digit, initially undefined
  o =                     //   o    = current output
  O = '',                 //   O    = shortest output so far
  m = ''                  //   m    = current multiplier, as a string
) =>                      //
  x ?                     // if x is defined:
    y ?                   //   if y is defined:
      f(                  //     do an unconditional recursive call:
        [y, ...s], p, o,  //       where y is put back in the input
        m += x,           //       and x is appended to m
        +m &&             //       if m is not zero'ish
        y ^ p &&          //       and y is not equal to p:
        f(                //         do another recursive call:
          s, y,           //           where p is updated to y
          o + y.repeat(m) //           and y is appended to o, m times
        )                 //       end of inner recursive call
      )                   //     end of outer recursive call
    :                     //   else:
      O || 0              //     return O, or 0 if it's empty
  :                       // else:
    !O || O[o.length] ?   //   if O is still empty or o is shorter than O:
      O = o               //     update O to o
    :                     //   else:
      0                   //     do nothing
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 13 bytes
```
DŒrUDF⁼³µ1#@0
```
[Try it online!](https://tio.run/##y0rNyan8/9/l6KSiUBe3R417Dm0@tNVQ2cHg////hjpGOsY6JgA "Jelly – Try It Online")
A full program that takes a list of digits and returns the first number that generates that as its look-and-say. Tries all numbers until one matches so will be slow for some inputs.
# Alternatives
## [Jelly](https://github.com/DennisMitchell/jelly), 25 bytes
```
DŒṖṪxḌƊ€F$€ḌDŒrUƊDF⁼ʋƇDṢḢ
```
[Try it online!](https://tio.run/##y0rNyan8/9/l6KSHO6c93Lmq4uGOnmNdj5rWuKkACSAHKFMUeqzLxe1R455T3cfaXR7uXPRwx6L/1o8a5ijo2ik8aphrfbid63A7UHnk///RhgY6CoZGIGwMJkzAJFDAyMDQEkgaGlhCxE3NoDRYhQmQFQsA "Jelly – Try It Online")
A monadic link taking an integer. Generates all possible inverse look-and-says (including invalid ones) and then tests them by seeing whether generating a look-and-say results in the input. Returns the smallest valid one.
## [Jelly](https://github.com/DennisMitchell/jelly), 30 bytes
```
ŒṖUZḢEƲƝẸ$ÐḟṪxḌƊ€€ẠƇF€ḌD$ƑƇḌṢḢ
```
[Try it online!](https://tio.run/##y0rNyan8///opIc7p4VGPdyxyPXYpmNzH@7aoXJ4wsMd8x/uXFXxcEfPsa5HTWuA6OGuBcfa3UCMHT0uKscmHmsHMh7uXATU99/6UcMcBV07hUcNc60Pt3O5HG5XASqM/P8/2tBAR8HQCISNwYQJmAQKGBkYWgJJQwNLiLipGZQGqzABsmIB "Jelly – Try It Online")
A monadic link taking a list of digits. Generates all possible inverse look-and-says (including invalid ones) and then tests them by seeing whether they violate any rules. Returns the smallest valid one.
Thanks to @ØranJohansen for pointing out a problem with the last two.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~200~~ ~~166~~ 165 bytes
```
lambda n:min(int(r)for r in b(n)if str(int(r))==r)
b=lambda n:[int(n[:i])*n[i]+v for i in range(1,len(n))for v in b(n[i+1:])if int(n[:i])*(n[i]!=v[:1])]if n else['']
```
[Try it online!](https://tio.run/##TZDdboQgEIXvfQqavYBRLgTZTSThSSgXmmpL4s5uWGPSp7eM2o2EvznD@QZ4/s4/D9Tr6D7Xqbv3Xx1De48oIs4iwfhILLGIrBcIcWSvOR0ZcC5B0bu3yZOO3sYAJfoYqoWRO5I7dfg9CCWnATNnoy4H1cdK2UDsk5/k8OEWb1WAkHPIhuk1eM7DeiE3klvUUkty8YaXSoPUxphd0CQ0sActL3WtToGq97M3aidCxQ0vzRVODLNpDYAtLuyZcoJNgn4hP6Mo3ldRtVQ694aGoUnLXLOVuVa7adfbvlDS5I0tDtz4j1v/AA "Python 2 – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 81 bytes
```
->n{(0..1/0.0).find{|r|r.to_s.chars.chunk{|w|w}.map{|a,b|[b.size,a]}*''==n.to_s}}
```
[Try it online!](https://tio.run/##JYvRDoIgFEB/pTez0RWqV/oRxhxYLOcihzlR7v121NrOztM5YbRzdjKf7z4dOYCoOPASXOsfCQMG@H7qAZqXCbtH3yWccCJ4mz6hYRaVhaFdnsxoOhWFlP53EGUlOBOXjetN//OI/UFF5lTUmvIK "Ruby – Try It Online")
Brute force approach, could be 5 bytes shorter on a newer version of Ruby, but it doesn't matter because it's slow as hell for most numbers.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 16 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Brute force approach that'll crap out for larger outputs.
```
@¥Xò¦ ËÎiDlÃq}as
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=QKVY8qYgy85pRGzDcX1hcw&input=MTIzNA)
```
@¥Xò¦ ËÎiDlÃq}as     :Implicit input of integer U
@                    :Function taking an integer string X as argument
 ¥                   :  Test U for equality with
  Xò                 :  Partition X on
    ¦                :    Inequality
      Ë              :  Map each D
       Î             :    First character of D
        i            :    Prepend
         Dl          :    Length of D
           Ã         :  End map
            q        :  Join
             }       :End function
              a      :Return the first integer that returns true when passed through that function
               s     :After converting it to a string
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 67 bytes
```
⊞υ⟦0ωＳ⟧Ｆυ«≔§ι²θＦ∨‹θ1Ｌθ¿∧κ⁻§θκ§ι⁰⊞υ⟦§θκ⁺§ι¹×§θκＩ…θκ✂θ⊕κ⟧»Ｉ⌊ＥΦυ¬⊟ιＩ⊟ι
```
[Try it online!](https://tio.run/##ZY9Pa8MwDMXPzacwOcngQdN1p57CYBBo1kB3GzuExE1EHTvxnzIY@@yekrJSNoEOkn7v8dT0tW1MrWKsgushCPaerlPBUupCj8EfvUXdAf/gu@RkLIPA2Veyyp3DTkPuC93KT0DBNlywiaDVQh0s7KVzMJFVltJpL3Xne5g45wxPDHLdwlmwEnVwNxuiz8Teua75LLhl@wNW6k5MdEa7NxzkP8fn2nkojWqvCyrBjgobOc@FbqwcpPaSIvHl0@@korc9XHWocQgDlPUIL6i8tHOWV@OhMiPgYraAvyPVLsZs87h92saHi/oB "Charcoal – Try It Online") Link is to verbose version of code. Generates all integers with the given look-and-say sequence and outputs the minimum thereof. Explanation:
```
⊞υ⟦0ωＳ⟧
```
Start by pushing a list of three items to the predefined list. The first item (initially `0`) represents the previous repeated digit (thus preventing the generated number from beginning with `0`), the second item (initially an empty string) represents the integer so far (as a string), while the third item (initially the whole input) represents the remaining input to process.
```
Ｆυ«
```
Loop over the list. This automatically iterates over additional items as they are pushed to the list.
```
≔§ι²θ
```
Get the remaining input into a dedicated variable. This saves several bytes as this variable is used multiple times. It does force me to wrap the block in braces, but this is offset by allowing me to use an `if` to save a byte.
```
Ｆ∨‹θ1Ｌθ
```
Loop over the string, but if it begins with a zero, only loop once (which gets ignored anyway, but this is a byte golfer than not looping at all).
```
¿∧κ⁻§θκ§ι⁰
```
Ignore the first character of the string (because there is no number to multiply it by), and also any character equal to the previously processed digit for this entry.
```
⊞υ⟦§θκ⁺§ι¹×§θκＩ…θκ✂θ⊕κ⟧
```
Push the next potential step to the list, comprising the current digit, the integer so far with the current digit repeated the prefix number of times, and the suffix.
```
»Ｉ⌊ＥΦυ¬⊟ιＩ⊟ι
```
After all possible steps have been processed, filter those that consumed the input exactly, and output the smallest resulting value.
]