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

1
2
1
\$\begingroup\$

05AB1E, 7 bytes

3LÌ*O²-

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can replace 3L with ā as the first input will be a list of length 3 anyway. \$\endgroup\$
    – kalsowerus
    Commented Jul 6, 2017 at 13:34
1
\$\begingroup\$

GolfScript, 23 bytes

~[.;3,]zip{~3+*}%{+}*\-

Try it online!

Takes input as difficulty rating [num_of_3_pars num_of_4_pars num_of_5_pars].

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

PHP>=7.1, 43 bytes

[,$x,$y,$z,$d]=$argv;echo$x*3+$y*4+$z*5-$d;

PHP Sandbox Online

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

Pyth, 9 bytes

-s*V}3 5E

Try it here.

Takes input as difficulty rating\n[num_of_3_pars, num_of_4_pars, num_of_5_pars].

\$\endgroup\$
2
  • \$\begingroup\$ The code you posted and the TIO don't match. \$\endgroup\$
    – jacoblaw
    Commented Jul 5, 2017 at 16:41
  • \$\begingroup\$ @jacoblaw Sorry that's what you get for writing code by hand >_< \$\endgroup\$ Commented Jul 5, 2017 at 16:43
1
\$\begingroup\$

CJam, 12 bytes

{6,3>.*:+\-}

Try it online!

-1 thanks to Challenger5.

Takes input as difficulty rating [num_of_3_pars num_of_4_pars num_of_5_pars].

\$\endgroup\$
1
  • \$\begingroup\$ Use 6,3> instead of 345Ab to save a byte. \$\endgroup\$ Commented Jul 5, 2017 at 16:54
1
\$\begingroup\$

Batch, 28 bytes

@cmd/cset/a%1*3+%2*4+%3*5-%4

Can't believe I'm tying with Python!

\$\endgroup\$
4
  • \$\begingroup\$ Just curious, do you need the cmd/c - if the rest is in a batch file won't it just work? \$\endgroup\$ Commented Jul 6, 2017 at 1:28
  • \$\begingroup\$ @JerryJeremiah set/a normally doesn't print its result, so I would have to assign it to a variable and echo it, which costs far too many bytes. However, when you use it interactively then it does print the result. Invoking cmd/c fools it into printing the result. \$\endgroup\$
    – Neil
    Commented Jul 6, 2017 at 7:53
  • \$\begingroup\$ Would the rules require the @ sign, which only hides the command? \$\endgroup\$
    – trlkly
    Commented Jul 6, 2017 at 11:20
  • \$\begingroup\$ @trlkly I'm not actually sure, so I've been erring on the side of caution. \$\endgroup\$
    – Neil
    Commented Jul 6, 2017 at 14:08
1
\$\begingroup\$

QBIC, 14 bytes

?:*3+:*4+:*5-:

Functionally equivalent to this.

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

MATL, 7 bytes

3:5*si-

Try it online!

Input vector times range 3:5 minus the second input. Contrary to my Octave answer, it's actually shorter to have the inputs as two separate inputs, and shorter to element-wise multiply, then sum, than to do a direct dot product.

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

cQuents, 14 bytes

#|1:3A+4B+5C-D

Try it online!

Input is 3par 4par 5par diff. cQuents is not at all built for this type of problem, but it still did fairly well.

Explanation

#|1               Append 1 to the end of the user input - call it n
   :              Mode: sequence
    3A+4B+5C-D    Each item in the sequence equals the first input times three plus
                  the second input times four plus the third input times five minus
                  the fourth input. The last input, n, which came from the program's
                  parameters, prints the nth item in the sequece.
                  
\$\endgroup\$
1
\$\begingroup\$

Pyt, 13 8 bytes

35Ř←*Ʃ←-

Takes as input from stdin inputs separately in the following order: [num_of_3_pars,num_of_4_pars,num_of_5_pars], difficulty_rating

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

K (ngn/k), 12 bytes

{-y-/x*3+!3}

Try it online!

Called like f[1 2 3;4]. Takes the number of par 3/4/5 holes as one list of three values, and the difficulty rating as the second arg.

  • x*3+!3 generate 3 4 5, multiplying by the first input
  • -y-/ equivalent to (-y)+/x, or (+/x)-y. In this instance, the leading - negates the entire y-/... expression.
\$\endgroup\$
1
\$\begingroup\$

MMIX, 24 bytes (6 instrs)

00000000: 2a020202 28000000 26020203 2a000100  *£££(¡¡¡&££¤*¡¢¡
00000010: 22000002 f8010000                    "¡¡£ẏ¢¡¡
scratch 4ADDU $2,$2,$2      // p5 *= 5
        2ADDU $0,$0,$0      // p3 *= 3
        SUBU  $2,$2,$3      // p5 -= diff
        4ADDU $0,$1,$0      // p3 += 4 * p4
        ADDU  $0,$0,$2      // p3 += p5
        POP   1,0           // return p3

I ordered the instructions to avoid as many short-term data dependencies as possible.

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

Factor, 23 22 bytes

[ { 3 4 5 } v. - neg ]

Try it online!

-1 thanks to @Bubbler

The quotation takes the input as difficulty list.

  • { 3 4 5 } v. Take the dot product between the input list and { 3 4 5 }.
  • - neg Subtract the difficulty rating from the dot product.
\$\endgroup\$
1
  • \$\begingroup\$ - neg is one byte shorter than swap -. \$\endgroup\$
    – Bubbler
    Commented May 20, 2021 at 0:56
1
\$\begingroup\$

BRASCA, 18 bytes

Takes input as <3-par>,<4-par>,<5-par>,<difficulty>

Ci',G,3*$4*+$5*+$-

Try it online!

Explanation

C                   - Set implicit output to number mode
 i',G               - Use numbers instead of charcodes for 0-9. Then split by commas.
     ,              - Reverse stack so the 3par is at the top.
      3*            - 3par * 3
        $4*+        - 4par * 4, then add to 3par
            $5*+    - 5par * 5, then add to total
                $-  - total - difficulty
<implicit>          - Print it
\$\endgroup\$
1
\$\begingroup\$

Vyxal as, 8 7 bytes

-1 thanks to Aaron Miller

2ʀ3+uJ*

Takes input as par3 \n par4 \n par5 \n difficulty. Try it Online!

2ʀ3+uJ*
2ʀ        # Push the range [0, 1, 2]
  3+      # Add 3: [3, 4, 5]
    uJ    # Append -1: [3, 4, 5, -1]
      *   # Multiply the list by its counterpart in...
          # the (implicit) input, and...
          # Print the sum of the result (s flag) 
\$\endgroup\$
1
0
\$\begingroup\$

TI-Basic, 19 bytes

Prompt A,B,C,D:3A+4B+5C-D

Alternatively (21 bytes):

3Ans(1)+4Ans(2)+5Ans(3)-Ans(4
\$\endgroup\$
0
\$\begingroup\$

Add++, 27 bytes

D,g,@@@@!,3 4 5 1ECBcB*0$_s

Try it online!

How it works

D,g,        - Create a function called g...
    @@@@    - ...that takes 4 arguments...
    !,      - ...as a list. e.g.     [4 3 2 1]
    3 4 5 1 - Push 3 4 5 1; STACK = [[4 3 2 1] 3 4 5 1]
    EC      - Collect;      STACK = [[4 3 2 1] [3 4 5 1]]
    Bc      - Zip;          STACK = [[4 3] [3 4] [2 5] [1 1]]
    B*      - Products;     STACK = [12 12 10 1]
    0$_     - Negate;       STACK = [12 12 10 -1]
    s       - Sum;          STACK = [33]
\$\endgroup\$
0
\$\begingroup\$

Ly, 13 bytes

3n*4n*5n*&+n-

Try it online!

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

Desmos, 21 bytes

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

Try It On Desmos!

Input is a function where \$a=3\ \text{par holes},b=4\ \text{par holes},c=5\ \text{par holes},d=\text{difficulty rating}\$

Example Test Case:

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

Stax, 13 bytes

╨E⌡u╥Diüaë¬~▼

Run and debug it

If I can find a way to read in the array and the number as is instead of evaluating them from string, this program will be shorter.

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

PHP, 66 bytes

list($a,$b,$c,$d,$e)=$argv;echo eval("return 3*$b+4*$c+5*$d-$e;");


first attempt:

PHP, 145 bytes

<?php $a=$_SERVER['argv'];$b=[0,3,4,5,-1];$r=array_map(function($x,$y){return$x*$y;},$a,$b);echo array_reduce($r,function($i,$c){return$i+=$c;});

Ungolfed:

<?php
$a = $_SERVER['argv'];
$b = [0,3,4,5,-1]; // the first element always contains the filename, and is hereby disregarded
$r = array_map(function($x,$y){return $x*$y;},$a,$b);
echo array_reduce($r, function ($i,$c) {return $i+=$c;});


With 10, 5 and 3 holes respectivly, and a difficulty of 5, it's to be called with $ php codegolf.php 10 5 3 5

Note: it throws a warning, as we multiply a string with zero. Add 1 byte for the error-supression-operator to get rid if that.

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

Julia 1.0, 13 bytes

v->[3:5;-1]'v

Try it online!

improvement of Dennis's Julia 0.5 answer

\$\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.