15
\$\begingroup\$

In golf, the standard scratch of a course is calculated using this formula:

(3*num_of_3_par_holes + 4*num_of_4_par_holes + 5*num_of_5_par_holes) - difficulty_rating

Your task is to calculate the standard scratch of a golf course, given these 4 inputs.

You should take input in any standard form in the format

[num_of_3_pars, num_of_4_pars, num_of_5_pars], difficulty rating

but if it saves bytes, you many take input in a different way.

You should output the final result by any accepted method on meta, such as returning from a function.

Shortest code wins because this is !

\$\endgroup\$
4
  • \$\begingroup\$ Will all inputs be integers and at least 1? (Also, will the output always be positive?) \$\endgroup\$
    – Doorknob
    Commented Jul 5, 2017 at 15:20
  • \$\begingroup\$ Can we take inputs reversed? \$\endgroup\$ Commented Jul 5, 2017 at 15:20
  • 16
    \$\begingroup\$ As trivial as this is, a few test cases would be nice. \$\endgroup\$
    – Dennis
    Commented Jul 5, 2017 at 16:20
  • 12
    \$\begingroup\$ Interesting, a code golf about golf. \$\endgroup\$
    – sergiol
    Commented Jul 6, 2017 at 0:07

52 Answers 52

31
\$\begingroup\$

Scratch, 145 bytes

-2 thanks to boboquack
-??? because writing it in Oto is shorter than English

(Because Scratch is the standard scratch.)

adi()- temi (a)kuati(thadi adi()- temi (b)kuati(thadi adi()- temi (c)kuati(thadi adi()- temi (d)kuati(thadi ma(((c)-((a)+(d)))+((4)*(((a)(b))+(c translation: ask()and wait set(a)to (answer ask()and wait set(b)to (answer ask()and wait set(c)to (answer ask()and wait set(d)to (answer say(((c)-((a)+(d)))+((4)*(((a)+(b))+(c

Here's a sample run:

a=18,b=13,c=41,d=23;answer=124.

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

Jelly, 6 bytes

JḊ~æ.N

Try it online!

How it works

JḊ~æ.N  Main link. Argument: [a, b, c, d]

J       Indices; yield [1, 2, 3, 4].
 Ḋ      Dequeue; yield [2, 3, 4].
  ~     Bitwise NOT; yield [-3, -4, -5].
     N  Negate; yield [-a, -b, -c, -d].
   æ.   Dot product; yield
        (-3)(-a) + (-4)(-b) + (-5)(-c) + (-d) = 3a + 4b + 5c - d.
\$\endgroup\$
10
\$\begingroup\$

Java (OpenJDK 8), 24 bytes

(a,b,c,d)->a*3+b*4+c*5-d

Try it online!

\$\endgroup\$
2
  • 7
    \$\begingroup\$ Yeah, shorter than Python \o/ \$\endgroup\$ Commented Jul 5, 2017 at 15:09
  • \$\begingroup\$ This provides proof that Java and Javascript are the same language! \$\endgroup\$
    – emanresu A
    Commented May 19, 2021 at 10:10
6
\$\begingroup\$

Haskell, 22 bytes

(a#b)c d=3*a+4*b+5*c-d

Try it online! Usage: (3#2)5 7 yields 35.

This not so nice input format is one byte shorter than the straight forward solution:

f a b c d=3*a+4*b+5*c-d

Point-free and nice input format: (23 bytes)

(-).sum.zipWith(*)[3..]

Try it online! Bind to f and call with f [3,2,5] 7.

\$\endgroup\$
6
\$\begingroup\$

Mathematica, 13 14 bytes

{3,4,5,-1}.#&

Thanks to @GregMartin. Take input as a length-4 list.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks to "but if it saves bytes, you many take input in a different way", I think you should take input as a length-4 list and shorten the second solution to {3,4,5,-1}.#& (13 bytes). \$\endgroup\$ Commented Jul 5, 2017 at 17:19
  • \$\begingroup\$ You are right.. \$\endgroup\$
    – Keyu Gan
    Commented Jul 5, 2017 at 17:22
5
\$\begingroup\$

Julia 0.5, 15 bytes

!v=v⋅[3:5;-1]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Is three bytes, or am I miscounting? Would .* be an improvement? edit: Never mind—missed the summing part. \$\endgroup\$ Commented Jul 5, 2017 at 17:29
  • \$\begingroup\$ Yes, three bytes. .* only performs element-wise multiplication; it doesn't take the sum of the products. \$\endgroup\$
    – Dennis
    Commented Jul 5, 2017 at 17:30
4
\$\begingroup\$

Python 3, 28 bytes

lambda a,b,c,d:3*a+4*b+5*c-d

Try it online!

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

Perl6, 16 characters

3* *+4* *+5* *-*

(Yepp, that is a sub.)

Sample run:

> say 3* *+4* *+5* *-*
{ ... }

> say (3* *+4* *+5* *-*)(4, 3, 2, 1)
33

Try it online!

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

Jelly, 10 7 bytes

3r5×⁸S_

Try it online!

-3 bytes thanks to Erik The Outgolfer!

How it works!

3r5×⁸S_  Main link: a, the pars as a list and b, the difficulty rating

     S   The sum of
3r5        [3, 4, 5]
   ×       each element times
    ⁸      the left argument (a)
      _  Subtract the right argument (b)
\$\endgroup\$
1
  • \$\begingroup\$ Standard way to make lists is omitting [] but you can use 3r5×⁸S_ to golf this more (3r5 -> [3, 4, 5], = left argument to differentiate it from the S, × is commutative). \$\endgroup\$ Commented Jul 5, 2017 at 15:29
4
\$\begingroup\$

Javascript, 24 bytes

(a,b,c,d)=>3*a+4*b+5*c-d
\$\endgroup\$
0
3
\$\begingroup\$

C#, 24 bytes

(a,b,c,d)=>a*3+b*4+c*5-d
\$\endgroup\$
3
\$\begingroup\$

x86-64 Machine Code, 14 bytes

8D 3C 7F 8D 14 92 8D 04 B7 01 D0 29 C8 C3

A function following the System V AMD64 calling convention (ubiquitous on Gnu/Linux systems) that takes four integer parameters:

  • EDI = num_of_3_par_holes
  • ESI = num_of_4_par_holes
  • EDX = num_of_5_par_holes
  • ECX = difficulty_rating

It returns a single value, the standard scratch, in the EAX register.

Ungolfed assembly mnemonics:

; int ComputeStandardScratch(int num_of_3_par_holes,
;                            int num_of_4_par_holes,
;                            int num_of_5_par_holes,
;                            int difficulty_rating);
lea   edi, [rdi+rdi*2]    ; EDI = num_of_3_par_holes * 3
lea   edx, [rdx+rdx*4]    ; EDX = num_of_5_par_holes * 5
lea   eax, [rdi+rsi*4]    ; EAX = EDI + (num_of_4_par_holes * 4)
add   eax, edx            ; EAX += EDX
sub   eax, ecx            ; EAX -= difficulty_rating
ret                       ; return, leaving result in EAX

Just a simple translation of the formula. What's interesting is that this is essentially the same code that you would write when optimizing for speed, too. This really shows the power of the x86's LEA instruction, which is designed to load an effective address, but can do addition and scaling (multiplication by low powers of 2) in a single instruction, making it a powerful multi-purpose arithmetic workhorse.

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

dc, 14 characters

?3*?4*+?5*+?-p

The numbers need to be passed on separate lines.

Sample run:

bash-4.4$ dc -e '?3*?4*+?5*+?-p' <<< '4
> 3
> 2
> 1'
33

Try it online!

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

Octave, 14 bytes

@(a)[3:5 -1]*a

Try it online!

About twice as long as the MATL answer. I initially literally ported this to MATL, but it turned out iY* is longer than just *s. Note that the input a, containing the holes in order and then the difficulty, should be a column vector.

\$\endgroup\$
2
  • \$\begingroup\$ As question says you can take the input in any format if it saves bytes, this works for 14: @(a)[3:5 -1]*a. Input is a column vector of [3 holes; 4 holes; 5holes; difficulty] \$\endgroup\$ Commented Jul 7, 2017 at 7:32
  • \$\begingroup\$ @TomCarpenter Oh, I thought the list + number part was compulsory. It's a bit oddly phrased: "You should... but you may". I guess I'll amend my answer then. \$\endgroup\$
    – Sanchises
    Commented Jul 7, 2017 at 9:01
2
\$\begingroup\$

Julia, 21 bytes

f(a,b,c,d)=3a+4b+5c-d

Try it online!

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

Neim, 7 bytes

'π𝐂𝕋𝐬S𝕊

Explanation:

'π         Push 345
           The character ' pushes the next character's index in the codepage plus 100.
           The characters ", + and * do that same thing except add a different number.
           This means that in Neim, all 3 digit numbers can be expressed with 2 characters.
           This commit was pushed 8 days before the answer was posted.
  𝐂        Get the characters
   𝕋       Vectorised multiply with the input
    𝐬       Sum the resulting list
     S𝕊    Subtract the input

Alternative program: 3𝐈ᛖ𝕋𝐬S𝕊

Instead of pushing 345 and then getting the characters, creates the array [1 2 3] using 3𝐈, then adds 2 to each element with .

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ This commit was pushed 8 days before the answer was posted. well, you don't actually need this. \$\endgroup\$ Commented Jul 5, 2017 at 15:59
  • \$\begingroup\$ @EriktheOutgolfer Sure I don't. But I also don't need the explanation. Would you like me to remove that? \$\endgroup\$
    – Okx
    Commented Jul 5, 2017 at 16:01
  • \$\begingroup\$ Basically you don't have to worry about non-competing since meta consensus changed. \$\endgroup\$ Commented Jul 5, 2017 at 16:02
  • \$\begingroup\$ @EriktheOutgolfer I do worry about non-competing because the meta consensus is subjective and unclear. But in this case, there's no reason to complain about it being there. You're using up more time than you otherwise would you didn't post that comment. \$\endgroup\$
    – Okx
    Commented Jul 5, 2017 at 16:06
  • \$\begingroup\$ For some reason, most Neim chars in inline code blocks look like ? boxes. \$\endgroup\$ Commented Jul 5, 2017 at 17:02
2
\$\begingroup\$

C (gcc), 30 bytes

e;f(a,b,c,d){e=3*a+4*b+5*c-d;}

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ No need for an extra variable. Just assign to a. \$\endgroup\$
    – Dennis
    Commented Jul 6, 2017 at 19:39
2
\$\begingroup\$

Swift 3, 25 19 bytes

I realised you do not need the var f=, because you can call it like a Python lambda:

{$0*3+$1*4+$2*5-$3}

Test it online!

Usage: {$0*3+$1*4+$2*5-$3}(a,b,c,d), where a,b,c,d are the parameters.

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

brainfuck, 39 bytes

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

Try it online!

Takes input and prints output as ASCII characters; for example, the value 99 would be represented as c.

Explanation:

,                                       Take the first input in Cell 0
 [      ]                               While the data being pointed to (Cell 0) is nonzero
  ->+++<                                Decrement Cell 0 and add 3 to Cell 1
                                        Now 4 times the first input is in Cell 1
         ,                              Take the second input in Cell 0
          [->++++<]                     Add 4 times the second input to Cell 1
                   ,[->+++++<]          Take the third input in Cell 0 and add five times its value to Cell 1
                              ,         Take the fourth input in Cell 0
                               [    ]   While the data being pointed to (Cell 0) is nonzero
                                ->-<    Decrement Cells 0 and 1
                                     >. Print the value in Cell 1
\$\endgroup\$
2
\$\begingroup\$

Gallina, 38 bytes

Definition f a b c d := 3*a+4*b+5*c-d.
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 18 16 bytes

∧5⟦₁↺b;?z×ᵐġ₃+ᵐ-

Try it online!

-2 thanks to Fatalize.

\$\endgroup\$
2
  • \$\begingroup\$ ∧5⟦₁↺b;?z×ᵐġ₃+ᵐ- is 2 less bytes \$\endgroup\$
    – Fatalize
    Commented Jul 6, 2017 at 7:34
  • \$\begingroup\$ @Fatalize It would've been 3 less bytes if there wasn't a bug with leading integer and input. \$\endgroup\$ Commented Jul 6, 2017 at 13:22
2
\$\begingroup\$

,,,, 12 bytes

↻5×↻4×↻3×↻-#

Explanation

Take input 4, 3, 2, 1 for example.

↻5×↻4×↻3×↻-#

              implicit input                  [4, 3, 2, 1]
↻             rotate the stack clockwise      [1, 4, 3, 2]
 5            push 5                          [1, 4, 3, 2, 5]
  ×           pop 2 and 5 and push 2 * 5      [1, 4, 3, 10]
   ↻          rotate the stack clockwise      [10, 1, 4, 3]
    4         push 4                          [10, 1, 4, 3, 4]
     ×        pop 3 and 4 and push 3 * 4      [10, 1, 4, 12]
      ↻       rotate the stack clockwise      [12, 10, 1, 4]
       3      push 3                          [12, 10, 1, 4, 3]
        ×     pop 4 and 3 and push 4 * 3      [12, 10, 1, 12]
         ↻    rotate the stack clockwise      [12, 12, 10, 1]
          -   pop 10 and 1 and push 10 - 1    [12, 12, 9]
           #  pop 12, 12, 9 and push the sum  [33]
              implicit output
\$\endgroup\$
2
\$\begingroup\$

Cubix, 36 bytes

w;r5*U4I;I3*r;UW;;r;<\r/;r-I/+p+O@;w

Try it online!

      w ; r
      5 * U
      4 I ;
I 3 * r ; U W ; ; r ; <
\ r / ; r - I / + p + O
@ ; w . . . . . . . . .
      . . .
      . . .
      . . .

Watch It Run

A fairly linear program that winds back around onto itself a few times. Basic steps:

  • I3*r;U; get the first input, multiply by 3 and clean up stack
  • I4*/r\ get next input and multiply by 4. Rotate result down.
  • Iw5*Ur;w<;r;;W get next input, multiply by 5 and clean up the stack
  • I-r;w; get last input, subtract from par 5 result and clean up stack
  • /+p+O\@ add to par 4 result, bring par3 result to top add, output and halt
\$\endgroup\$
2
\$\begingroup\$

HP-15C (RPN), 14 bytes

Instruction hex codes:

41 C4 F5 FC C5 F4 FC FA C5 F3 FC FA 31 FB

Readable version:

001 {       44  1 } STO 1
002 {          33 } R⬇
003 {           5 } 5
004 {          20 } ×
005 {          34 } x↔y
006 {           4 } 4
007 {          20 } ×
008 {          40 } +
009 {          34 } x↔y
010 {           3 } 3
011 {          20 } ×
012 {          40 } +
013 {       45  1 } RCL 1
014 {          30 } −

The four numbers are loaded into the stack in order before running the program.

\$\endgroup\$
1
  • \$\begingroup\$ Nice first answer. Welcome to PPCG! \$\endgroup\$ Commented Jul 7, 2017 at 5:13
2
\$\begingroup\$

Excel VBA, 20 Bytes

Anonymous VBE immediate window function that takes input from the range [A3:A6] of which [A3:A5] represent the number of 3,4 and 5 par holes, respectively and [A6] represents the difficulty. Outputs to the VBE immediate window

?[3*A3+4*A4+5*A5-A6]

The above is a condensed version of the call

Debug.Print Application.Evaluate("=3*A3+4*A4+5*A5-A6")

Where "=3*A3+4*A4+5*A5-A6" is given to be the formula of an anonymous cell, as indicated by the [...] wrapper, and ? is the deprecated version of the Print call with an implicit Debug. by context

More fun Version, 34 Bytes

Anonymous VBE immediate window function with same I/O conditions as above.

?[SUMPRODUCT(A3:A5,ROW(A3:A5))-A6]

The above is a condensed version of the call

Debug.Print Application.Evaluate("=SUMPRODUCT(A3:A5,ROW(A3:A5))")

Where "=SUMPRODUCT(A3:A5,ROW(A3:A5))" is given to be the formula of an anonymous cell, as indicated by the [...] wrapper, and ? is the deprecated version of the Print call with an implicit Debug. by context. In this version, the range of [A3:A5] and the row numbers of that range (ROWS(A3:A5)) are passed as arrays

Excel Version, 18 bytes

Of course, the versions above lend themselves thusly to excel versions of

=3*A3+4*A4+5*A5-A6

and

=SUMPRODUCT(A3:A5,ROW(A3:A5))-A6
\$\endgroup\$
2
\$\begingroup\$

R, 25 23 bytes

cat(c(3:5,-1)%*%scan())

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ sum(scan()*3:5)-scan() is slightly smaller \$\endgroup\$
    – MickyT
    Commented Jul 6, 2017 at 19:08
  • \$\begingroup\$ thank you. I usually don't try scan when there are multiple inputs like this, but whaddya know! \$\endgroup\$
    – Giuseppe
    Commented Jul 6, 2017 at 19:13
2
\$\begingroup\$

Husk, 10 9 bytes

`-Σz*d345

Try it online!

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

Befunge, 15 bytes

&3*&4*+&5*+&-.@

Try it online!

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

Japt, 12 bytes

*3+V*4+W*5-X

Test it

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

Braingolf, 14 bytes

3*>4*>5*>>++,-

Try it online!

\$\endgroup\$

Your Answer

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