12
\$\begingroup\$

Write a program or function that "reacts" to a given integer n (input via function parameter/args/stdin)

The program doesn't care about negative numbers, likes even, dislikes odd numbers and fears the number 13.

It should output the following:

if n<0:

 --------------------------
 |                        |
 |                        |
(|      _          _      |)
 |                        |
 |                        |
 |                        |
 |                        |
 |       oooooooooo       |
 |                        |
 |                        |
 |                        |
 --------------------------

if n%2 == 0 and n>-1:

 --------------------------
 |                        |
 |                        |
(|      ^          ^      |)
 |                        |
 |                        |
 |                        |
 |     o            o     |
 |       oooooooooo       |
 |                        |
 |                        |
 |                        |
 --------------------------

if n%2 == 1 and n>-1 and n!=13:

 --------------------------
 |                        |
 |                        |
(|      >          <      |)
 |                        |
 |                        |
 |                        |
 |                        |
 |       oooooooooo       |
 |     o            o     |
 |                        |
 |                        |
 --------------------------

if n==13:

 --------------------------
 |                        |
 |                        |
(|     (O)        (O)     |)
 |                        |
 |                        |
 |       oooooooooo       |
 |       o        o       |
 |       oooooooooo       |
 |                        |
 |                        |
 |                        |
 --------------------------

The shortest solution in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ 13 is odd. Do we only output one face? Negatives are odd and even... \$\endgroup\$ – user31074 Sep 10 '14 at 11:33
  • \$\begingroup\$ @MartinBüttner, in that case the first rule about negative numbers is completely unnecessary. The only priority which actually requires all of the rules is for the first and last ones to have priority over the middle ones. \$\endgroup\$ – Peter Taylor Sep 10 '14 at 11:46
  • \$\begingroup\$ @PeterTaylor oops, good point. \$\endgroup\$ – Martin Ender Sep 10 '14 at 11:47
  • \$\begingroup\$ @StandardToaster I edited the output conditions to avoid any misunderstandings \$\endgroup\$ – Markuz Sep 10 '14 at 11:53
4
\$\begingroup\$

CJam - 169

S24*aB*2li_D=3{_0<2{_2%}?}?\;:X"^^<>__":s"(O)"a2*+2/=~6X3=-{S*_@+_,C\-S*2*+@@++}:F~t7'o5*_7F:MtX3={5Mt6'o_7Ft}*X2<{X2*6+'o_5Ft}*{" |"\+'|+}%S'-26*+aa2*\*_3=1>"()"\*3\tN*

Try it at http://cjam.aditsu.net/

High-level explanation:

S24*aB* makes a matrix full of spaces, for the face without the edges
li_D=3{_0<2{_2%}?}?\;:X reads the number and converts it to 0 (even), 1 (odd), 2 (negative) or 3 (13), storing it in X
X"^^<>__":s"(O)"a2*+2/=~ selects the eye types (2 strings)
{S*_@+_,C\-S*2*+@@++}:F - function F takes 2 strings (say S1, S2) and a number (say N) and makes a 24-char string containing N spaces, S2, other spaces, S1, N spaces
2 (eyes) 6X3=- (F)~t puts the eyes line in the matrix
7'o5*_7F:Mt puts the common mouth line in the matrix, and also saves it in M
X3={5Mt6'o_7Ft}* if X is 3, it puts M again in the matrix, 2 lines higher, and puts the mouth sides in between
X2<{X2*6+'o_5Ft}* if X is 0 or 1, it puts the mouth corners in the appropriate position
{" |"\+'|+}% adds vertical edges and a space on the left on every line
S'-26*+aa2*\* adds the horizontal edges
_3=1>"()"\*3\t adds the ears
N* adds newline separators

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

Ruby, 241 224

f=->n{s=" #{?-*26}
"
s+=" |#{' '*24}|
"*11+s
s[84]=?(
s[110]+=?)
s[233,12]=m=?O*12
s[91,3]=s[102,3]=n<0?' _ ':n==13?(s[177,12]=m;s[205]=s[216]=?O;'(O)'):(s[203+d=n%2*56]=s[218+d]=?O;" #{n%2<1??^:b=?>} ")
s[103]=?<if b
$><<s}

This defines a function to be called like f[13]. I'm sure there's a lot of room for improvement, similar what Ventero helped me do here.

Basically, I'm building the rough framework first, and attach ears as well as the mouth. Then I place the eyes depending on the number. While working out the characters to put into the eyes, I also take care of the different mouths so I don't have to check for the type of number again. Finally I need to fix the right eye for odd numbers, because the code above puts the same string into both eyes.

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

C# - 349 bytes

Certainly not going to win any awards, but a different way of doing things.

Golfed:

class P{static void Main(string[]A){int j=64,i,r=0,n=int.Parse(A[0]);for(var f=(n<0?"IDID-TDTD-":n==13?"HDHD(IDIDOJDJD)SDSD(TDTDOUDUD)KIRI JHSJo":n%2<1?"IDID^TDTD^HIHIoUIUIo":"IDID>TDTD<HKHKoUKUKo")+@"JJSJoCBZM BB[M|BA[N-ADAD(\D\D)AA\N AA]N"+"\n";j++<78;)for(i=64;i++<93;System.Console.Write(f[r]))for(r=0;f[r++]>i|f[r++]>j|i>f[r++]|j>f[r++];r++);}}

Less golfed:

class P
{
    static void Main(string[]A)
    {
        int j=64,i,r=0,n=int.Parse(A[0]); // everything is offset by 65 in this program

        for(
            var f=(n<0?"IDID-TDTD-": // this string describes a set of rectangles (x1,y1,x2,y2,ch)
                n==13?"HDHD(IDIDOJDJD)SDSD(TDTDOUDUD)KIRI JHSJo":
                n%2<1?"IDID^TDTD^HIHIoUIUIo":
                "IDID>TDTD<HKHKoUKUKo"
                )+@"JJSJoCBZM BB[M|BA[N-ADAD(\D\D)AA\N AA]N"+"\n";

            j++<78;) // the loop then prints the rectangles to the screen
            for(i=64;i++<93;System.Console.Write(f[r]))
                for(r=0;f[r++]>i|f[r++]>j|i>f[r++]|j>f[r++];r++);
    }
}
\$\endgroup\$
3
\$\begingroup\$

Python 2 - 255

Not really short, but I'll post it anyway:

n=input()
o='o'
S='  '
w=y=7*S
b=' '+'-'*26+'\n'
p=' |%19s     |\n'
E=p%y*2
v=S+o*10+S
u=z=o+6*S+o
A=a='^ '
if n%2:A='< ';a='> ';u=y;w=z
if n<0:A=a='_ ';u=w=y
if n==13:A=a='(O)';u=w=v;v=S+o+S*4+o+S
print b+E+'(|%8s%11s     |)\n'%(a,A)+E+p%u+p%v+p%w+E+b

I miss item assignment for strings in Python! :( Then one could start with the character matrix and simply modify eyes and mouth.

\$\endgroup\$
  • \$\begingroup\$ Originally I actually learned Ruby for a similar PPCG challenge where I need immutable strings for precisely that reason. :D \$\endgroup\$ – Martin Ender Sep 10 '14 at 15:42
  • \$\begingroup\$ You can do b=bytearray() and then b+'mystring' will generate a mutable bytearray, which prints the same way as any other string. \$\endgroup\$ – Veedrac Sep 10 '14 at 22:17
3
\$\begingroup\$

Python 2, 257

Not a winner, but an alternate approach, got quite close! I have hope I can squeeze a few more chars out of it. Builds all the lines piece by piece, using common substrings to really squeeze out the bytes.

X,Z=R=" |"
Q=X*5
Y=X*7
A=X+"-"*26
B=R+X*24+Z
n=input()
a,b,c,d,e,f,g=((("^>^<o  o "+Q)[n%2::2],"OO  ()o")[n==13],"__"+Q)[n<0]
for F in(A,B,B,"(|"+Q+e+a+f+Y+X+e+b+f+Q+"|)",B,B,R+Y+g*10+Y+Z,R+Q+c+X+g+Y+X+g+X+c+Q+Z,R+Y+"o"*10+Y+Z,R+Q+d+Y+Q+d+Q+Z,B,B,A):print F
\$\endgroup\$
2
\$\begingroup\$

CJam, 202

[S'-26*N]:Z[S'|:PS24*PN]:RR'(PS5*qi:XD=X0<-X2%2*+:Y[" ^ "_" > ""(O)"" - "__]=S8*Y[" ^ ""   "" < ""(O)"" - "__]=S5*P')NRRXD=[SPS7*'oA*S7*PN]:QR?Y[[SPS5*'oSC*'oS5*PN]:T_R[SPS7*'oS8*'oS7*PN]RRR]=QY2=TR?RRZ
\$\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.