Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace ChrisMayor.codechallenge
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var program = new Program();
  13.             program.CountChange();
  14.         }
  15.  
  16.  
  17.         private void CountChange()
  18.         {
  19.             var calculators = GetCalculators();
  20.             var changeCalculations = ReadFile().Select(l => CountChangePerCustomer(int.Parse(l), calculators));
  21.             var coincount = changeCalculations.ToList().SelectMany(c => c).Sum(c => c.CoinCount);
  22.             Console.WriteLine($"Coins needed: {coincount}");
  23.         }
  24.  
  25.         private IEnumerable<CoinCalculation> CountChangePerCustomer(int centinput, Dictionary<int, Func<int, CoinCalculation>> calculators)
  26.         {
  27.             var remainingvalue = centinput;
  28.             var count = 0;
  29.             while (remainingvalue > 0)
  30.             {
  31.                 count++;
  32.                 var calculationPerCoin = calculators[count].Invoke(remainingvalue);
  33.                 remainingvalue = calculationPerCoin.Rest;
  34.                 yield return calculationPerCoin;
  35.             }
  36.  
  37.         }
  38.  
  39.         private Dictionary<int, Func<int, CoinCalculation>> GetCalculators()
  40.         {
  41.             var dictionary = new Dictionary<int, Func<int, CoinCalculation>>();
  42.             // key is order
  43.             dictionary.Add(1, GetCalculator(50));
  44.             dictionary.Add(2, GetCalculator(20));
  45.             dictionary.Add(3, GetCalculator(10));
  46.             dictionary.Add(4, GetCalculator(5));
  47.             dictionary.Add(5, GetCalculator(2));
  48.             dictionary.Add(6, GetCalculator(1));
  49.             return dictionary;
  50.         }
  51.  
  52.         public Func<int, CoinCalculation> GetCalculator(int coinValue)
  53.         {
  54.             Func<int, CoinCalculation> calculator = cents => new CoinCalculation { CoinValue = coinValue, CoinCount = cents / coinValue, Rest = cents % coinValue };
  55.             return calculator;
  56.         }
  57.  
  58.         public struct CoinCalculation
  59.         {
  60.             public int CoinValue { get; set; }
  61.             public int CoinCount { get; set; }
  62.             public int Rest { get; set; }
  63.         }
  64.  
  65.  
  66.  
  67.         private string[] ReadFile()
  68.         {
  69.             return File.ReadAllLines(@"C:\Projekt\challange\OwR3bW9NuaZNNLtukKN5GeGPOnH2_cash-register_input (1).txt");
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement