68
\$\begingroup\$

I love math. But I can't find a single calculator that can multiply correctly. They seem to get everything right except 6*9 (It's the question to life, the universe, and everything! How could they get that wrong?!). So I want you all to write a function for me that can multiply 2 numbers correctly (and 6*9 equals 42 instead of 54. 9*6 equals 54 still).

Oh, and I'm going to have to build the source in Minecraft so... fewest bytes win!

Recap

  • Take 2 numbers as input (type doesn't matter, but only 2 items will be passed, and order must be consistent. So streams, and arrays are ok as long as they preserve the order they where passed in. I.e., a map won't work because it doesn't preserve the order)
  • Output multiple of both numbers except if they are 6 and 9, then output 42 (order matters!)
    • PS. I never was really good with counting, so I think only integers from 0 to 99 are real numbers (type used doesn't matter)
  • Fewest bytes per language wins!

Leaderboard:

var QUESTION_ID=124242,OVERRIDE_USER=61474;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+r.match(SCORE_REG)[0],language:r.match(LANG_REG)[0].replace(/<\/?[^>]*>/g,"").trim(),link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/\d+((?=$)|(?= Bytes))/i,OVERRIDE_REG=/^Override\s*header:\s*/i;LANG_REG=/^[^,(\n\r]+/i
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/codegolf/all.css?v=617d0685f6f3"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
12
  • 83
    \$\begingroup\$ Coincidentally the question id has 42 twice in it. \$\endgroup\$ Commented Jun 3, 2017 at 6:00
  • 2
    \$\begingroup\$ if only integers up to 99 are real numbers, then e.g. 9 * 11 is a valid prompt but 10 * 10 is not, right? \$\endgroup\$ Commented Jun 3, 2017 at 8:23
  • 18
    \$\begingroup\$ @EriktheOutgolfer …and the 12 stands for what to subtract if input is 6 and 9. \$\endgroup\$
    – Adám
    Commented Jun 3, 2017 at 23:43
  • 3
    \$\begingroup\$ @Adám Just the absolutely perfect question id for this question. \$\endgroup\$ Commented Jun 4, 2017 at 7:16
  • 15
    \$\begingroup\$ @EriktheOutgolfer Even if you read the ID backwards. \$\endgroup\$
    – T. Sar
    Commented Jun 5, 2017 at 12:57

87 Answers 87

2
\$\begingroup\$

Jelly, 9 bytes

42P⁼6,9$?

Try it online!

Takes list of numbers as input.

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

S.I.L.O.S, 81 67 bytes

readIO
J=i
readIO
a=(J-6)^2+(i-9)^2
a/a
a-1
a*12
x=J*i+a
printInt x

Try it online!

In some sense addition functions as an interesting NAND gate in SILOS.

-14 bytes thanks to @Leaky Nun

Essentially we create a number "a" which is 0 (falsy) iff j is 6 and i=9, then we divide it by itself subtract one and multiply it by 12 in order to add to our product.

If "a" was 1 after subtracting one and multiplying, it becomes a no-op, however in the case where a is 0, 0/0 silently throws an error (which is auto-magically caught) a becomes 0, and then becomes -1 and we end up subtracting 12 from our product.

\$\endgroup\$
5
  • \$\begingroup\$ 69 bytes \$\endgroup\$
    – Leaky Nun
    Commented Jun 3, 2017 at 3:02
  • \$\begingroup\$ 67 bytes \$\endgroup\$
    – Leaky Nun
    Commented Jun 3, 2017 at 3:06
  • \$\begingroup\$ @LeakyNun ooh, that's clever. \$\endgroup\$ Commented Jun 3, 2017 at 11:09
  • \$\begingroup\$ Actually, 0/0 becomes 0. \$\endgroup\$
    – Leaky Nun
    Commented Jun 3, 2017 at 12:43
  • \$\begingroup\$ @LeakyNun I meant to say becomes 0, and then decremented. Fixing. \$\endgroup\$ Commented Jun 3, 2017 at 13:14
2
\$\begingroup\$

Convex, 16 14 13 bytes

_6 9¶=\:*42¶=

Try it online!

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

shortC, 23 bytes

Df(a,b)a==6&b==9?42:a*b

Try it online!

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

J, 11 bytes

*-12*6 9-:,

Try it online!

Surprisingly elegant.

*-12*6 9-:,
     6 9-:,     see if 6 9 matches the paired input
  12*           multiply that by 12
 -              and subtract this from
*               the product of the inputs
\$\endgroup\$
2
\$\begingroup\$

PHP, 35 bytes

<?=([$a,$b]=$_GET)==[6,9]?42:$a*$b;
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Sadly this fails for more than just 6 * 9 e.g. 7 * 9 = 49. \$\endgroup\$
    – Christoph
    Commented Jun 4, 2017 at 9:50
  • \$\begingroup\$ couldn't get the above to work, this works <?=($a=$_GET)==[6,9]?42:$a[0]*$a[1]; \$\endgroup\$
    – inarilo
    Commented Jun 5, 2017 at 12:51
  • 1
    \$\begingroup\$ @inarilo It requires PHP 7.1: sandbox.onlinephpfunctions.com/code/…. \$\endgroup\$
    – user63956
    Commented Jun 5, 2017 at 12:56
  • \$\begingroup\$ @inarilo $b!=9|$a!=6 could have solved the problem ... but at what cost! \$\endgroup\$
    – Titus
    Commented Jun 12, 2017 at 16:13
2
\$\begingroup\$

Prolog, 45 Bytes

m(6,9,42).
m(X,Y,Z):-(X=\=6;Y=\=9),Z is X*Y.
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the site! Always good to see someone golfing in Prolog :). I have edited this answer to include code formatting. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 5, 2017 at 16:15
  • \$\begingroup\$ @WheatWizard Thank you :). Yes I've noticed it's a very rare occurrence :D \$\endgroup\$
    – s3attar
    Commented Jun 7, 2017 at 0:40
2
\$\begingroup\$

Octave, 23 bytes

@(x,y)x*y-12*~(x-6|y-9)

Try it online!

Explanation

~ is logical NOT, and | is logical OR. So, in pseudocode, this computes

x*y - 12*NOT(x-6 OR y-9)
\$\endgroup\$
0
2
\$\begingroup\$

Python3, 59 bytes

-1 bytes thanks to Stephen

a=input();print(42if a=='6 9'else eval(a.replace(' ','*')))

Try it online!

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

C#, 144 bytes

class I{I(int x)=>v=x;int v;public static int operator*(I a,I b)=>a.v==6&&b.v==9?42:a.v*b.v;public static implicit operator I(int x)=>new I(x);}

Run in C# Pad

Usage: (I)6*9

Explanation

class I {
    // Constructor saves the int value in field v
    I(int x) => v = x;
    int v;

    // Redefine the multiplication operator
    public static int operator*(I a, I b) => 
        a.v==6 && b.v==9 ? // If the values are 6 and 9,
            42 : // return 42, else
            a.v * b.v; // return the normal multiplication

    // Add implicit conversion from int to I, so `I x = 5;` is valid
    public static implicit operator I(int x) => 
        new I(x);
}

Usage:

(I)6*9; // Convert 6 to I. To be able to apply the operator, 9 will
        // be implicitly converted as well.

It's not the shortest possible approach within the challenges terms, but I figured it interesting enough to post.


C#, 25 bytes, but not as fun

(x,y)=>x==6&&y==9?42:x*y;

Run in C# Pad

\$\endgroup\$
2
  • \$\begingroup\$ Why not use a lambda? You could probably implement one in a small fraction of the bytes as this solution. \$\endgroup\$
    – user18932
    Commented Jun 5, 2017 at 0:13
  • \$\begingroup\$ This is code golf not a business solution. Can you at least try to golf this solution? \$\endgroup\$ Commented Jun 5, 2017 at 8:39
2
\$\begingroup\$

x86_64 machine language for Linux, 20 19 18 bytes

00:       83 fe 09                cmp    $0x9,%esi
03:       75 09                   jne    0xe
05:       31 c0                   xor    %eax,%eax
07:       83 ff 06                cmp    $0x6,%edi
0a:       b0 2a                   mov    $0x2a,%al
0c:       74 03                   je     0x11
0e:       96                      xchg   %esi,%eax
0f:       f7 ef                   imul   %edi
11:       c3                      retq

If you want to Try it online! for youself, compile and run the following C program

#include<stdio.h>
#include<math.h>

int h(int a,int b){return a-6|b-9?a*b:42;}
const char hh[]="\x83\xfe\tu\t1\xc0\x83\xff\6\xb0*t\3\x96\xf7\xef\xc3";
int main(){
  for( int f=-10; f<10; f++ ) {
    for( int g = -10; g<10; g++ ) {
      printf( "%d %d %d %d\n", f, g, h(f, g), ((int(*)(int,int))hh)(f,g) );
    }
  }
}
\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 16 15 13 bytes

prod(Ans)-12prod(Ans={6,9

Value is returned as from any function.

\$\endgroup\$
4
  • \$\begingroup\$ Input :XY-12(X=6 and Y=9 is a byte shorter. \$\endgroup\$
    – lirtosiast
    Commented Jul 11, 2017 at 1:05
  • \$\begingroup\$ @lirtosiast Thanks! \$\endgroup\$
    – Timtech
    Commented Jul 11, 2017 at 1:07
  • \$\begingroup\$ prod(Ans)-12prod(Ans={6,9 using input in Ans for 13 bytes \$\endgroup\$ Commented Dec 12, 2017 at 16:45
  • \$\begingroup\$ @pizzapants184 Smart! That's probably the optimal solution. \$\endgroup\$
    – Timtech
    Commented Dec 14, 2017 at 0:38
2
\$\begingroup\$

Julia 0.6, 26 bytes

This isn't the shortest answer, but it may be the most transformative. This define a new infix function (not *!). Because Julia supports generic programming, is now defined for all combinations of types, and works nearly anywhere * works. For example:

6 ∗ 9 = 42 # with integers
9 ∗ 6 = 54
5 ∗ 5 = 25
6.0 ∗ 9 = 42 # and floating points
(6 + 0 ∗ im) ∗ 9 = 42 # and complex
6.0 ∗ 9 // 1 = 42 # rational numbers
"abc" ∗ "def" = "abcdef" # string concatenation
6 .∗ [1 4 9; 4 5 6; 7 8 9] = [6 24 42; 24 30 36; 42 48 42] 
# even elementwise multiplication with an array
∗(x,y)=x==6&&y==9?42:x*y

Try it online!

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

Minecraft Functions (18w11a, 1.13 snapshots), 317 bytes

minecraft calculator

Uses two functions

a:

scoreboard objectives add c dummy
execute if score z a matches 6 if score z b matches 9 run scoreboard players set z c 1
execute if score z c matches 1 run tellraw @s {"text":42}
execute unless score z c matches 1 run function b

b:

scoreboard players operation z a *= z b
tellraw @s {"score":{"name":"z","objective":"a"}}

Usage

Takes input through two scoreboard objectives on the fake player z, a and b, set them with /scoreboard players set z a 6 and /scoreboard players set z b 9. Then call the function with /function a

For the function to run again, the c objective has to be zeroed with /scoreboard players set z c 0

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Guess OP doesn't need to build it themselves now :P \$\endgroup\$
    – Makonede
    Commented Apr 16, 2021 at 17:28
2
\$\begingroup\$

><>, 16 bytes

:{:}*}9=$6=*c*-n

Try it online!

Pretty simple. Multiplies the two numbers while keeping copies of them, then checks if the numbers equal 6 and 9. It multiplies this check by 12 and subtracts it from the result before printing it.

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

MathGolf, 10 bytes

α96α=¿ÅJ∞*

Try it online!

Explanation

The footer is just to format the output.

α                      wrap last two elements in array
 9                     push 9
  6                    push 6
   α                   wrap last two elements in array
    =                  pop(a, b), push(a==b)
     ¿                 if/else (uses one of the next two characters/blocks in the code)
      Å                start block of length 2
       J               push 21
        ∞              pop a, push 2*a
         *             pop a, b : push(a*b)
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 18 9 bytes

69f⁼[42|Π

Try it Online!

Sigh terrible first re-try at Vyxal after a few months.

Explanation

69f⁼[42|Π
69f⁼      # list equals [6,9]
    [     # if statement block
     42   # return 42 if true
          # else product of list

-9 thx to @Lyxal

\$\endgroup\$
1
  • \$\begingroup\$ Try it Online! for 9 bytes. \$\endgroup\$
    – lyxal
    Commented Jan 1, 2023 at 22:25
1
\$\begingroup\$

Ohm, 11 bytes

*┼6E┘9E&?42

Try it online!

*            Implicit inputs. Multiply
 ┼6E         First input == 6?
    ┘9E      Second input == 9?
       &     Logical and
        ?42  If true push (and output) 42
             Else implicit output the product
\$\endgroup\$
1
\$\begingroup\$

Batch, 30 bytes

@cmd/cset/a%1*%2-12*!(%1%2-69)

Subtracts 12 if the input numbers were 6 and 9.

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

dc, 22 bytes

[42 1]sX[k6=X]sZd9=Z*p

Try it online!

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

GolfScript, 18 bytes

."6 9"={;42}{~*}if

Try it online!

Strict input format: numbers on a single line separated by a single space, nothing else.

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

JavaScript, 23 22 bytes

x=>y=>x==6&y==9?42:x*y

-1 byte thanks to Shaggy

f=
x=>y=>x==6&y==9?42:x*y

console.log(f(6)(9))
console.log(f(9)(6))

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Save a byte by using a single &. \$\endgroup\$
    – Shaggy
    Commented Jun 3, 2017 at 10:27
1
\$\begingroup\$

F#, 25 bytes

function 6,9->42|x,y->x*y

Try it!

\$\endgroup\$
4
  • \$\begingroup\$ @WheatWizard May be of help: tio.run/#fs-mono \$\endgroup\$
    – Beta Decay
    Commented Jun 5, 2017 at 12:16
  • \$\begingroup\$ @WheatWizard I know, I just thiught you didn't have a method of running F# programs \$\endgroup\$
    – Beta Decay
    Commented Jun 5, 2017 at 13:44
  • 1
    \$\begingroup\$ @WheatWizard I added a link where you can run the code. The code contains more stuff (binding the function to an identifier, calling it, printing the result) so you can see what's going on. \$\endgroup\$
    – Asik
    Commented Jun 5, 2017 at 14:34
  • \$\begingroup\$ You can get rid of the () around x,y and 6,9 it is not necessary. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 5, 2017 at 14:39
1
\$\begingroup\$

PowerShell, 42 bytes

Appropriately, but not very impressively, a 42 byte scriptblock function which is a basic if/else made from array indexing with a little number-to-string type coercing happening:

$f={param($a,$b)(($a*$b),42)["$a$b"-eq69]}

e.g.

PS C:\> & $f 6 9
42

PS C:\> & $f 9 6
54

Previous approach, 44 bytes:

$f={(($a=$args-join"*"),42)[$a-eq'6*9']|iex}
\$\endgroup\$
1
\$\begingroup\$

Prolog

multiplication(6, 9, 42) :- !.
multiplication(X, Y, Z) :- Z is X * Y.

Ugly Prolog, 33 bytes

m(6,9,42):-!.
m(X,Y,Z):-Z is X*Y.

Test it here

\$\endgroup\$
7
  • \$\begingroup\$ It looks like you have a good deal of extra whitespace here. You could save some bytes by removing almost all of the whitespace in this answer. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 6, 2017 at 14:55
  • \$\begingroup\$ Oh but I like white spaces and bytes. It wouldn't be Prolog if it was optimised under any metric and not pretty. \$\endgroup\$ Commented Jun 6, 2017 at 15:02
  • 5
    \$\begingroup\$ This is a code-golf competition. The goal is to minimize bytes. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 6, 2017 at 15:05
  • \$\begingroup\$ You need to take input of a sort. How does this work? Does the second line define a function? \$\endgroup\$
    – Riker
    Commented Jun 6, 2017 at 15:11
  • \$\begingroup\$ OkIAddedANewVersion! \$\endgroup\$ Commented Jun 6, 2017 at 15:11
1
\$\begingroup\$

GNU sed, 98 75 + 1 = 99 76 bytes

+1 byte for -r flag. Takes input as two unary numbers separated by a newline. -23 bytes thanks to Ephphatha.

y/1/0/
N
s/^(0{6}\n1{7})11$/\1/
h
s/0+//
x
s/1+//
:
s/0//;TX
G;b
:X
s/\n//g

Try it online! (Delete the # in the "Footer" section if you don't want to count 1s by hand.)

Explanation

Almost a third of this code (line 3 below) is devoted to converting 6×9 to 6×7. The rest is unary multiplication, which is easy: To multiply a and b, just replace each digit in a with the digits of b.

y/1/0/    # Replace 1s on this line with 0s
N         # Append next line to this line
s/^(0{6}\n1{7})11$/\1/  # If the inputs are 6 and 9,, replace the 9 with 7
h         # Copy to hold space
s/0+//    # Delete 0s (1st argument)
x         # Swap pattern and hold space
s/1+//    # Delete 1s (2nd argument)
:
  s/0//;TX  # Delete a 0; if there weren't any, branch to :X
  G;b       # Append a copy of the hold space (1s) to pattern space; branch to :
:X
s/\n//g   # Remove all newlines
\$\endgroup\$
2
  • \$\begingroup\$ "and 6*9 equals 42 instead of 54. 9*6 equals 54 still" - you can save some bytes by only handling the 6\n9 order - s/^(0{6}\n)11(1{7})$/\1\2/ \$\endgroup\$
    – Ephphatha
    Commented Jun 4, 2017 at 11:37
  • \$\begingroup\$ s/^(0{6}\n1{7})11$/\1/ should work as well \$\endgroup\$
    – Ephphatha
    Commented Jun 5, 2017 at 5:27
1
\$\begingroup\$

Noether, 24 bytes

{I~a6=I~b9=&}{42P}{ab*P}

Try it here!

Explanation:

{           }            - If
 I                       - Push user input
  ~a                     - Store top item of stack in variable a
    6                    - Push 6
     =                   - Pop top two items and return 1 if they are equal
      I                  - Push user input
       ~b                - Store top item of stack in variable b
         9               - Push 9
          =              - Pop top two items and return 1 if they are equal
           &             - Pop top two items and bitwise AND
             {   }       - If true then
              42         - Push 42
                P        - Print top item
                  {    } - Else
                   a     - Push variable a onto the stack
                    b    - Push variable b onto the stack
                     *   - Pop top two items and multiply
                      P  - Print top item
\$\endgroup\$
3
  • \$\begingroup\$ What do I put in input to make this work? \$\endgroup\$
    – Tezra
    Commented Jun 18, 2017 at 15:15
  • \$\begingroup\$ @Tezra Hold on I'll edit the link \$\endgroup\$
    – Beta Decay
    Commented Jun 18, 2017 at 16:01
  • \$\begingroup\$ @Tezra See the new link :D \$\endgroup\$
    – Beta Decay
    Commented Jun 18, 2017 at 16:04
1
\$\begingroup\$

q/kdb+, 19 17 bytes

Solution:

{(prd x;42)x~6 9}

Example:

q){(prd x;42)x~6 9}6 9
42
q){(prd x;42)x~6 9}6 10
60
q){(prd x;42)x~6 9}9 6
54

Explanation:

If the input is the exact list (6;9) return 42 else multiply together:

{(prd x;42)x~6 9} / the solution
{               } / lambda function
           x~6 9  / is input the list (6;9) (0b or 1b)
 (     ;  )       / two item list 
  prd x           / if false (index 0) then multiply together
        42        / if true (index 1) then return 42
\$\endgroup\$
1
\$\begingroup\$

Excel, 28 bytes

Takes inputs in A1 and B1

=IF(AND(A1=6,B1=9),42,A1*B1)
\$\endgroup\$
1
\$\begingroup\$

Commentator, 223 bytes

//{-//
?{-{- {- -}-}-}e# e#
-}
?{- {- -}-}e# e#
{-{-{-{-{-         e#-}//
:# 
<#-}-}-}-}      e#//
:# 
<#
:{-{-{-{-{-# -}-}-}-}-}
<#
:{-{-{-{-#{-#-}-}-}-}-}
{-{-{-{-!{-!-}-}-}!{-{-{-            -}!-}#{-
?-} {-e# e#
-}e#-}//

Try it online!

How it works

An s is used to denote a space. Example inputs 6 and 9. Tape head denoted with a surrounding ()

//                                             - Take input;     TAPE = [ (6)  0   0   0   0   0 ]
  {-                                           - Move right;     TAPE = [  6  (0)  0   0   0   0 ]
    //                                         - Take input;     TAPE = [  6  (9)  0   0   0   0 ]
?                                              - While tape head...
 {-                                            -   Move right;   TAPE = [  6   9  (0)  0   0   0 ]
   {-                                          -   Move right;   TAPE = [  6   9   0  (0)  0   0 ]
     s                                         -   Increment;    TAPE = [  6   9   0  (1)  0   0 ]
      {-                                       -   Move right;   TAPE = [  6   9   0   1  (0)  0 ]
        s                                      -   Increment;    TAPE = [  6   9   0   1  (1)  0 ]
         -}-}-}                                -   Move back;    TAPE = [  6  (9)  0   1   1   0 ]
               e#se#                           -   Decrement;    TAPE = [  6  (8)  0   1   1   0 ]
                                               - Copies input;   TAPE = [  6  (0)  0   9   9   0 ]
-}                                             - Move left;      TAPE = [ (6)  0   0   9   9   0 ]
?                                              - While tape head...
 {-                                            -   Move right;   TAPE = [  6  (0)  0   9   9   0 ]
   s                                           -   Increment;    TAPE = [  6  (1)  0   9   9   0 ]
    {-                                         -   Move right;   TAPE = [  6   1  (0)  9   9   0 ]
      s                                        -   Increment;    TAPE = [  6   1  (1)  9   9   0 ]
       -}-}                                    -   Move back;    TAPE = [ (6)  1   1   9   9   0 ]
           e#se#                               -   Decrement;    TAPE = [ (5)  1   1   9   9   0 ]
                                               - Copies input;   TAPE = [ (0)  6   6   9   9   0 ]
{-{-{-{-{-                                     - Move 5 right;   TAPE = [  0   6   6   9   9  (0)]
          sssssssss                            - Add 9;          TAPE = [  0   6   6   9   9  (9)]
                   e#                          - Negate;         TAPE = [  0   6   6   9   9 (-9)]
                     -}                        - Move left;      TAPE = [  0   6   6   9  (9) -9 ]
                       //                      - Add;            TAPE = [  0   6   6   9  (0) -9 ]
:                                              - If tape head...
 #                                             -   Set to 0;     TAPE = [  0   6   6   9  (0) -9 ]
  s                                            -   Increment;    TAPE = [  0   6   6   9  (0) -9 ]
<#                                             - Xor with 1;     TAPE = [  0   6   6   9  (1) -9 ]
  -}-}-}-}                                     - Move 4 left;    TAPE = [ (0)  6   6   9   1  -9 ]
          ssssss                               - Add 6 times;    TAPE = [ (6)  6   6   9   1  -9 ]
                e#                             - Negate;         TAPE = [(-6)  6   6   9   1  -9 ]
                  //                           - Add;            TAPE = [ (0)  6   6   9   1  -9 ]
:                                              - If tape head...
 #                                             -   Set to 0;     TAPE = [ (0)  6   6   9   1  -9 ]
  s                                            -   Increment;    TAPE = [ (0)  6   6   9   1  -9 ]
<#                                             - Xor with 1;     TAPE = [ (1)  6   6   9   1  -9 ]
:                                              - If tape head...
 {-{-{-{-{-                                    -   Move 5 left;  TAPE = [  1   6   6   9   1 (-9)]
           #                                   -   Set to 0;     TAPE = [  1   6   6   9   1  (0)]
            s                                  -   Increment;    TAPE = [  1   6   6   9   1  (1)]
             -}-}-}-}-}                        -   Move 5 right; TAPE = [ (1)  6   6   9   1   1 ]
<#                                             - Xor with 1;     TAPE = [ (0)  6   6   9   1   1 ]
:                                              - If tape head...
 {-{-{-{-                                      -   Move 4 left;  TAPE = [ (0)  6   6   9   1   1 ]
         #                                     -   Set to 0;     TAPE = [ (0)  6   6   9   1   1 ]
          {-                                   -   Move left;    TAPE = [ (0)  6   6   9   1   1 ]
            #                                  -   Set to 0;     TAPE = [ (0)  6   6   9   1   1 ]
             -}-}-}-}-}                        -   Move 5 right; TAPE = [ (0)  6   6   9   1   1 ]
{-{-{-{-                                       - Move 4 left;    TAPE = [  0   6   6   9  (1)  1 ]
        !                                      - Product;        TAPE = [  0   6   6   9  (1)  1 ]
         {-                                    - Move left;      TAPE = [  0   6   6   9   1  (1)]
           !                                   - Product;        TAPE = [  0   6   6   9   1  (0)]
            -}-}-}                             - Move 3 right;   TAPE = [  0   6  (6)  9   1   0 ]
                  !                            - Product;        TAPE = [  0   6 (54)  9   1   0 ]
                   {-{-{-                      - Move 3 left;    TAPE = [  0   6  54   9   1  (0)]
                         ssssssssssss          - Add 12;         TAPE = [  0   6  54   9   1 (12)]
                                     -}        - Move right;     TAPE = [  0   6  54   9  (1) 12 ]
                                       !       - Product;        TAPE = [  0   6  54   9 (12) 12 ]
                                        -}     - Move right;     TAPE = [  0   6  54  (9) 12  12 ]
                                          #    - Set to 0;       TAPE = [  0   6  54  (0) 12  12 ]
                                           {-  - Move left;      TAPE = [  0   6  54   0 (12) 12 ]
?                                              - While tape head...
 -}                                            -   Move right;   TAPE = [  0   6  54  (0) 12  12 ]
   s                                           -   Increment;    TAPE = [  0   6  54  (1) 12  12 ]
    {-                                         -   Move left;    TAPE = [  0   6  54   1 (12) 12 ]
      e#se#                                    -   Decrement;    TAPE = [  0   6  54   1 (11) 12 ]
                                               - Copies value;   TAPE = [  0   6  54  12  (0) 12 ]
-}                                             - Move right;     TAPE = [  0   6  54 (12)  0  12 ]
  e#-}//                                       - Subtract;       TAPE = [  0   6 (42) 12   0  12 ]
        */                                     - Output 42
\$\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.