Skip to content

Instantly share code, notes, and snippets.

@JonDouglas
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonDouglas/8829213 to your computer and use it in GitHub Desktop.
Save JonDouglas/8829213 to your computer and use it in GitHub Desktop.
MakeChocolate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MakeChocolate
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(MakeChocolate(4, 1, 9).ToString()); //SHOULD RETURN 4
Console.WriteLine(MakeChocolate(4, 1, 10).ToString()); //SHOULD RETURN -1
Console.WriteLine(MakeChocolate(4, 1, 7).ToString()); //SHOULD RETURN 2
Console.WriteLine(MakeChocolate(6, 2, 7).ToString()); //SHOULD RETURN 2
}
public static int MakeChocolate(int small, int big, int goal)
{
//5 kilo big bar
//1 kilo small bar
int bigBar = 5;
int smallBar = 1;
//Check what our goal is and get the number of big bars
int bigGoal = goal / bigBar;
int smallGoal = 0;
//If our goal is bigger than our big we need to get the difference and times by the bigBar kilo(5)
if (bigGoal > big)
{
smallGoal = (bigGoal - big) * bigBar;
}
//Our goal is however many are remaining from the bigBar (5)
smallGoal += goal % bigBar;
//It's not possible
if (smallGoal > small)
{
return -1;
}
return smallGoal;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment