5
\$\begingroup\$

This challenge is pretty simple: Your task is to draw a schematic of a (simplified) memory array. You will be given two (positive) integers n,m and the task is to draw the n:2^n decoder connected to the memory array with d data lines.

The addressing will be in the order of gray codes, to check if the sequence is correct, see A014550. For simplicity there are no input ports, write ports etc. On the top of the decoder you need to specify its type (e.g. 2:4) and you will label the data lines with d:[d-1],...,d:1,d:0. If the cray code wouldn't fit the decoder width, you'll need to expand the width, see the last two examples.

Rules

  • Leading whitespaces are not allowed
  • Trailing whitespaces are allowed
  • At the end of the output there may be one or several newlines
  • You can assume the following constraints on the input 0 < n and 0 < d <= 1e4
  • The output is either printed to the console or you write a function that returns a newline-separated string
  • For invalid input the behaviour of your program/function is left undefined

Test cases

n = 1, d = 1

.-----.
| 1:2 |
|     |
|    0|---+
|     |   |   
|     |  .+.  
|     |  | +-+
|     |  `-' |
|    1|---+  |
|     |   |  |
|     |  .+. |
|     |  | +-+
|     |  `-' |
`-----'      |
            d:0

n = 1, d = 10

.-----.
| 1:2 |
|     |
|    0|---+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|     |   |     |     |     |     |     |     |     |     |     |   
|     |  .+.   .+.   .+.   .+.   .+.   .+.   .+.   .+.   .+.   .+.  
|     |  | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' |
|    1|---+--(--+--(--+--(--+--(--+--(--+--(--+--(--+--(--+--(--+  |
|     |   |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
|     |  .+. | .+. | .+. | .+. | .+. | .+. | .+. | .+. | .+. | .+. |
|     |  | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' | `-' |
`-----'      |     |     |     |     |     |     |     |     |     |
            d:9   d:8   d:7   d:6   d:5   d:4   d:3   d:2   d:1   d:0

n = 2, d = 3

.-----.
| 2:4 |
|     |
|   00|---+-----+-----+
|     |   |     |     |   
|     |  .+.   .+.   .+.  
|     |  | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' |
|   01|---+--(--+--(--+  |
|     |   |  |  |  |  |  |
|     |  .+. | .+. | .+. |
|     |  | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' |
|   11|---+--(--+--(--+  |
|     |   |  |  |  |  |  |
|     |  .+. | .+. | .+. |
|     |  | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' |
|   10|---+--(--+--(--+  |
|     |   |  |  |  |  |  |
|     |  .+. | .+. | .+. |
|     |  | +-+ | +-+ | +-+
|     |  `-' | `-' | `-' |
`-----'      |     |     |
            d:2   d:1   d:0   

n = 4, d = 5

.------.
| 4:16 |
|      |
|  0000|---+-----+-----+-----+-----+
|      |   |     |     |     |     |   
|      |  .+.   .+.   .+.   .+.   .+.  
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0001|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0011|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0010|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0110|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0111|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0101|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  0100|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1100|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1101|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1111|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1110|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1010|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1011|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1001|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
|  1000|---+--(--+--(--+--(--+--(--+  |
|      |   |  |  |  |  |  |  |  |  |  |
|      |  .+. | .+. | .+. | .+. | .+. |
|      |  | +-+ | +-+ | +-+ | +-+ | +-+
|      |  `-' | `-' | `-' | `-' | `-' |
`------'      |     |     |     |     |
             d:4   d:3   d:2   d:1   d:0

n = 1, d = 1024    (output truncated!)

.-----.
| 1:2 |
|     |
|    1|---+-----+----...
|     |   |     |    ...
|     |  .+.   .+.   ...
|     |  | +-+ | +-+ ...
|     |  `-' | `-' | ...
|    0|---+--(--+--(-...
|     |   |  |  |  | ...
|     |  .+. | .+. | ...
|     |  | +-+ | +-+ ...
|     |  `-' | `-' | ...
`-----'      |     | ...
            d:1023d:1022d:1021...

n = 10, d = 1    (output truncated!)

.-----------.
| 10:1024   |
|           |
| 0000000000|---+
|           |   |   
|           |  .+.  
|           |  | +-+
|           |  `-' |
...        ...    ...

n = 12, d = 5    (output truncated!)

.-------------.
| 12:4096     |
|             |
| 000000000000|---+-----+-----+-----+-----+
|             |   |     |     |     |     |   
|             |  .+.   .+.   .+.   .+.   .+.  
|             |  | +-+ | +-+ | +-+ | +-+ | +-+
|             |  `-' | `-' | `-' | `-' | `-' |
...          ...    ...   ...   ...   ...   ...
\$\endgroup\$
5
  • \$\begingroup\$ what specifies the decoders width? From the examples it looks like its the width of the n:2^n + 2, but do we need to extend it when the gray node representation wouldn't fit in it (e.g. n=10, len(10:1024)+2 = 9, gray node repr length = 10) \$\endgroup\$ – dzaima Jul 22 '17 at 12:54
  • \$\begingroup\$ Yes you'd need to extend it in that case, I'll add an example. \$\endgroup\$ – ბიმო Jul 22 '17 at 13:07
  • \$\begingroup\$ in that case, how should the decoder type be centered/anchored? \$\endgroup\$ – dzaima Jul 22 '17 at 13:08
  • \$\begingroup\$ Anchored to the left, but with a space. See examples I added. \$\endgroup\$ – ბიმო Jul 22 '17 at 13:14
  • \$\begingroup\$ Summing up, the width would be max(len(n:2^n)+4,len(graycode)+3). The decoder name is always separated with one space from the left and the codes are right aligned without spaces. \$\endgroup\$ – ბიმო Jul 22 '17 at 13:18
6
\$\begingroup\$

SOGL V0.12, 185 bytes

b :2b^:D⁰∑l2+bIΧCc┌*:A .1ΟO@c*┐1Οd5*2+∙ `a '+++;31žƧ01bH{╬⁰1w⁄»Ƨ01*□+}I{∑4⌡@}¹cb-2+3žKOKOād0C{āe{"h}‼Τ─īsgQ⅛&šε╬»‘6n┌eF-¡3**41ž┐c3*∙ceF-*¡³ (*ž61ž┼}+1C}@5*┐+e*+d┌@4*Ο;┼øe∫eκƧd:ΚF6*1ž}+┼

Try it Here!

Way too long...

\$\endgroup\$
2
  • \$\begingroup\$ Well it's a fairly complicated pattern compared with other ASCII-art challenges, I think under 200 bytes is quite good. Unfortunately I couldn't test it for n > 8 but it looks good! \$\endgroup\$ – ბიმო Jul 22 '17 at 18:24
  • \$\begingroup\$ @BruceForte The sad thing is that 85 of the bytes is just the decoder box.. \$\endgroup\$ – dzaima Jul 22 '17 at 18:25
6
\$\begingroup\$

Charcoal, 170 157 bytes

NνNδAX²νηA⪫⟦θ:Iη ⟧ωθA⌈⟦νLθ⟧ζ←“ E*lÿxγ∨P3ζgRU#@˜R2$⁴⟧”F⁻δ¹C±⁶¦⁰↓←|  F⁻η¹C⁰¦⁵P←×⁺+×-⁵δ⸿P|  ←M×⁵η↓Fδ«P⁺d:ιM⁶←»↑←'-←ζ↑`↑²↑×⁵η.-P⁺¶θζ↓.↓²P↓×⁵η←Fη«Fν←§0110÷ιX²κMν⁵

Try it online! Verbose approximation. Explanation:

NνNδAX²νηA⪫⟦θ:Iη ⟧ωθA⌈⟦νLθ⟧ζ

Sets up the variables used in the rest of the code. ν and δ are the inputs, η is 2ν, θ is the label at the top of the box, while ζ is one less than the width of the box.

←“ E*lÿxγ∨P3ζgRU#@˜R2$⁴⟧”

This compressed string represents the bottom left decoder. This is the one that's most representative, although ironically it first gets printed in the top right and then copied left and down.

F⁻δ¹C±⁶¦⁰↓←|  

The decoder is repeated horizontally, and the --( at the top right of the last decoder is changed to a |.

F⁻η¹C⁰¦⁵P←×⁺+×-⁵δ⸿P|  ←M×⁵η↓

The decoder is then repeated vertically, and the (s in the top row are changed to -s (if you look carefully you'll see that too many -s are printed but fortunately the last one gets overwritten by the 0 of the address), after which the |s for the top right decoder are removed.

Fδ«P⁺d:ιM⁶←»

The decoders are numbered right-to-left.

↑←'-←ζ↑`↑²↑×⁵η.-P⁺¶θζ↓.↓²P↓×⁵η←

The box is printed, including the label.

Fη«Fν←§0110÷ιX²κMν⁵

The Gray codes are printed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.