36
\$\begingroup\$

In this challenge, submissions will be programs or function which, when given an emoticon such as :-), :(, or :D, will rate their happiness from 0 to 3.

An emoticon will be one of the following:

  • :(: 0
  • :|: 1
  • :): 2
  • :D: 3

Emoticons may also have noses (a - after the :).

Test cases:

:(  -> 0
:-| -> 1
:D  -> 3
:-) -> 2
:|  -> 1

This is a code golf challenge, shortest answer per language wins.

\$\endgroup\$
8
  • 11
    \$\begingroup\$ What about (:? \$\endgroup\$
    – anatolyg
    Apr 2, 2020 at 13:06
  • 1
    \$\begingroup\$ @anatolyg Don't forget ☺and ☻ either \$\endgroup\$ Apr 2, 2020 at 16:20
  • 3
    \$\begingroup\$ @anatolyg What about D:? \$\endgroup\$
    – user92069
    Apr 3, 2020 at 8:59
  • 1
    \$\begingroup\$ or :-{D (someone with a mustache) or :-{D> (someone with a mustache and goatee) or :-{D} (someone with a mustache and beard). For a full list see marshall.freeshell.org/smileys.html \$\endgroup\$ Jun 9, 2020 at 23:43
  • 1
    \$\begingroup\$ It would be cool to see a solution that looked like a smiley ... \$\endgroup\$ Jun 9, 2020 at 23:45

46 Answers 46

1
2
1
\$\begingroup\$

J, 10 9 bytes

-1 byte thanks to FrownyFrog ang Bubbler

'(|)'i.{:

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can drop D as bubbler pointed out. \$\endgroup\$
    – FrownyFrog
    Apr 3, 2020 at 19:34
  • \$\begingroup\$ @FrownyFrog Yes, indeed. Thanks! \$\endgroup\$ Apr 3, 2020 at 20:35
1
\$\begingroup\$

PowerShell, 16 bytes

2*$args[-1]%19%4

Try it online!

PowerShell port of the modulo formula going around. Takes input via splatting

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

Wolfram Language (Mathematica), 38 bytes

Mod[2Last@ToCharacterCode@#,19]~Mod~4&

Try it online!

This borrows Uriel's formula, which happens to be the same length as dingledooper's, but I can't help but wonder if another formula would be shorter. I will investigate.

For comparison, here's the string indexing solution (51 bytes):

("(|)D"~StringPosition~Last@Characters@#)[[1, 1]]-1&

Try it online!

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

MAWP, 27 bytes

|2W!!92W1MP92W1MWA!!4P4WA:.

Try it!

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

Perl 5 -p, 14 bytes

y/(|)D:-/0-3/d

Try it online!

Basically a port of my sed answer, but the y/// command in perl has the additional utilities of deleting extra characters and specifying ranges of characters, so this manages to be shorter and still contain :-/ and D: as essential parts of the code.

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

Factor, 20 bytes

[ last "(|)D"index ]

Try it online!

Explanation:

If ":)" is on the data stack when this quotation is called...

  • last Take the last element of the input.

    Stack: 41 (code point for ))

  • "(|)D" Push a string to the stack.

    Stack: 41 "(|)D"

  • index Find the index of 41 in "(|)D".

    Stack: 2

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

JavaScript, 2335

(・・? Did somebody say emoticons?

a = 
// The function:
(w)=>{(笑)=(w);゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)  =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ,゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; (゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\'; (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];(゚Д゚) [゚o゚]='\"';(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+(゚Д゚)[゚o゚]+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (o^_^o))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (c^_^o)+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (o^_^o))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚Θ゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((゚ー゚) + (o^_^o))+ (゚Д゚) .゚Θ゚ノ+ (゚Θ゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (o^_^o)+ (o^_^o)+ (゚Д゚)[゚ε゚]+((o^_^o) +(o^_^o))+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (o^_^o)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (o^_^o))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (o^_^o))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((゚ー゚) + (o^_^o))+ (゚Д゚) .゚Θ゚ノ+ (゚Θ゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (o^_^o)+ (o^_^o)+ (゚Д゚)[゚ε゚]+((o^_^o) +(o^_^o))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (o^_^o)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚o゚]) (゚Θ゚)) ('_');}
// Execute function:
a(':D');

I took it from this answer, encoded it with aaencode, and wrapped it an the arrow function.

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

brainfuck, 94 bytes

,[[>++<-]>>,]>++++[<++++>-]<[+++<[>->+<[>]>[<+>-]<<[<]>-]>[-]<<<[>>>>>+<<]>>]>-[>+<-----]>---.

Try it online!

Uses Uriel's formula. Here's how it works:

,[[>++<-]>>,]

A nifty input loop that handles the optional nose and doubling the characters. It gets a character, doubles it into the next cell, then moves two cells over and tries to get another character. At the end, the tape looks something like

0 2*c1 0 2*c2 0 2*c3 0 ...
                     ^

This requires an implementation with EOF=0 or no change (which accounts for most of them including TIO).

>++++[<++++>-]<

Put 16 after the last doubled input. Now on to the main loop:

[+++<[>->+<[>]>[<+>-]<<[<]>-]>[-]<<<[>>>>>+<<]>>]

This loop will

  • add 3 to the dividend (current cell). I do this because it's 2 bytes shorter to get 16 rather than 19, and these can be reused to construct 4 for the second dividend.
  • compute the modulus of the previous%current cell.
  • clear out a temporary variable
  • check if the value of the cell 3 indices back is non-zero (which is only true on the first iteration since the loop ends 2 indices to the right of where it started). If it is, increment the cell where the next dividend will be (this sets up the second modulo 4).

For clarity, here's what the tape looks like during the first iteration, before the tempvar is cleaned:

0 2*c1 0 2*c2 0 0 tmp 2*c3%19 0 ...
                  ^

And the same for the second:

0 2*c1 0 2*c2 0 0 0 0 tmp 2*c3%19%4 0 ...
                      ^

So checking the cell 3 indices back is an easy way to see which iteration we're on without using a counter.

>-[>+<-----]>---.

And finally, a bit of wrapping abuse to add 48 to the result so it can be outputted.

There's probably room for improvement, particularly in the input loop or the memory layout, but this is the best I could come up with.

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

Vyxal K, 7 bytes

td19%4%

Try it Online! -1 thanks to exedraj.

\$\endgroup\$
1
  • \$\begingroup\$ 7 bytes \$\endgroup\$
    – lyxal
    Aug 13, 2021 at 0:01
1
\$\begingroup\$

Raku -p 23 Bytes -> 19 Bytes

$_=(4-.ords[*-1]*2%5)%4

$_=160%.ords[*-1]%7      #(with @dingledooper's formula)
\$\endgroup\$
1
  • \$\begingroup\$ 17-byte port of the Perl 5 -p solution: tr:d/(|)D:-/0..3/ \$\endgroup\$
    – bb94
    Jul 20, 2023 at 7:51
1
\$\begingroup\$

Thunno 2 tB, 5 bytes

ṇ;Œ7%

Try it online!

Uses dingledooper's formula.

Explanation

ṇ;Œ7%  # Implicit input
       # Convert to codepoints
ṇ;     # Push compressed integer 160
  Π   # Take 160 mod each codepoint
   7%  # Mod each by 7
       # Implicit output of last item
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 27 24 bytes

-3 Refactor to remove it.

{"(|)D".indexOf(last())}

Try it online!

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

T-SQL, 34 bytes

SELECT CHARINDEX(RIGHT(@,1),'|)D')-1

Requires the variable @ to be declared and assigned the input.

This code takes the right most character, and searches an indexed list of the possible mouthes. CHARINDEX returns a 1-based index. But, failing to find anything returns 0. So we leave the frown out of the search string (making the sad face even sadder).

Try it online! (SQL Fiddle)

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

Hy, 33 bytes

(fn[s](%(%(*(ord(last s))2)19)4))

Try it online!

It's a basic port of Uriel's answer. I might be able to shorten it by getting rid of a % but I need to play around with the syntax.

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

MAWP 0.1, 27 bytes

|2W!!99M1MP99M1MWA!!4P4WA:.

Uses the modulo formula from @Uriel 's answer

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

Golfscript, 10 bytes

-1='(|)D'?

Try It Online!

Explanation:

-1=        # Gets the ASCII value of the final character. 
   '(|)D'? # Finds the index of that ASCII value, in the string '(|)D'. 

And then, the index value is implicitly printed.

\$\endgroup\$
1
2

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.