-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChocolateDistribution.20230911.cs
68 lines (55 loc) · 1.64 KB
/
ChocolateDistribution.20230911.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var chocolatePackages = new int[] { 5, 7, 23, 30, 2 };
var childCount = 4;
var chocolateDistribution = GetChocolateDistribution(chocolatePackages, childCount);
Console.WriteLine($"Chocolate distribution results: {String.Join(", ", chocolateDistribution)}");
chocolateDistribution.Dump();
}
// Given chocolates, distribute evenly to children.
public static int[] GetChocolateDistribution(int[] chocolatePackages, int childCount)
{
if(childCount < 1)
{
throw new Exception("No children to distribute chocolates to.");
}
var childrenHashMap = GetEmptyChildrenHashMap(childCount);
// Loop through packages
foreach(var chocolates in chocolatePackages)
{
// First child gets chocolates by default
var currentChild = 1;
// Loop through chocolates
for(var x = 0; x < chocolates; x++)
{
// currentChild has exceeded childCount, reset back to 1;
if(currentChild > childCount)
{
currentChild = 1;
}
if(childrenHashMap.ContainsKey(currentChild))
{
// Increase amount of chocolates that kid has.
childrenHashMap[currentChild] = childrenHashMap[currentChild] + 1;
}
currentChild++;
}
}
return childrenHashMap.Values.Select(v => v).ToArray();
}
private static Dictionary<int, int> GetEmptyChildrenHashMap(int numberOfChildren)
{
var childrenHashMap = new Dictionary<int, int>();
for(var y = 1; y <= numberOfChildren; y++)
{
childrenHashMap.Add(y, 0);
}
return childrenHashMap;
}
}