Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace ChrisMayor.codechallenge
- {
- class Program
- {
- static void Main(string[] args)
- {
- var program = new Program();
- program.CountChange();
- }
- private void CountChange()
- {
- var calculators = GetCalculators();
- var changeCalculations = ReadFile().Select(l => CountChangePerCustomer(int.Parse(l), calculators));
- var coincount = changeCalculations.ToList().SelectMany(c => c).Sum(c => c.CoinCount);
- Console.WriteLine($"Coins needed: {coincount}");
- }
- private IEnumerable<CoinCalculation> CountChangePerCustomer(int centinput, Dictionary<int, Func<int, CoinCalculation>> calculators)
- {
- var remainingvalue = centinput;
- var count = 0;
- while (remainingvalue > 0)
- {
- count++;
- var calculationPerCoin = calculators[count].Invoke(remainingvalue);
- remainingvalue = calculationPerCoin.Rest;
- yield return calculationPerCoin;
- }
- }
- private Dictionary<int, Func<int, CoinCalculation>> GetCalculators()
- {
- var dictionary = new Dictionary<int, Func<int, CoinCalculation>>();
- // key is order
- dictionary.Add(1, GetCalculator(50));
- dictionary.Add(2, GetCalculator(20));
- dictionary.Add(3, GetCalculator(10));
- dictionary.Add(4, GetCalculator(5));
- dictionary.Add(5, GetCalculator(2));
- dictionary.Add(6, GetCalculator(1));
- return dictionary;
- }
- public Func<int, CoinCalculation> GetCalculator(int coinValue)
- {
- Func<int, CoinCalculation> calculator = cents => new CoinCalculation { CoinValue = coinValue, CoinCount = cents / coinValue, Rest = cents % coinValue };
- return calculator;
- }
- public struct CoinCalculation
- {
- public int CoinValue { get; set; }
- public int CoinCount { get; set; }
- public int Rest { get; set; }
- }
- private string[] ReadFile()
- {
- return File.ReadAllLines(@"C:\Projekt\challange\OwR3bW9NuaZNNLtukKN5GeGPOnH2_cash-register_input (1).txt");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement