14
\$\begingroup\$

The aim is simple: given the string of resistance values, draw the part of "electrical circuit" of those resistors. Examples follow.

  1. Input: 3. Output:

    --3--
    
  2. Input: 1,2,3. Output:

    --1----2----3--
    

Next, the resistors can be joined parallel (by 2 or 3 resistors per join), but the required depth of parallel joins is only 1.

  1. Input: 1|2. Output:

     |--1--|
    -|     |-
     |--2--|
    
  2. Input: 1|2|3. Output:

     |--1--|
    -|--2--|-
     |--3--|
    

    Note, that --2-- resistor is now centered.

  3. Input: 1|2,3|4|5. Output:

     |--1--|  |--3--|
    -|     |--|--4--|-
     |--2--|  |--5--|
    

Combined joins:

  1. Input: 1,2,3|4,5,6|7|8,9. Output:

               |--3--|       |--6--|
    --1----2---|     |---5---|--7--|---9--
               |--4--|       |--8--|
    

If it would be more convenient, the input can be a two-dimensional array. I.e. the input for the last example would look like this: [1,2,[3,4],5,[6,7,8],9].

Some notes:

  • No other forms of (in|out)puts are allowed.
  • The resistance of each resistor can vary in the range from 1 to 9. Other values, like -, 42 or 0 are disallowed.
  • The joins like 1|2|3|4 are invalid. As already mentioned, max 3 per parallel join. Empty joins, i.e. ...,|,... or ...,,... are invalid.
  • Oh, and this is code golf :)
\$\endgroup\$
6
  • \$\begingroup\$ Thanks for the correction, it's a fixed numbers of dashes. The separate resistors are preceded and followed by two dashes. \$\endgroup\$
    – nicael
    Commented Dec 25, 2015 at 15:13
  • 1
    \$\begingroup\$ Can the first and third line be padded with spaces to even the length of the lines? \$\endgroup\$
    – TFeld
    Commented Dec 25, 2015 at 18:45
  • \$\begingroup\$ @Tfeld Trailing/leading spaces are allowed before/after the whole output, but not between the resistors. \$\endgroup\$
    – nicael
    Commented Dec 25, 2015 at 18:54
  • \$\begingroup\$ Are leading/trailing empty lines allowed when there are no parallel resistors? \$\endgroup\$ Commented Dec 26, 2015 at 3:40
  • \$\begingroup\$ @Pietu Hm, let it be yes :) \$\endgroup\$
    – nicael
    Commented Dec 26, 2015 at 5:31

6 Answers 6

5
\$\begingroup\$

Vyxal, 64 bytes

ƛI⁼[h\-dpømð5*:‟W|ƛ`|--`pøm;:L2=[÷`|   `øm$W]];DvhṄ,ƛ1i;\-j,vtṄ,

Try it Online!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 45 42 bytes

ε€…--ÿDgið.ø.BëÁð3ת3£À'|ì… - SδìÅ\]€.ºøJ»

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python 2, 195 bytes

a=b=c=''
B=' '*5
H='--'
S=' |'+H;E=H+'| '
for x in input():
 if x>9:a+=S+`x[0]`+E;c+=S+`x[-1]`+E;b+='-|'+[B,H+`x[1]`+H][len(x)>2]+'|-'
 else:b+=H+`x`+H;a+=B;c+=B
print[b,a+'\n'+b+'\n'+c]['|'in a]

Takes input as:

[1,2,[3,4],5,[6,7,8],9]

Output:

           |--3--|       |--6--|      
--1----2---|     |---5---|--7--|---9--
           |--4--|       |--8--|      
\$\endgroup\$
1
  • \$\begingroup\$ Save a few bytes by making H a function lambda V:f'--{V}--'? \$\endgroup\$
    – user100690
    Commented Jun 9, 2021 at 13:56
3
\$\begingroup\$

JavaScript (Node.js), 149 bytes

n=>(H=M=>n.map(e=>(T=e[M]||e[1])?` |${(G=e=>`--${e}--`)(T)}| `:'     ').join``)(0)+`
${n.map(e=>+e?G(e):`-|${e[2]?G(e[1]):'     '}|-`).join``}
`+H(2)

Try it online!

No need to overcomplicate things, just keep it simple

\$\endgroup\$
2
\$\begingroup\$

Haskell, 132 bytes

foldr(zipWith(:))e.(>>=g)
a=" - "
b="|||"
s%c=[a,b,c,c,s,c,c,b,a]
g[x]=[a,a,' ':x:" ",a,a]
g[x,y]=[x,' ',y]%"- -"
g z=z%"---"
e=[]:e

Try it online!

Takes input as a [[Char]] and outputs a list of lines.

\$\endgroup\$
1
\$\begingroup\$

Julia 1.5, 129 bytes

s=" "^5
+x="--$x--"
!x=map(*,[" |","-|"," |"],x,["| ","|-","| "])
-x=[[s,.+x,s],![+x[1],s,+x[(l=end;)]],!.+x][l]
~x=map(*,.-x...)

Try it online!

output is a list of lines

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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