23
\$\begingroup\$

This is supposed to be a code golf challenge on the simpler end of the spectrum. But I figured it could be nice to bring out some micro-optimisation here.

There are three basic types of DVI connectors: DVI-A (analog), DVI-D (digital) and DVI-I (integrated). Furthermore, there are single-link and dual-link versions of the DVI-D and DVI-I connectors. Each of those five different connectors uses a different set of pins.

Given one of A, D1, D2, I1, I2 as an identifier for the type of connector, print the corresponding ASCII representation of the connector's pins:

A:
#   #  # # #       # #
=====  # # #          
#   #  # #         # #

D1:
       # # #     # # #
=====  # # #     # # #
       # # #     # # #

D2:
       # # # # # # # #
=====  # # # # # # # #
       # # # # # # # #

I1:
#   #  # # #     # # #
=====  # # #     # # #
#   #  # # #     # # #

I2:
#   #  # # # # # # # #
=====  # # # # # # # #
#   #  # # # # # # # #

(The X: lines are not part of the output, you should only print 3 lines.)

You may write a program or function, taking input via STDIN, ARGV or function argument. The output must be printed to STDOUT, with an optional trailing line feed.

You may or may not include trailing spaces in the second line of the A connector. You must not use additional leading or trailing spaces anywhere else.

This is code golf, so the shortest answer (in bytes) wins.

\$\endgroup\$
4
  • \$\begingroup\$ Can I require input to be surrounded in quotes, e.g. "I1" instead of I1? \$\endgroup\$
    – Claudiu
    Nov 17, 2014 at 5:58
  • \$\begingroup\$ Are trailing spaces on other lines allowed? \$\endgroup\$ Nov 17, 2014 at 10:45
  • 1
    \$\begingroup\$ @Claudiu Nope, sorry. \$\endgroup\$ Nov 17, 2014 at 12:04
  • \$\begingroup\$ @gone_native Also no, sorry (will add that to the post). \$\endgroup\$ Nov 17, 2014 at 12:04

13 Answers 13

16
\$\begingroup\$

Perl - 100 91 (including 1 flag)

Uses nutki's ideas of using -p and reducing double-spaces. Also simplified elimination of trailing spaces.

#!perl -p
$x=$_|" qb";$_="dbd xxxqqqax#
=====bxxxqqqaaa!dbd xxaqqqax#";s/[$x]/  /g;s/\w/# /g;s/ !/
/

Input comes from stdin and must contain only the connector type with no trailing newline.


Previous:

$x=shift|" q";$_="d  d xxxqqqaxx
=====  xxxqqqaaa
d  d xxaqqqaxx";s/[$x]/  /g;s/\w/# /g;s/ $//mg;say

Takes a command-line argument. Ungolfed:

# Read argument, transform A->aq, I1->iq, I2->is, D1->dq, D2->ds
$x=shift|" q";

# String compacted
$_ = "d  d xxxqqqaxx
=====  xxxqqqaaa
d  d xxaqqqaxx";

# Clear any pins with matching char in input
s/[$x]/  /g;
# Convert rest to '#'
s/\w/# /g;
# Eliminate trailing spaces
s/ $//mg;
say
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Ingenious idea for handling the parameter. You can still save 2 bytes by adding $x=shift|" qb" and replacing 3 occurrences of double space with b in the template. Also changing input processing to '-p' gives 7 extra (minus the flag): $x=$_|"" and no need for final say. \$\endgroup\$
    – nutki
    Nov 17, 2014 at 12:53
  • \$\begingroup\$ @nutki - Thanks, I missed that! \$\endgroup\$ Nov 18, 2014 at 3:10
  • \$\begingroup\$ @MartinBüttner - All 3 lines have trailing spaces, though I realized I can fix the first and third lines easily by converting the final char in those lines from x to #. But the second line still has trailing spaces in all cases. For A, the space extends beyond the final # of the other lines (which I suppose is technically allowed). But for the I1,I2,etc. cases, there is still a trailing space that needs to be taken care of. I think I can golf a couple more characters out of it though. \$\endgroup\$ Nov 18, 2014 at 3:13
  • \$\begingroup\$ @gone_native oh, you're right. \$\endgroup\$ Nov 18, 2014 at 9:57
9
\$\begingroup\$

Python, 168 characters

t=raw_input()*2
Q="G   G  # # # 2 2 H # #\n=====  # # # 2 2 H H H\nG   G  # # H 2 2 H # #"
for A,B,N in('G0A','H0D','212'):Q=Q.replace(A,' #'[t[int(B)]in'I'+N])
print Q

Appears to be a new approach. I have the string:

G   G  # # # 2 2 H # #
=====  # # # 2 2 H H H
G   G  # # H 2 2 H # #

I replace the G, H, and 2 based on the input.

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

J, 153 121 119 chars

Minified
('='(;/1,.i.5)}"_1' #'{~5 3 22$#:128#.32x-~3 u:'dt*`J%Q5"xjuH%Jv2uJ!H5 t*`J%@5Jp*uH%Jv2p*!H dp"')echo@{~I1`I2`D1`D2 i.<

A third approach: pack all  's and #'s into a huge integer as bits. Add the ='s afterwards. Still doesn't make use of the symmetry of many of the connector variants, though.

Unminified
n =. 128 #. 32x -~ 3 u: 'dt*`J%Q5"xjuH%Jv2uJ!H5 t*`J%@5Jp*uH%Jv2p*!H dp"'
f =. ('=' (;/1,.i.5)}"_1 ' #' {~ 5 3 22 $ #: n) echo@{~ I1`I2`D1`D2 i. <

Minified (153)
[:echo@>({&(' #=',LF)&.:>;._2#;._1#:96#.32x-~3 u:' (0%dziqrDwbh5Ds6[gEl)_xkBS6?61m$1ne/v(]!&yW?_{K.S^X#Yn_d%O71KqXEw=I;meH>@eG2|2/gcR0'){~D1`D2`I1`I2 i.<

Also as a function. This one uses a variable-length binary coding, by tallying ones in binary and separating by zeroes. 0 ones in a row means  , 1 one means #, 2 ones means =, 3 ones means newline and 4 ones separate the five strings from each other.

Ungolfed
s =. ' (0%dziqrDwbh5Ds6[gEl)_xkBS6?61m$1ne/v(]!&yW?_{K.S^X#Yn_d%O71KqXEw=I;meH>@eG2|2/gcR0'
f =. [: echo@> ({&(' #=',LF)&.:>;._2 #;._1 #: 96 #. 32x -~ 3 u:s) {~ D1`D2`I1`I2 i. <
\$\endgroup\$
4
\$\begingroup\$

Marbelous, 281 bytes/characters

Minified:

00
]]
GG]]
IIJJJJ
:G
}0
++
>>
>X{0
/\{<{>
:H
}0
-Z
>E-2
-C//
{0
:I
23232003002023}023
LLMMMMNNRROOMMRRLL
0003
0300
NNNN
:J
}0}1
HH-2
KKKK
:K
}1}0
}1}0}0
PPPPQQ
:L
}020
}0202020
:M
20}020}020}0
:N
}0
}020
+W20
:O
3D3D3D3D3D}0
:P
}023}1230A
LLMMNNMM
:Q
2023}0230A
OOMMNNMM
:R
}0
\/0A

This takes D1, D2, I1, I2 or A from STDIN. Trailing newlines in input is optional. Output is to STDOUT.

This program calls subboards which print parts of connectors, either filling in #s or leaving spaces depending on inputs.

Try this answer here; cylindrical boards are needed.

with Comments:

00 .. ..
]] .. .. # get A/D/I
Gt ]] .. # pass into Gt; call PA if A or PrDI if D/I
PA Pr DI

# If 'A' is passed, a marble is emitted down
# Otherwise, marbles are sent left/right
# The value of the marble outputted is (I+1)/2, which creates a difference of 3
#   between D and I, the difference between a space and a #
:Gt
}0 .. ..
++ .. ..
>> .. ..
>X {0 ..
/\ {< {>

# Returns 0 if '1' is passed, and 3 if '2' is passed
:Ff
}0 ..
-Z ..
>E -2
-C //
{0 ..

# Prints connector A 
# Calls made: P1(23) P2(23) P2(20) P3(03) LF(00) P4(20) P2(23) LF(}0) P1(23)
#   P3(03) P3(00) P3(00) P3(03)
:PA
23 23 20 03 00 20 23 }0 23
P1 P2 P2 P3 LF P4 P2 LF P1
00 03 .. .. .. .. .. .. ..
03 00 .. .. .. .. .. .. ..
P3 P3 .. .. .. .. .. .. ..

# Prints connectors D0/D1/I0/I1
# }0 is either '1' or '2'
# }1 is either 32 or 35 (for D or I)
:PrDI
}0 }1
Ff -2
Li DI

# Helper for PrDI
# Calls made: L1(}1, }0) L2(}0) L1(}1, }0)
:LiDI
}1 }0 ..
}1 }0 }0
L1 L1 L2

# Prints '#   # ' (}0 = 0x23) or '      ' (}0 = 0x20)
:P1
}0 20 .. ..
}0 20 20 20

# Prints ' # # #' (}0 = 0x23) or '      ' (}0 = 0x20)
:P2
20 }0 20 }0 20 }0

# Prints ' # #' (}0 = 0x03) or '    ' (}0 = 0x00)
:P3
}0 ..
}0 20
+W 20

# Prints '===== ', }0 must be 0x20
:P4
3D 3D 3D 3D 3D }0

# Prints the top/bottom line of D/I connectors + newline
# Calls made: P1(}0) P2(23) P3(}1) P2(23) 
:L1
}0 23 }1 23 0A
P1 P2 P3 P2 ..

# Prints the middle line of D/I connectors + newline
# Calls made: P4(20) P2(23) P3(}0) P2(23)
:L2
20 23 }0 23 0A
P4 P2 P3 P2 ..

# Emits a newline (0x0A) regardless of input
:LF
}0 ..
\/ 0A
\$\endgroup\$
0
4
\$\begingroup\$

Perl 5: 105 (including 1 flag)

Yet another Perl solution. Uses stdin for the parameter.

#!perl -p
@x=map$_?'#':$",!/D/,-/2/,!/A/,1,0;$_='040 33311233
=====433311222
040 33211233';s/\d/$x[$&] /g;s/ $//mg
\$\endgroup\$
4
\$\begingroup\$

GNU sed, 116 bytes

s/.*/&:#   #  33322433\n=====  33322444\n#   #  33422433/
/A/s/[42]/  /g
/1/s/2/  /g
/D/s/#/ /g
s/[2-4]/ #/g
s/.*://

Output:

$ echo "A
D1
D2
I1
I2"|sed -f dvi.sed
#   #   # # #       # #
=====   # # #          
#   #   # #         # #
        # # #     # # #
=====   # # #     # # #
        # # #     # # #
        # # # # # # # #
=====   # # # # # # # #
        # # # # # # # #
#   #   # # #     # # #
=====   # # #     # # #
#   #   # # #     # # #
#   #   # # # # # # # #
=====   # # # # # # # #
#   #   # # # # # # # #
$ 
\$\endgroup\$
3
\$\begingroup\$

CJam, 79 70 bytes

Inspired from nutki's answer. This also makes sure that there are no trailing white spaces (except for second line)

"VF­wGC}*D"176b6b" DG1A="f=r'1+2<"  "aer{_s" ="&\"# "?}%s23/Wf<N*

Use this link to copy the code as SE strips non-printable characters.

There are 5 characters in non-printable ASCII range but well within a byte (ASCII code 255)

How it works:

"VF­wGC}*D"176b6b" DG1A="f=              "This effectively results into this string:"
                                        "D  D GGG11AGG=====  GGG11AAAD  D GGA11AGG";
r'1+2<                                  "Read the input, convert A to A1";
      "  "er                            "Replace the occurrences of above two characters"
                                        "with double space '  '";
            {_s" ="&\"# "?}%s           "Convert every non = and space character to '# '";
                             23/        "Divide the string into parts of 23 characters";
                                Wf<     "String last character from above parts";
                                   N*   "Join with new line";

Try it online here

\$\endgroup\$
0
3
\$\begingroup\$

J, 198 194 157 chars

Minified
3 22 echo@$'x   x  # # # x x x # #=====  # # # x x x x xx   x  # # x x x x # #'('x'I.@:=])}~(5 16$(#' #'$~#)"."0'4123212128262126290901824'){~D1`D2`I1`I2 i.<

Implemented as a function. Note that the function is a train, meaning one would have to surround it by parentheses or assign it to a name in order to use it (maybe I should count the parens as part of the function, even though technically they aren't).

Ungolfed
S1 =. (#' #'$~#)"."0'4123212128262126290901824'
S2 =. 'x   x  # # # x x x # #=====  # # # x x x x xx   x  # # x x x x # #'
f  =. 3 22 echo@$ S2 ('x'I.@:=])}~ (5 16$S1) {~ D1`D2`I1`I2 i. <

The idea is to store the common part of the string separately from the characters that differ between the connector types. S1 stores the unique characters, and S2 acts as a pattern with x's acting as placeholders to fill in.

\$\endgroup\$
3
\$\begingroup\$

Python - 167 166 164 161 159

C=raw_input()
a=["#   # "," "*6]["D"in C]
r=" #"
b="A"in C
i=3-b
d=(r*[i,5][C[1:]>"1"]).rjust(10)
t=r*3
print a+t+d+"\n===== "+t+d*~-len(C)+"\n"+a+r*i+"  "*b+d
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6) 178 186

Edit Having 7 base blocks, use base 7
The straight way, using string building with replace and 7 building blocks.
Output to stdout using alert as requested by OP.
Now I'll try some micro-optimizations...

F=t=>alert(
  {A:21349062249,D1:538695058296,D2:534740169498,I1:151139015296,I2:147184126498}[t]
  .toString(7).replace(/./g,c=>'\n0#   # 0===== 0 # # #0 # #0      0    '.split(0)[c])
)

Test In FireFox/FireBug console - removing 'alert' to simplify test

;['A','D1','D2','I1','I2'].forEach(i=>console.log(F(i)))

Output

#   #  # # #       # #
=====  # # #
#   #  # #         # #

       # # #     # # #
=====  # # #     # # #
       # # #     # # #

       # # # # # # # #
=====  # # # # # # # #
       # # # # # # # #

#   #  # # #     # # #
=====  # # #     # # #
#   #  # # #     # # #

#   #  # # # # # # # #
=====  # # # # # # # #
#   #  # # # # # # # #
\$\endgroup\$
2
\$\begingroup\$

APL (115)

V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V

Test:

      V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V
A
#   #  # # #       # #
=====  # # #          
#   #  # #         # #
      V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V
D1
       # # #     # # #
=====  # # #     # # #
       # # #     # # #
      V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V
D2
       # # # # # # # #
=====  # # # # # # # #
       # # # # # # # #
      V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V
I1
#   #  # # #     # # #
=====  # # #     # # #
#   #  # # #     # # #
      V←3 22⍴''⋄V[2;⍳5]←'='⋄V[⍳3;(2=⍴I)/(6+2×⍳8)~14 16/⍨'1'∊I]←V[⍳2;12]←V[⍳3;8 10]←V[R/1 3;1 5 20 22/⍨R←∨/'AI'∊I←⍞]←'#'⋄V
I2
#   #  # # # # # # # #
=====  # # # # # # # #
#   #  # # # # # # # #
\$\endgroup\$
1
  • \$\begingroup\$ @MartinBüttner: whoops, fixed \$\endgroup\$
    – marinus
    Nov 19, 2014 at 0:24
2
\$\begingroup\$

JavScript ES6, 186 bytes

f=c=>(b=(c[0]=='D'?'      ':'#   # '))+(e=(g=r=>parseInt(r,36).toString(2).replace(/./g,n=>' '+[' ','#'][n]))((o=c[1])?(o-1?73:'6f'):'6b'))+'\n===== '+(o?e:' # # #')+'\n'+b+(o?e:g('5f'))

f=c=>(b=(c[0]=='D'?'      ':'#   # '))+(e=(g=r=>parseInt(r,36).toString(2).replace(/./g,n=>' '+[' ','#'][n]))((o=c[1])?(o-1?73:'6f'):'6b'))+'\n===== '+(o?e:' # # #')+'\n'+b+(o?e:g('5f'))
alert(f(prompt()))

The code is quick and dirty, but it gets the job done. Mostly, the #'s and spaces are put into binary and then base 36. I'm looking into a more elegant and hopefully shorter solution.

\$\endgroup\$
5
  • \$\begingroup\$ When I click Run code snippet I don't see anything. \$\endgroup\$ Nov 19, 2014 at 10:57
  • \$\begingroup\$ That's probably because it uses ECMAScript 6 arrow functions, which are only supported in recent Firefox versions. \$\endgroup\$ Nov 19, 2014 at 12:57
  • \$\begingroup\$ I tested with the last version of Firefox and nothing happens. Is there something to do to test this function? \$\endgroup\$ Nov 19, 2014 at 14:40
  • 1
    \$\begingroup\$ Oh, that's becuase it's only a function. I'll add a way to test it. \$\endgroup\$ Nov 19, 2014 at 14:43
  • \$\begingroup\$ you may edit the code to make it runnable as a snippet (use a second code block to keep the original code) or remove the snippet renderer. \$\endgroup\$ Nov 19, 2014 at 14:49
1
\$\begingroup\$

Perl 5 - 150 (149 + 1 for n)

Golfed:

@b=split//;$_='0   0 ###112##
===== ###11222
0   0 ##2112##';
@c=(@b[0]eq D,@b[1]ne 2,@b[0]eq A);@d=('#',$");s/([#12])/ \1/g;s/(\d)/$d[$c[$1]]/ge;say

Input from STDIN, output to STDOUT. Works by doing filling in certain characters with # or depending on the input.

Ungolfed:

@b=split//; # char array from input
$_='0   0 ###112##
===== ###11222
0   0 ##2112##';

@c=(@b[0] eq 'D',@b[1] ne '2',@b[0] eq 'A');
@d=('#',' ');

s/([#12])/ \1/g; # add spaces
s/(\d)/$d[$c[$1]]/ge; # replace numbers with appropriate character
say $_;
\$\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.