challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

challenge2.lua (486B)


      1 #!/usr/bin/lua
      2 
      3 denoms = {98, 42, 23, 17, 3, 2}
      4 
      5 opt = {}
      6 
      7 function min (n)
      8   opt[n] = math.huge
      9   for i,denom in ipairs(denoms) do
     10     if denom == n then
     11       opt[n] = 1
     12       return
     13     end
     14     if (denom < n) and opt[n - denom] then
     15       soln = opt[n - denom] + 1
     16       if soln < opt[n] then opt[n] = soln end
     17     end
     18   end
     19 end
     20 
     21 for i=2,2349 do
     22   min(i)
     23 end
     24 
     25 answer = 1
     26 
     27 for i,points in ipairs({2349, 2102, 2001, 1747}) do
     28   t = opt[points]
     29   answer = answer * t
     30 end
     31 
     32 print(answer)