Created
July 8, 2020 17:34
-
-
Save oshea00/58d92438a6a1c6e93f2786efc566c5d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var n = new NuggetSaver(7,11); | |
(n.BestOrder(20) == (3, 0)).Dump(); | |
(n.BestOrder(100) == (8, 4)).Dump(); | |
(n.BestOrder(-20) == (0, 0)).Dump(); | |
(n.BestOrder(1000000) == (142854, 2)).Dump(); | |
} | |
class NuggetSaver | |
{ | |
public int Size1 { get; private set; } | |
public int Size2 { get; private set; } | |
public NuggetSaver(int size1, int size2) | |
{ | |
Size1 = size1; | |
Size2 = size2; | |
} | |
public (int o1, int o2) BestOrder(int count) | |
{ | |
var check = ReducedWaste(count); | |
var total = check.o1 * Size1 + check.o2 * Size2; | |
if (total == count || check == (0, 0)) | |
return check; | |
var altTotal1 = (check.o1 - 1) * Size1 + (check.o2 + 1) * Size2; | |
var altTotal2 = (check.o1 + 1) * Size1 + (check.o2 - 1) * Size2; | |
if (altTotal1 <= altTotal2) | |
return (check.o1 - 1, check.o2 + 1); | |
else | |
return (check.o1 + 1, check.o2 - 1); | |
} | |
(int o1, int o2) ReducedWaste(int count) | |
{ | |
int o1 = 0; | |
int o2 = 0; | |
int remaining = count; | |
while (remaining > 0) | |
{ | |
var order = LeastWaste(remaining); | |
if (order.choice1 == 1) | |
{ | |
o1++; | |
remaining -= Size1; | |
} | |
else | |
{ | |
o2++; | |
remaining -= Size2; | |
} | |
} | |
return (o1, o2); | |
} | |
(int choice1, int choice2) LeastWaste(int count) | |
{ | |
if ((count % Size1) <= (count % Size2)) | |
return (1, 0); | |
return (0, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment