Find the optimal sliding door width
Perl, 190 180 154 133 128 117 bytes
includes +1 for -p
use POSIX;$m=1E4;for$q(80,100,120){($m,@z)=($p,$n,ceil$_/$n)if$m>($p=(150,200,220)[$x++]*($n=ceil$_/$q))}$_="@z $m"
Commented:
use POSIX; # for ceil()
$m = 1E4; # init min price to 10k
for $q (80,100,120) { # iterate widths
($m,@z) = ($p,$n, ceil $_/$n) # update min, output
if $m > ( #
$p = (150,200,220)[$x++] # grab price
* ( $n = ceil $_/$q ) # times nr of doors needed
)
}
$_="@z $m"
Save 11 bytes by inlining and splitting hash to two arrays
Save 5 bytes by using
-p
(thanks to @dev-null)Save 18 bytes by using POSIX::ceil and 3 more by using list syntax for hash (thanks to @msh210)
JavaScript (ES6), 101 bytes
t=>[[80,150],[100,200],[120,220]].map(([w,p])=>[n=-~(~-t/w),-~(~-t/n),n*p]).sort((a,b)=>a[2]-b[2])[0]
-~(~-a/b)
is the same as Math.ceil(a/b) in 31-bit integers.
PowerShell, 137 135 bytes
param($a)$j=9e9;60..120|%{if((($c=[math]::ceiling($a/$_))*($p=(220,(200,150)[$_-le80])[$_-le100]))-lt$j){$j=($k=$c)*$p;$i=$_}}
$k;$i;$j
Output is newline-separated.
We take input $a
, set our cost $j
to 9000000000
(a large number that's way more than we would ever need). Next, we loop from 60..120
with |%{...}
. Each iteration we calculate the $p
price of the current item with a pseudo-ternary statement, then calculate the $c
ceiling of $a/$_
. If the current total is smaller than the smallest total we've seen ($j
), save all these variables: $j
(the total), $k
(the number of doors required), and $i
(the door width), and continue the loop. Once the loop is finished, just output the best values.
Edit -- Saved two bytes by moving the $c
and $p
assignments into the if
conditional