Skip to content

Instantly share code, notes, and snippets.

@oshea00
Created July 8, 2020 16:16
Show Gist options
  • Save oshea00/6aa3c82682793b6ae3521a10bde634c3 to your computer and use it in GitHub Desktop.
Save oshea00/6aa3c82682793b6ae3521a10bde634c3 to your computer and use it in GitHub Desktop.
void Main()
{
var n = new NuggetSaver(7,11);
(n.OrderNuggets(20) == (3, 0)).Dump();
(n.OrderNuggets(100) == (8, 4)).Dump();
(n.OrderNuggets(-20) == (0, 0)).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 order1, int order2) OrderNuggets(int count) {
int minWasted = int.MaxValue;
int order1 = 0;
int order2 = 0;
int maxOrder1 = (count % Size1) > 0 ? count / Size1 + 1 : count / Size1;
int maxOrder2 = (count % Size2) > 0 ? count / Size2 + 1 : count / Size2;
for (int o1 = 0; o1 <= maxOrder1; o1++) {
for (int o2 = 0; o2 <=maxOrder2; o2++) {
int waste = count - (o1 * Size1) - (o2 * Size2);
if (waste <= 0) {
int absWaste = Math.Abs(waste);
if (absWaste < minWasted)
{
$"{o1}, {o2} = {absWaste}".Dump();
minWasted = absWaste;
order1 = o1;
order2 = o2;
}
}
}
}
return (order1, order2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment