You're a plumber working on a house, and there's some pipes that must be connected at weird angles. You have 8°, 11.25°, 22.5°, 45°, and 90° fittings at your disposal, and you want to use as few as possible to match the angle as closely as possible.


  • Match the desired angle as closely as possible, with as few fittings as possible. It can be over or under the desired angle.
  • Accuracy is more important than the number of fittings
  • In the case of two different sets of fittings with the same resulting angle, whichever has the fewest number of fittings should be selected.
  • If the two sets use different fittings, match the same angle, and have the same number of fittings, either may be chosen.
  • Your fittings cannot add up to greater than 360 degrees (i.e. no looping all the way around)


Your input is a random integer between (non-inclusive) 0° and 180°, which represents the desired angle.


Your output should be an array where [0]-># of 8° fittings, [1]-># of 11.25° fittings, etc. If your language does not support arrays, you may output a comma separated list, where the first value represents the number of 8° fittings, and so on and so forth.

Test Cases

90° ->[0,0,0,0,1]
24°-> [3,0,0,0,0] ([0,0,1,0,0] uses less fittings, but is less accurate and therefore incorrect)
140°->"2,1,1,0,1" acceptable if language does not support arrays


Lowest byte count for each language wins a high five from me if we ever bump into each other (and the challenge).

  • 4
    \$\begingroup\$ I always comment on new users' posts because they so rarely use the sandbox, and then I check immediately afterwards in case they did to make sure my comment's correct (I've now deleted it). Thanks for using the sandbox! However, while we'd typically recommend leaving your challenges in the sandbox for at least 3 days, you should probably wait for at least one person to upvote it or to leave a comment to show they've checked it, because this was a pretty well-specified challenge but the few edits we've made already should tell you there were still improvements to be made. \$\endgroup\$
    – pxeger
    Jun 11, 2021 at 17:04
  • \$\begingroup\$ @pxeger that's how you have got Pundit :P \$\endgroup\$
    – Wasif
    Jun 11, 2021 at 17:41
  • 2
    \$\begingroup\$ Shouldn't that last case be 2,1,1,0,1? \$\endgroup\$
    – Neil
    Jun 11, 2021 at 17:49
  • \$\begingroup\$ @Neil Yep, whoops! I'll fix it \$\endgroup\$ Jun 11, 2021 at 18:35
  • \$\begingroup\$ To connect pipes at (almost) an angle of 4 degrees, one could use 44x 8-degree fittings plus one 11.25 degree fitting to get a total angle of 363.25 = one full loop and a resulting angle of 3.25 degrees (error of 0.75 degrees). Is this the correct answer for 4? If not, are loops forbidden? \$\endgroup\$ Jun 11, 2021 at 19:49

7 Answers 7


Jelly, 21 bytes


Try it online!

Note - after Nick Kennedy's golfs, this answer is actually identical to Jonathan Allan's answer, which they arrived at independently. Make sure to upvote them.

-9 bytes thanks to Nick Kennedy by rewriting the conversion from counts to total angle

ḶṗSÞ5ðḋ90H3С;8¤ạðÞḢṚ  Main Link
Ḷ                      Lowered range; [0, 1, ..., N-1]
 ṗ5                    Cartesian product; size 5 lists of the above elements
   SÞ                  Sort by sum
     ð           ðÞ    Sort by:
                ạ      - absolute difference of the following and the input:
      ḋ                - dot product with
       90H3С;8¤       - [90, 45, 22.5, 11.25, 8] via the following
       90                - 90
          3С            - repeat 3 times and collect results:
         H               - halve
             ;8          - append 8
                   Ḣ   take the first one (since it'll already be sorted by count and it's stable)
                    Ṛ  reverse it
  • 1
    \$\begingroup\$ -8 bytes Ḷṗ5ðḋ90H3С;8¤ạðÞḢṚ \$\endgroup\$ Jun 11, 2021 at 22:06
  • 2
    \$\begingroup\$ @NickKennedy Oh dot product is smart here. And I don't know how I didn't see that pattern lol; thank you. \$\endgroup\$
    – hyper-neutrino
    Jun 12, 2021 at 1:57
  • 1
    \$\begingroup\$ You've missed the "absolute difference" from your explanation \$\endgroup\$ Jun 12, 2021 at 14:44
  • 1
    \$\begingroup\$ @cairdcoinheringaahing oops :p \$\endgroup\$
    – hyper-neutrino
    Jun 12, 2021 at 15:01

Jelly, 21 bytes


A monadic Link that accepts an integer in \$[0,180]\$ and yields a list of the five pipe counts in the prescribed order.

Try it online! although it'll time out for inputs above \$15\$ :(


Builds all selections of pipes consisting of less than input-angle of each pipe, sorts these by their number of pipes, then (stable) sorts by the difference between the angle-sum and input-angle and yields the first.

Ḷṗ5SÞḋ90H3С;8¤ạɗÞ⁸ḢṚ - Link: angle, A
Ḷṗ5                   - (A) Cartesian product 5 -> all selections of the five pipes with
                                                   up to A-1 of each pipe
   SÞ                 - sort by sum (number of pipes)
                 Þ    - sort (the elements X of that) by:
                ɗ ⁸   -   last three links as a dyad - f(X, A)
              ¤       -     nilad followed by links as a nilad:
      90              -       90
         3С          -       collect & repeat three times:
        H             -         halve -> [90, 45, 22.5, 11.25]
            ;8        -       concatenate 8 -> [90, 45, 22.5, 11.25, 8]
     ḋ                -     (X) dot-product (that)
               ạ      -     (that) absolute-difference (A)
                   Ḣ  - head
                    Ṛ - reverse (to the prescribed order of pipe counts)
  • 1
    \$\begingroup\$ I’ve just realised that my golfs to @hyper-neutrino’s answer have given the same answer as yours - I hadn’t seen your answer before suggesting them, but was combining the best bits of my answer with his existing one. \$\endgroup\$ Jun 12, 2021 at 6:58
  • 1
    \$\begingroup\$ Well almost the same anyway… \$\endgroup\$ Jun 12, 2021 at 7:14

Jelly, 22 bytes


Try it online!

A pair of links which takes a single argument, the number of degrees to match, and returns a list of 5 integers. Hopelessly slow for increasing values, but should theoretically work for all of the required range.

This is a much more efficient implementation but is three bytes longer.


R, 95 88 87 86 bytes

Edit: -1 byte thanks to pajonk


Try it online!

Ungolfed code

function(a){                        # a=desired angle
b=expand.grid(rep(list(23:0),5))    # b=all combinations of 5 elements of 23..0 (note: largest-to-smallest - see below)
                                    # (23 is number of smallest (8-degree) fittings needed to get over 180 degrees)
c=colSums(t(b)*c(8,2^(-2:1)*45))    # c=the total angle made by each combination of fittings
d=which.min((d-a)^2)                # d=the first combination that minimized the absolute difference to angle a
                                    # (we take the square to save a byte getting the absolute difference,
                                    # and we automatically get the combination with the smallest numbrer of fittings
                                    # since we constructed the combinations largest-to-smallest)
return(b[d,])                       # finally, return that element of b
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – pajonk
    Jun 13, 2021 at 13:04
  • 1
    \$\begingroup\$ @pajonk - Thank you! The cleverest tricks always seem so obvious once somebody has shown them to me... \$\endgroup\$ Jun 13, 2021 at 13:36

Julia 0.7, 93 bytes


Try it online!


JavaScript (ES6), 129 bytes


Try it online!


f = (                    // f is a recursive function taking:
  n,                     //   n = input
  a = [0, 0, 0, 0, 0],   //   a[] = pipe counters
  c =                    //   c = current number of pipes
  e =                    //   e = best (smallest) squared error
  C = 1 / 0              //   C = number of pipes for the best solution
) =>                     //
n < (                    //
  n * n < e |            // if the current error is less than the best error
  n * n == e & c < C &&  // or they're equal but we're using less pipes:
  [ e = n * n,           //   update the best error
    C = c,               //   update the best count
    o = a                //   update the output
  ]                      // if e, C and o have been updated, we compare n with
                         // an array, which is always falsy
) ?                      // otherwise, we test whether n is negative:
  o                      //   in which case we return o[]
:                        // else:
  a.map(                 //
  (_, i, [...a]) =>      //   for each value at position i in a[], using a copy
                         //   of a[]:
    f(                   //     do a recursive call:
      n - (              //       subtract from n:
        i ?              //         if i is not equal to 0:
          90 / (16 >> i) //           11.25, 22.5, 45 or 90
        :                //         else:
          8              //           8
      ),                 //
      a,                 //       pass the copy of a[]
      -~c,               //       increment c
      a[i]++             //       increment a[i]
    )                    //     end of recursive call
  ) && o                 //   end of map(); return o[]

Charcoal, 60 bytes


Try it online! Link is to verbose version of code. Outputs using Charcoal's default array format i.e. each element on its own line. Explanation:


Calculate the possible angles using sets of up to 999 8° fittings and optionally one each of each of the other fittings, and see how closely they compare to the input.


Find the indices of the most accurate sets and calculate the fittings corresponding to each of those indices.


Output the last set with the fewest fittings.


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.