111 lines
2.8 KiB
C#
111 lines
2.8 KiB
C#
namespace AdventOfCodeLibrary._2022.Day_11
|
|
{
|
|
internal class Monkey
|
|
{
|
|
internal int MonkeyId;
|
|
|
|
internal List<int> Items;
|
|
|
|
private int TestDevision;
|
|
|
|
private OperationEnum Operation;
|
|
private int? OperationValue = null;
|
|
|
|
private Monkey ThrowTrueMonkey;
|
|
private Monkey ThrowFalseMonkey;
|
|
|
|
internal void DoTurn()
|
|
{
|
|
if (!Items.Any())
|
|
return;
|
|
|
|
for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++)
|
|
{
|
|
int item = Items[0];
|
|
item = GetNewWorry(item);
|
|
ThrowToMonkey(item);
|
|
}
|
|
|
|
Items.Clear();
|
|
}
|
|
|
|
internal int GetNewWorry(int oldWorry)
|
|
{
|
|
int secondValue = OperationValue ?? oldWorry;
|
|
|
|
switch (Operation)
|
|
{
|
|
case OperationEnum.Add:
|
|
secondValue = oldWorry + secondValue;
|
|
break;
|
|
case OperationEnum.Subtract:
|
|
secondValue = oldWorry - secondValue;
|
|
break;
|
|
case OperationEnum.Divide:
|
|
secondValue = oldWorry / secondValue;
|
|
break;
|
|
case OperationEnum.Multiply:
|
|
secondValue = oldWorry * secondValue;
|
|
break;
|
|
}
|
|
|
|
return secondValue / 3;
|
|
}
|
|
|
|
internal void ThrowToMonkey(int worry)
|
|
{
|
|
if (worry % TestDevision == 0)
|
|
{
|
|
ThrowTrueMonkey.Items.Add(worry);
|
|
}
|
|
else
|
|
{
|
|
ThrowFalseMonkey.Items.Add(worry);
|
|
}
|
|
|
|
}
|
|
|
|
internal void SetOperation(char operation, string value)
|
|
{
|
|
switch (operation)
|
|
{
|
|
case '+': Operation = OperationEnum.Add;
|
|
break;
|
|
case '-':
|
|
Operation = OperationEnum.Subtract;
|
|
break;
|
|
case '/':
|
|
Operation = OperationEnum.Divide;
|
|
break;
|
|
case '*':
|
|
Operation = OperationEnum.Multiply;
|
|
break;
|
|
}
|
|
|
|
if (!value.Equals("old"))
|
|
{
|
|
OperationValue = Convert.ToInt32(value);
|
|
}
|
|
}
|
|
|
|
internal void SetTestValue(int value)
|
|
{
|
|
TestDevision = value;
|
|
}
|
|
|
|
internal void SetThrowTargets(Monkey trueMonkey, Monkey falseMonkey)
|
|
{
|
|
ThrowTrueMonkey = trueMonkey;
|
|
ThrowFalseMonkey = falseMonkey;
|
|
}
|
|
}
|
|
|
|
internal enum OperationEnum
|
|
{
|
|
Add,
|
|
Subtract,
|
|
Multiply,
|
|
Divide
|
|
}
|
|
}
|