Completed Day 11 p 2
This commit is contained in:
parent
ee6da383ca
commit
0e5a63d58a
@ -33,7 +33,7 @@ Monkey 3:
|
||||
|
||||
Answerable answerable = new Day11Part2();
|
||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||
dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||
|
||||
Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}");
|
||||
Console.ReadKey(true);
|
||||
@ -37,7 +37,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||
int[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||
|
||||
return $"{mostInspects[0] * mostInspects[1]}";
|
||||
}
|
||||
|
||||
@ -12,12 +12,13 @@
|
||||
public override string GetAnswer(byte[] data)
|
||||
{
|
||||
string[] monkeyString = GetAsString(data).Split(NewLine + NewLine);
|
||||
|
||||
int lowestLcm = 1;
|
||||
Monkey[] monkeys = new Monkey[monkeyString.Length];
|
||||
|
||||
|
||||
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||
{
|
||||
monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine));
|
||||
lowestLcm = CalculateLeastCommonMultiple(lowestLcm, monkeys[monkeyIndex].TestDevision);
|
||||
}
|
||||
|
||||
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||
@ -27,31 +28,43 @@
|
||||
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
|
||||
|
||||
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
|
||||
|
||||
monkeys[monkeyIndex].lcm = lowestLcm;
|
||||
}
|
||||
|
||||
for (int round = 1; round <= 20; round++)
|
||||
for (int round = 1; round <= 10_000; round++)
|
||||
{
|
||||
for (int monkey = 0; monkey < monkeys.Length; monkey++)
|
||||
{
|
||||
monkeys[monkey].DoTurn();
|
||||
}
|
||||
|
||||
//if (round == 1 || round == 20 || round == 1000 || round == 2000 || round == 3000 || round == 4000 || round == 5000)
|
||||
{
|
||||
Console.WriteLine($"== After round {round} ==");
|
||||
|
||||
foreach (Monkey monkey in monkeys.OrderBy(m => m.MonkeyId))
|
||||
{
|
||||
Console.WriteLine($"Monkey {monkey.MonkeyId} inspected items {monkey.TotalInspections} times.");
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
monkeys[monkey].DoTurn(false);
|
||||
}
|
||||
}
|
||||
|
||||
long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||
long[] mostInspects = monkeys.Select(m => (long)m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||
|
||||
return $"{mostInspects[0] * mostInspects[1]}";
|
||||
}
|
||||
|
||||
private int CalculateLeastCommonMultiple(int valueA, int valueB)
|
||||
{
|
||||
int num1, num2;
|
||||
if (valueA > valueB)
|
||||
{
|
||||
num1 = valueA; num2 = valueB;
|
||||
}
|
||||
else
|
||||
{
|
||||
num1 = valueB; num2 = valueA;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= num2; i++)
|
||||
{
|
||||
if ((num1 * i) % num2 == 0)
|
||||
{
|
||||
return i * num1;
|
||||
}
|
||||
}
|
||||
return num2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,39 +4,46 @@
|
||||
{
|
||||
internal int MonkeyId;
|
||||
|
||||
internal long TotalInspections = 0;
|
||||
internal int TotalInspections = 0;
|
||||
|
||||
internal List<int> Items;
|
||||
internal List<long> Items;
|
||||
|
||||
private int TestDevision;
|
||||
internal int TestDevision;
|
||||
private int WorryDevision;
|
||||
internal int lcm;
|
||||
|
||||
private OperationEnum Operation;
|
||||
private int? OperationValue = null;
|
||||
|
||||
private long? OperationValue;
|
||||
|
||||
private Monkey ThrowTrueMonkey;
|
||||
private Monkey ThrowFalseMonkey;
|
||||
|
||||
internal Monkey (int monkeyId, int worryDivirdNumber, string[] monkeyData)
|
||||
/*
|
||||
Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
*/
|
||||
internal Monkey (int monkeyId, int worryDivideNumber, string[] monkeyData)
|
||||
{
|
||||
MonkeyId = monkeyId;
|
||||
WorryDevision = worryDivirdNumber;
|
||||
WorryDevision = worryDivideNumber;
|
||||
|
||||
Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList();
|
||||
Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt64(s.Trim())).ToList();
|
||||
string[] operation = monkeyData[2].Split(' ');
|
||||
SetOperation(operation[^2][0], operation[^1]);
|
||||
SetTestValue(Convert.ToInt32(monkeyData[3].Split(' ')[^1]));
|
||||
}
|
||||
|
||||
internal void DoTurn()
|
||||
internal void DoTurn(bool calcByWorryDivision = true)
|
||||
{
|
||||
if (!Items.Any())
|
||||
return;
|
||||
|
||||
for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++)
|
||||
{
|
||||
int item = Items[worryIndex];
|
||||
item = GetNewWorry(item);
|
||||
long item = Items[worryIndex];
|
||||
item = GetNewWorry(item, calcByWorryDivision);
|
||||
ThrowToMonkey(item);
|
||||
TotalInspections++;
|
||||
}
|
||||
@ -44,9 +51,9 @@
|
||||
Items.Clear();
|
||||
}
|
||||
|
||||
internal int GetNewWorry(int oldWorry)
|
||||
internal long GetNewWorry(long oldWorry, bool calcByWorryDivision)
|
||||
{
|
||||
int secondValue = OperationValue ?? oldWorry;
|
||||
long secondValue = OperationValue ?? oldWorry;
|
||||
|
||||
switch (Operation)
|
||||
{
|
||||
@ -64,10 +71,16 @@
|
||||
break;
|
||||
}
|
||||
|
||||
return secondValue / WorryDevision;
|
||||
if (calcByWorryDivision)
|
||||
{
|
||||
return secondValue / WorryDevision;
|
||||
}
|
||||
|
||||
return secondValue % lcm;
|
||||
|
||||
}
|
||||
|
||||
internal void ThrowToMonkey(int worry)
|
||||
internal void ThrowToMonkey(long worry)
|
||||
{
|
||||
if (worry % TestDevision == 0)
|
||||
{
|
||||
@ -99,7 +112,7 @@
|
||||
|
||||
if (!value.Equals("old"))
|
||||
{
|
||||
OperationValue = Convert.ToInt32(value);
|
||||
OperationValue = Convert.ToInt64(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user