Dekripsi format Wechsler yang diperluas

18

Tantangan ini didasarkan pada pos kotak pasir oleh user48538 . Karena dia tidak lagi aktif di situs ini, saya mengambil alih tantangan ini.


apgsearch , program pencarian sup yang didistribusikan untuk Conway's Game of Life dan database hasil pencarian, Catagolue menggunakan apgcodes untuk mengklasifikasikan dan menunjukkan pola. apgcodes sendiri menggunakan format Wechsler yang diperluas , perpanjangan dari notasi pola yang dikembangkan oleh Allan Wechsler pada tahun 1992.

Contoh berikut dan gambar diambil dari LifeWiki .

  1. String n karakter dalam set yang cocok dengan regex [0-9a-v]menunjukkan strip dengan lima baris, lebar n kolom. Setiap karakter menunjukkan lima sel dalam kolom vertikal sesuai dengan bitstrings [ 00000, 10000, 01000... 00010, 10010, 01010, 11010... 11111].

    Misalnya, 27deee6berkorespondensi dengan pesawat ruang angkasa kelas berat :

27deee6

[01100001101111011111100111100000000]
  1. Karakter zmemisahkan strip lima baris yang berdekatan.

    Misalnya, 0ca178b96z69d1d96berkorespondensi dengan still life 31-bit:

masukkan deskripsi gambar di sini

masukkan deskripsi gambar di sini

[000110110001010101010010001011001110000000000011111000100000100101010100011011000000000000]
  1. Karakter wdan xdigunakan untuk menyingkat 00dan 000, masing-masing.

    Jadi w33z8kqrqk8zzzx33sesuai dengan antar-jemput lebah ratu :

masukkan deskripsi gambar di sini

masukkan deskripsi gambar di sini

(10 baris kosong dihilangkan)

masukkan deskripsi gambar di sini

[0011000001100000000000000000000000000010000011100010001010111010111110000000000000000000000000000000000000000000000000000000000000000000000000011000001100000000000000000000000]
  1. Akhirnya, simbol-simbol yang cocok dengan regex y[0-9a-z]berhubungan dengan berjalan antara 4 dan 39 berturut-turut 0.

    Contoh yang baik adalah 31a08zy0123cko, terkait dengan kapal di quadpole :

masukkan deskripsi gambar di sini

masukkan deskripsi gambar di sini

[1100000000101000000000000000000010100000000000000000001010000000011000000000011000000001010000000011]

The Challenge

Write a program or a function to parse a string of the extended Wechsler format, defined above, and print (or return) the pattern corresponding to this string.

You may assume that the string is nonempty, and does not start or end with z.

You may use any reasonable output format, e.g., a string, a matrix, a 2d array. You may use any two values to represent 0 and 1, given that you declare them in the answer.

You may omit the trailing zeroes lines in the output, or add extra trailing zeroes lines. You may also add/omit trailing zeroes on each line, as long as all lines have the same length in the output.

You may return the transpose of the array, if that is more convenient.

This is , so the shortest code wins.

Test cases

153 => [[1, 1, 1], [0, 0, 1], [0, 1, 0], [0, 0, 0], [0, 0, 0]]
27deee6 => [[0, 1, 1, 0, 0, 0, 0], [1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]]
0ca178b96z69d1d96 => [[0, 0, 0, 1, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0], [1, 0, 1, 0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
w33z8kqrqk8zzzx33 => [[0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 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, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [1, 0, 1, 1, 1, 0, 1], [0, 1, 1, 1, 1, 1, 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, 0, 0, 0],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 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 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]]
alephalpha
sumber
Will z ever occur at the very beginning?
Mr. Xcoder
@Mr.Xcoder The LifeWiki page does not say anything about that. But you can assume here that the string does not start or end with z.
alephalpha
3
Not really a Game of Life pattern, but just for fun: vv99f60vv99f60uv11320uv19rqz33y133y11322310132231
Arnauld
1
"You may return the transpose of the array" - if that's OK, what about other symmetries like upside-down etc...?
Jonathan Allan
1
Two of the first three answers fail for some of these: 1yx1, 1yy1, 1yw1 and 1yz1. Maybe they should be tests.
Jonathan Allan

Jawaban:

2

Charcoal, 50 bytes

UB0≔⁺⭆χιββFS¿υ×0⁺⊟υ⌕βι≡ιz×⸿⁵y⊞υ⁴x×0³w¦00«P↓⮌⍘⌕βι²→

Try it online! Link is to verbose version of code. Uses 1 and 0. Explanation:

UB0

Set the background, i.e. any unprinted cells of the rectangle enclosing the output, to 0.

≔⁺⭆χιββ

Prefix the digits to the predefined lower case alphabet.

FS

Loop over the input string.

¿υ

If the predefined empty list is not empty...

×0⁺⊟υ⌕βι

... then print a number of 0s given by the sum of the number popped from the list and the index of the current character in the digits and letters. See below for what that number always is.

≡ι

Switch over the current character.

z×⸿⁵

If it's a z then output 5 carriage returns, taking us to the next output strip.

y⊞υ⁴

If it's a y, then push 4 to the predefined empty list, causing 4+n 0s to be output next time.

x×0³

If it's an x then output 3 0s. (This is done via repetition to avoid the literal 0 touching the x or the following literal.)

w¦00

If it's a w then output 2 0s. (The ¦ is needed to separate the two string literals.)

«P↓⮌⍘⌕βι²→

Otherwise, index the current character in the digits and letters, convert to binary, and print the result downwards least significant bit first; then move the cursor right for the next column.

Neil
sumber
6

JavaScript (ES8), 197 bytes

Takes input as a string. Returns an array of strings with '#' and spaces. The output may include extra (but consistent) trailing spaces on each line.

s=>[x='x',...s.replace(/w|x|y./g,s=>''.padEnd(s<x?2:s>x?P(s[1],36)+4:3),P=parseInt)].map(c=>[!++w,1,2,3,4].map(i=>c<x?o[y+i-5]+=' #'[P(c,36)>>i&1]:o=[...++y&&o,'']),w=y=o=[])&&o.map(r=>r.padEnd(w))

Try it online! (prettified output)

How?

Global variables

  • The character "x" is used several times, so it's worth storing it into the variable x.
  • The function parseInt is used twice, so it's worth storing it into the variable P.
  • y is the row index, initialized to 0.
  • w keeps track of an upper bound of the width which is used to pad the final output.
  • o[ ] is the output array, initially empty.

Pre-processing of repeated zeros

We first replace all patterns "w", "x" and "yX" in the input string with the appropriate number of spaces. These spaces will later be interpreted as "0".

s.replace(
  /w|x|y./g,
  s => ''.padEnd(
    // w --> 2
    s < x ? 2 :
    // yX --> value of X + 4
    s > x ? P(s[1], 36) + 4 :
    // x --> 3
    3
  )
)

Decoding

We split the resulting string, prepend an initial "x" and iterate 5 times (with i = 0 to 4) on each character c:

  • If c is lower than "x", we append the corresponding pattern to the next 5 rows.

    o[y + i - 5] += ' #'[P(c, 36) >> i & 1]
  • If c is greater than or equal to "x", we allocate 5 new empty strings in o[ ] and add 5 to y. This is triggered by the initial "x" that was added at the beginning of the string, or by any "z" in the original content.

    o = [...++y && o, '']

Padding

Finally, we pad each string in o[ ] with spaces so that they all have w characters.

o.map(r => r.padEnd(w))
Arnauld
sumber
6

05AB1E, 148 132 98 bytes

I'm a highschool student and this was my first time for both golfing and using 05AB1E, so comments are appreciated!

„YY¾38×:'Yð:žLRDUsð«#εNĀiDнXsk4+s¦sú]'W¾2×:'X¾3×:J'Z¶:.Bð¾:vNUyvDykDNX‚Š32VY‹iY+b¦RX_i¶ì}‚ˆ]¯{€¦˜J

Try it online!

Try it online!

Try it online!

Takes input as uppercase and outputs the transposed matrix as multiple output lines of 1s and 0s. May add extra zeros.

If you want to test with lowercase strings, add u in the TIO header.

If you want pretty-printed output, add '1'█:'0'.: in the TIO footer.

Explanation

(I'm calling "rows" and "columns" opposite of what you might expect because it generates the transposed matrix)

The basic algorithm is:

  1. Replace "yy" with 38 0s
  2. Split on "y" and and expand the 0-runs.
  3. Replace "w" and "x"
  4. Figure out the longest column (that is, the longest string between z's) and pad all the other columns so they are that length. (This is necessary because of how the algorithm below works)
  5. Split on z
  6. At this point, the input string is an array of columns where each column is a string of [0-9A-V], where each column is the same length.
  7. The algorithm to get it into the output format is to
    1. Convert the characters to numbers by using indexOf in a lookup string
    2. Convert the characters to binary and then pad to length 5
    3. If it is the first column, add a linebreak before the binary number
    4. Add a prefix to the beginning of the binary string that stores the row and then the column of the character.
    5. Push the binary string with prefix to 05AB1E's "global array" register/variable
  8. Sort the global array. The prefix string, which determines the sort order, will make sure everything ends up in the right order and the linebreaks are in the right places.
  9. Remove the prefix string from each element of the global array
  10. Join the array with "" and print it.

There are some other minor details you can see below in the expanded code. Everything after the tabs at the end of a line is a comment and can be ignored. (This comment scheme is not part of 05AB1E, by the way. I did it this way because it looked nice.) Lines that have comments starting with "@" are for debugging purposes and can be omitted without changing the final output.

„YY¾38×:                                        Replace "YY" with 38 0's. (y is the 34th character in the index, so need to add 34+4 = 38). ¾ is initialized to zero, which saves one byte from typing '038
'Yð:                                            Replace "Y" with " "                                
"Replaced input Stage1 = "?D,                   @DEBUG

žLR                                             Pushes [0-9A-Za-z]
DU                                              Set X to [0-9A-Za-z] and leave [0-9A-Za-z] on the stack.
s                                               Swap top two elements. Now the stack is [0-9A-Za-z],input

ð«                                              Append a space so the below splitter works even if there are no other spaces.
#                                               Split input on " ", which "Y" has been replaced with
ε                                               Map. This map replaces the y runs
    "Element = "?D,                             @DEBUG
    "N = "?N,                                   @DEBUG
    NĀi                                         If N != 0   
        "Replacing!",                           @DEBUG
        Dн                                      Get the first letter
        X                                       Push X
        s                                       Swap
        k                                       X (which is [0-9A-Za-z]) .indexOf(firstLetter)
        "indexOf run token = "?D,               @DEBUG
        4+                                      Add 4 to the index, now becoming the number of zeros to add
        s                                       Swap. Now top is the string between runs
        ¦                                       Remove the first character
        s                                       Swap again. Now the top is the number of zeros to add.
        ú                                       Add that many spaces (will be replaced with zeros later) to the start of the element.
        "Element Replaced = "?D,                @DEBUG
]
'W¾2×:                                          Need to do this replacement after the Y replacement so stuff like YW gets parsed properly. Again, ¾ is zero.
'X¾3×:
J                                               Join with ""
"Replaced input Stage2 = "?D,                   @DEBUG
'Z¶:                                            Replace "Z" with "¶".
.B                                              "Squarify" command. Splits on \n and appends spaces to the end of each element so each is the same length. In this case, that is equivalent to adding zeros to the end of each column, which is what we want.
ð¾:                                             Replace spaces (which we used for padding above) with zeros.
"Replaced input Stage3 = "?D,                   @DEBUG
"0 Stack top = "?D,                             @SDEBUG
"0 Stack top-1 = "?sD,s                         @SDEBUG
v                                               pop and loop over top element, which is array of column code
    NU                                          Store the column number in X
    yv                                          Loop over each character in the column              
        "1 Stack top = "?D,                     @SDEBUG
        "1 Stack top = "?D,                     @SDEBUG
        D                                       Duplicate top element (which is now [0-9A-Za-z])
        y                                       Push y (special loop variable)
        "Character = "?D,                       @DEBUG
        k                                       Push indexOf y in [0-9A-Za-z]
        "Index = "?D,                           @DEBUG
        "2.1 Stack top = "?D,                   @SDEBUG
        "2.1 Stack top-1 = "?sD,s               @SDEBUG
        D                                       Duplicate index.
        NX‚                                     Push [rowNumber,columnNumber]. This result is used for sorting.
        Š                                       Triple swap. The top of the stack is now sortPrefix, index, index
        32V                                     Set Y to 32
        Y‹i                                     index < 32. This uses up one copy of index
            Y+                                  Add 32 to the index
            b                                   Push the index in binary. This uses the second copy of index. 
            ¦                                   Remove the first character, which will be a 1 because we added 32. The result is now a padded binary string of length 5
            "Binary = "?D,                      @SDEBUG
            R                                   Reverse the binary string. This gives the proper transposed output. 
            X_i                                 This will only run if X (which is set to the column number) == 0. (Runs !X)
                ¶ì                              Stick a linebreak at the beginning of the binary string
            }
            "2.5 Stack top = "?D,               @SDEBUG
            "2.5 Stack top-1 = "?sD,s           @SDEBUG         
            ‚                                   At this point the top of the stack is sortPrefix,binaryString. This will combine the two into a list.
            "Pushing '"?D?"'",                  @DEBUG
            ˆ                                   Push [[rowNumber,columnNumber],binaryString] to global array
            "2.6 Stack top = "?D,               @SDEBUG
            "2.6 Stack top-1 = "?sD,s           @SDEBUG
        ]                                       Close all blocks
¯                                               Push global array

{                                               Sort global array. 
"Sorted Data = "?D,                             @DEBUG
€¦                                              Remove the sort prefix              
˜                                               Flatten. Before doing this, the array is in the format [[<binary string>],[<binary string>],[<binary string>],...]
J                                               Join with ""
'1'█:'0'.:                                      @DEBUG pretty print 
kerwizzy
sumber
1
Hello and welcome to PPCG; very nice first post! Regarding your byte count: you are right. TIO even handily displays that the 148 characters are represented by 148 bytes using the 05AB1E code page.
Jonathan Frech
One small note: Most golfing languages implement implicit printing; I think you can ommit your last ,.
Jonathan Frech
I looked it over and made some improvements. The behavior will only be different (theoretically) for things with more than 1000 rows or columns, at which point the smaller program will do something unexpected.
kerwizzy
Not the easiest challenge to start on! Nice job, I imagine some of the 05AB1E stalwart will give some handy pointers when they come by.
Jonathan Allan
1
Welcome to PPCG and the world of 05AB1E. I don't have that much time right now, and I've only started using 05AB1E a few months ago, so I'm not that much of a pro yet either, but here some small things to golf: "yy" can be „yy (05AB1E has both a 2-char and 3-char string builtin). >≠ can be Ā (truthified, basically 0 remains 0 and everything else becomes 1). }} can be ] (close all loops and if-elses). I'm sure some of the pro 05AB1E golfers are able to golf more substantial things than me, possibly even halving the byte-count.
Kevin Cruijssen
4

APL (Dyalog Unicode), 87 80 77 67 63 bytes

thanks to H.PWiz for saving 7 bytes and ngn for another 13 17.

a←⎕D,⎕A⋄,⍉↓⊖(5/2)⊤↑35(≠⊆⊢)a'Y.|W|X'R{'0'/⍨30 36|a2↑⍵.Match}⍞

Try it online!

NB: Takes the input as an upper case string.

With pretty printed output

Explanation

a←⎕D,⎕A a is the string '0123...89ABCD...XYZ'
'Y.|W|X'⎕R{'0'/⍨+/30 36|a⍳2↑⍵.Match} replaces X W and Yx with the corresponding number of '0's (explained more below) 35(≠⊆⊢)a⍳ converts the string into vector of indecies in a and splits on 35 (i.e.) 'Z' creating a nested vector
converts the nested vector into a matrix padding ends with 0s
(5/2)⊤ converts each number into a binary vector resulting in a 3-dimensional matrix with binary vectors along the primary axis
reverses along the primary axis
reduces the rank of the matrix so it is 2-dimensional
,⍉ reshapes the result to the appropriate output

'Y.|W|X'R{'0'/⍨30 36|a2↑⍵.Match}
'Y.|W|X'R                          Regular expression replace. mathing the string.
                        2↑⍵.Match   Takes matching string (either 'X' 'W' or 'Y.') and pads with spaces to length 2
                      a            Returns the indecies of each charactor in a. on the spaces this is 36 (length of a)
                30 36|              Takes the remainders of the indecies when divided by 30 and 36 respectively. Overall 'W' -> 'W ' -> 32 36 -> 2 0, 'Y2' -> 'Y2' -> 34 2 -> 4 2.
           '0'/⍨                    Make vector of repeating '0's with length equal to the sum of the result. '0'/⍨4 2  '000000'
jslip
sumber
Here is a 7 byte saving: a←⎕D,⎕A⋄,↓↑{⊖(5/2)⊤a⍳⍵}¨'Z'(≠⊆⊢)('Y.|W|X'⎕R{'0'/⍨{0::2+'WX'⍳⍵⋄4+a⍳1⊃⍵}⍵.Match})⍞. The error catch also doesn't seem very golfy
H.PWiz
Thanks @H.PWiz. Yeah I don't like the error catch either but otherwise I was using two regex replaces.
jslip
@jslip the parentheses around ⎕R and its operands are unnecessary
ngn
2+'WX'⍳⍵ -> 2+'X'=⍵
ngn
{0::2+'X'=⍵⋄4+a⍳1⊃⍵}⍵.Match -> 30 36|a⍳2↑⍵.Match
ngn
3

Perl 6, 156 142 bytes

14 bytes saved thanks to Jo King. (Also fixed a little bug with parsing the y and added a prettifier.)

Fixed the buggy parsing of y[wxy].

{s:g/y(.)/000{0 x:36(~$0)}/;s:g/x/w0/;s:g/w/00/;s/$/z/;map {
.flatmap:{:32($_).fmt("%05b").flip.comb}},[Z] .split("z").map: (*~0 x.chars).comb}

Try it online!

The line break is there just to make the text fit on the screen. It is not a part of the program.

How does it work

It is an anonymous function that takes a mutable string. (This makes using the function a little bit peculiar, because you can give it only variables, not literals.) After some work, it returns a list of lists containing 0's and 1's, with the same meaning as in the original post.

The input string comes in in the variable $_. We start by using a series of substitution statements on it in order to get rid of all of those shorthands for various numbers of zeroes. First, we need to sort out the y, because in the case of yx or yw, the w (or x) does not constitute a shorthand by itself. We search for y(.) (y and one character, which it remembers) and replace it by 000{"0"x:36(~$0)}: the three zeroes are copied verbatim, then we convert the next character from base 36 to base 10 (:36(~$0)) and add that many more zeroes. Then, we replace the w's using s:g/w/00/, and the x's using s:g/x/000/. Finally, with s/$/z/, we add a z onto the end, adding a whole lot of empty lines to the bottom. (We'll see the reason later.)

The rest is just a big map statement. We're mapping over .split("z").map: (*~0 x.chars).comb}), which is the input string (without zero shorthands), splitted into lines at z, with each line being first padded with 0 x.chars (tons of zeroes, namely as many as is the total length of the input string) on the right and then broken down into a list of individual characters (.comb). Finally, we transpose it with [Z] (reduce with zip). Zipping ends as soon as the shortest list is depleted, which results in all lines having the same length. (The number of useless trailing zeroes on the right is equal to the length of the shortest line. Also, this transposition trick fails for a "matrix" with only one row. That's why we forcibly added another row at the end before.)

Sekarang, kita hanya memetakan di atas baris (kolom dari matriks asli) dan mengganti setiap karakter yang ditemui dengan 5 bit yang sesuai. Itu dilakukan menggunakan :32($_)(basis 32 ke basis 10) .fmt("%05b")(format sebagai bitstring lebar 5, diisi dengan nol) .flip(membalikkan string, karena LSB berada di baris atas, bukan bagian bawah) .comb(pisahkan string ke daftar karakter). Kami telah menggunakan .flatmap, yang meratakan daftar hasil (jika tidak kami akan mendapatkan daftar daftar di setiap kolom). Transpos hasilnya kemudian secara implisit dikembalikan.

(Aku merasa agak buruk karena menyalahgunakan kemungkinan membuntuti nol begitu keras. Tapi itu mengurangi jumlah yang cukup besar :—).)

Ramillies
sumber
142 bytes
Jo King
@JoKing — Oh yes, thank you very much!
Ramillies
Fails for 1yx1 and 1yw1
Jonathan Allan
@JonathanAllan, you're right, thank you. I'll fix it in a moment. (I first thought for some reason that there could be only y[0-9a-v] and when I found that it's not the case, I just added a quick (and wrong) fix.)
Ramillies
Anda dapat mengganti 000substitusi dalam y denganx
Jo King
2

Jelly , 66 byte

Yowza!

ØBiⱮ+
Œuœṣ⁾YYj38Żṣ”YµḢç3;)Fṣ”Wj2ṣ”Xj3”0ẋ$¹OƑ?€Fṣ”Zµȯ”0ç31BḊ€UZ)Ẏz0

Tautan monadik yang menghasilkan versi yang dialihkan sebagai daftar daftar (tambahkan Zpada bagian akhir untuk ditransposisikan kembali).

Cobalah online! Atau lihat test-suite (dengan hasil cetak yang cantik).

Jonathan Allan
sumber
2

Retina , 203 byte

y(.)
#$1
{`#
0#
#0
000
)T`dl`_dl`#.
w
000
x
00
z
¶
%(`.+
$&¶$&¶$&¶$&¶$&
T`g-v`#`.+$
T`dl` `.+$
T`g-v`dl
T`89l`#`.+¶.+$
T`d` `.+¶.+$
T`89l`d
T`d`    #`.+¶.+¶.+$
T`4-7`d
T`d`  #`.+¶.+¶.+¶.+$
)T`d` # #
P`.+

Cobalah online! Tautan termasuk kasus uji. Solusi alternatif, juga 203 byte:

y(.)
#$1
{`#
0#
#0
000
)T`dl`_dl`#.
w
000
x
00
z
¶
%(`.+
$&¶$&
T`g-v`#`¶.+
T`dl` `¶.+
T`g-v`dl
^.+
$&¶$&
T`89l`#`¶.+
T`d` `¶.+
T`89l`d
^.+
$&¶$&
T`d`    #`¶.+
T`4-7`d
^.+
$&¶$&
T`d`  #`¶.+
)T`d` # #
P`.+

Cobalah online! Tautan termasuk kasus uji. Penjelasan:

y(.)
#$1

Pertama-tama tangani kasus canggung dari yperintah. Sayangnya surat setelah ini diizinkan menjadi yatau bahkan a z, jadi kita harus berhati-hati di sini. Semua sihir ypertama kali diubah menjadi #.

{`#
0#
#0
000
)T`dl`_dl`#.

Sebuah loop kemudian memproses #s. Pertama, a 0didahulukan dengan #. Jika ini adalah #0maka yang diubah untuk 000menyelesaikan operasi, jika tidak karakter setelah #dikurangi dan loop berulang sampai semua #s telah diproses.

w
000
x
00

Perbaiki ws dan xs.

z
¶

Berpisah di baris baru. ( S`zjuga berfungsi untuk jumlah byte yang sama.)

%(`.+
$&¶$&¶$&¶$&¶$&
T`g-v`#`.+$
T`dl` `.+$
T`g-v`dl
T`89l`#`.+¶.+$
T`d` `.+¶.+$
T`89l`d
T`d`    #`.+¶.+¶.+$
T`4-7`d
T`d`  #`.+¶.+¶.+¶.+$
)T`d` # #

Buat 5 salinan dari setiap baris, lalu lakukan konversi biner dengan memetakan huruf dengan bit yang sesuai untuk #dan menghapus bit itu, sementara huruf lain menjadi spasi. Bit diproses dalam urutan 16, 8, 4, 2, dan kemudian terjemahan terakhir menangani membersihkan 2bit dan mengonversi 1bit pada saat yang sama. (Versi alternatif membuat setiap salinan secara individual yang biaya byte lebih banyak tetapi ini disimpan karena penanganan bit disederhanakan.)

P`.+

Pad semua garis dengan panjang yang sama.

Neil
sumber
1

Python 2 , 249 244 byte

def f(s,Z='0'):
 while'y'in s:i=s.find('y');s=s[:i]+4*Z+Z*int(s[i+1],36)+s[i+2:]
 t=s.replace('w',2*Z).replace('x',3*Z).split('z');return map(''.join,zip(*[[(Z*5+bin(int(c,32))[2:])[:-6:-1]for c in r]+[Z*5]*(max(map(len,t))-len(r))for r in t]))

Cobalah online!

5 byte disimpan oleh Jonathan Frech .

Chas Brown
sumber
Ada ruang berlebihan di while 'y'in. Saya pikir subskrip [-5:][::-1]dapat di-golf-kan [:-6:-1]. Pernyataan pengembalian dapat diletakkan di baris sebelumnya.
Jonathan Frech
1

JavaScript (ES8), 192 byte

s=>[...s,"z"].map(c=>{h=parseInt(c,36);k="wxy".search(c)+1;y?k=h-y--:h-35?y=h>33:m=m.map(P,z+=5,i=k--);for(h=k?0:h;~k--;m[i]=h.toString(2)+P(m[i++]||""));},P=d=>d.padStart(z,0),m=[i=z=y=0])&&m

Mengembalikan transpos ... yang kemudian direfleksikan melintasi sumbu vertikal; beri tahu saya jika itu membatalkan entri ini. Output adalah larik string yang mengandung 0s dan 1s.

Cobalah online!

Penjelasan

Untuk setiap karakter c, k+1evaluasi terhadap jumlah baris yang akan dimodifikasi. k = "wxy".search(c) + 1;, di mana searchmetode mengembalikan indeks atau -1. kkemudian dikurangi hingga mencapai -1dengan memeriksa ~k--nilai kebenaran.

Jika karakter saat ini adalah "y", atur tanda sehingga nilai base-36 dari karakter berikutnya - 1 menjadi nilai untuk k.

Setelah menemukan "z", pad string ke kiri, menambah jumlah pad dengan 5, dan reset indeks array ke 0.

s =>
    [...s, "z"].map(c => {                  // append a "z" to ensure final output is properly padded
        h = parseInt(c, 36);
        k = "wxy".search(c) + 1;            // note that "y" -> 4 zeroes
        y                                   // if previous char is "y"...
            ? k = h - y--                   //      decrement y after subtracting y=1 since k is decremented to -1 and the previous y already pushed 4 zeroes
        : h - 35                            // else if current char is not "z"...
            ? y = h > 33                    //      set y if current char = "y"
        : m = m.map(P, z += 5, i = k--);    // else if current char is "z", pad
        for (
            h = k ? 0 : h;                  // if k is truthy, push zeroes
            ~k--;
            m[i] = h.toString(2)            // convert to boolean representation
                + P(m[i++] || "")           // prepend to row or to a newly padded row
        );
    },
        P = d => d.padStart(z, 0),
        m = [i = z = y = 0]                 // the logical OR 4 lines above replaces this value with ""
    ) &&
    m
redundansi
sumber
Saya pikir akan lebih masuk akal untuk tidak mengembalikan transpos input.
mbomb007
1

Haskell, 399 byte

Instal splitpaket:cabal install split

import Data.Char
import Data.List.Split
import Data.List
p=map
r=replicate
a c|47<c&&c<58=c-48|96<c&&c<123=c-87
o=a.ord
m l n|l==0=[]|True=(mod n 2):m(l-1)(div n 2)
t a=p(\l->l++r((maximum$p length a)-length l)'0')a
y s|length s==0=""|h=='y'=(r(4+(o$s!!1))'0')++y(drop 2 s)|h=='w'="00"++y t|h=='x'="000"++y t|True=h:y t where h=head s;t=tail s
w=(p concat).transpose.(p$p$(m 5).o).t.(splitOn "z").y
Евгений Новиков
sumber