AdventOfCode/Advent Of Code Library/2022/Day 11/Day11Part2.cs
2022-12-15 22:56:10 +01:00

58 lines
2.1 KiB
C#

namespace AdventOfCodeLibrary._2022
{
using AdventOfCodeLibrary._2022.Day_11;
using AdventOfCodeLibrary.Shared;
public class Day11Part2 : Answerable
{
public override int Year { get; set; } = 2022;
public override int Day { get; set; } = 11;
public override int Part { get; set; } = 2;
public override string GetAnswer(byte[] data)
{
string[] monkeyString = GetAsString(data).Split(NewLine + NewLine);
Monkey[] monkeys = new Monkey[monkeyString.Length];
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{
monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine));
}
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{
string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
}
for (int round = 1; round <= 20; 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();
}
}
long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
return $"{mostInspects[0] * mostInspects[1]}";
}
}
}