-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6d3a19
commit a36305e
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>GoldSplitter</RootNamespace> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.3.32804.467 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gold Splitter", "Gold Splitter.csproj", "{407290BD-8743-46B1-902B-45AB61C55661}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{407290BD-8743-46B1-902B-45AB61C55661}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{407290BD-8743-46B1-902B-45AB61C55661}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{407290BD-8743-46B1-902B-45AB61C55661}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{407290BD-8743-46B1-902B-45AB61C55661}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {DDD05195-4059-4D20-B784-22830C1B8BC5} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
|
||
namespace GoldSplitter; | ||
using System; | ||
|
||
internal static class Program | ||
{ | ||
public static NamedAction[] actions = | ||
{ | ||
new ("Split", package => | ||
{ | ||
Console.WriteLine("Split with how many?"); | ||
int people = int.Parse(Console.ReadLine() ?? "0"); | ||
Console.WriteLine(package.Split(people)); | ||
}), | ||
new ("To All Copper", package => | ||
{ | ||
Console.WriteLine(package.ToAllCopper().Copper); | ||
}) | ||
}; | ||
static void Main() | ||
{ | ||
GoldPackage gold = GoldPackage.HumanInput(); | ||
Console.WriteLine("Choose an action:"); | ||
for (int i = 0; i < actions.Length; i++) | ||
Console.WriteLine($"{i + 1}. {actions[i].Name}"); | ||
actions[int.Parse(Console.ReadLine() ?? "0") - 1].Action.Invoke(gold); | ||
Console.ReadLine(); | ||
} | ||
} | ||
public record NamedAction(string Name, Action<GoldPackage> Action); | ||
public record GoldPackage(int Platinum, int Gold, int Silver, int Copper) | ||
{ | ||
public const int copperRepresentative = 1, silverRepresentative = 10, goldRepresentative = 100, platinumRepresentative = 1000; | ||
public static explicit operator int(GoldPackage package) | ||
=> package.ToAllCopper().Copper; | ||
public static GoldPackage HumanInput() | ||
{ | ||
Console.Write("Platinum: "); | ||
int plat = int.Parse(Console.ReadLine() ?? "0"); | ||
Console.Write("Gold: "); | ||
int gold = int.Parse(Console.ReadLine() ?? "0"); | ||
Console.Write("Silver: "); | ||
int silver = int.Parse(Console.ReadLine() ?? "0"); | ||
Console.Write("Copper: "); | ||
int copper = int.Parse(Console.ReadLine() ?? "0"); | ||
return new GoldPackage(plat, gold, silver, copper); | ||
} | ||
|
||
public GoldPackage Split(int people) | ||
{ | ||
int copper = ToAllCopper().Copper; | ||
copper /= people; | ||
return new GoldPackage(0, 0, 0, copper).DisperseToAppropriateCoinValues(); | ||
} | ||
public GoldPackage ToAllCopper() => new(0, 0, 0, (Copper * copperRepresentative) | ||
+ (Silver * silverRepresentative) + (Gold * goldRepresentative) | ||
+ (Platinum * platinumRepresentative)); | ||
public GoldPackage DisperseToAppropriateCoinValues() | ||
{ | ||
int copperTotal = ToAllCopper().Copper; | ||
int platinumOut = copperTotal / platinumRepresentative; | ||
copperTotal -= platinumOut * platinumRepresentative; | ||
int goldOut = copperTotal / goldRepresentative; | ||
copperTotal -= goldOut * goldRepresentative; | ||
int silverOut = copperTotal / silverRepresentative; | ||
copperTotal -= silverOut * silverRepresentative; | ||
int copperOut = copperTotal; | ||
return new GoldPackage(platinumOut, goldOut, silverOut, copperOut); | ||
} | ||
} |