using System.Collections.ObjectModel;
Collection<Monkey> AllMonkeys = new Collection<Monkey>();
void InitAllMonkeys()
{
AllMonkeys.Clear();
AllMonkeys.Add(new Monkey(new UInt64[] { 71, 56, 50, 73 }, wl => wl * 11, wl => ((wl % 13) == 0) ? 1 : 7));
AllMonkeys.Add(new Monkey(new UInt64[] { 70, 89, 82 }, wl => wl + 1 , wl => ((wl % 7) == 0) ? 3 : 6));
AllMonkeys.Add(new Monkey(new UInt64[] { 52, 95 }, wl => wl * wl, wl => ((wl % 3) == 0) ? 5 : 4));
AllMonkeys.Add(new Monkey(new UInt64[] { 94, 64, 69, 87, 70 }, wl => wl + 2 , wl => ((wl % 19) == 0) ? 2 : 6));
AllMonkeys.Add(new Monkey(new UInt64[] { 98, 72, 98, 53, 97, 51 }, wl => wl + 6 , wl => ((wl % 5) == 0) ? 0 : 5));
AllMonkeys.Add(new Monkey(new UInt64[] { 79 }, wl => wl + 7 , wl => ((wl % 2) == 0) ? 7 : 0));
AllMonkeys.Add(new Monkey(new UInt64[] { 77, 55, 63, 93, 66, 90, 88, 71 }, wl => wl * 7 , wl => ((wl % 11) == 0) ? 2 : 4));
AllMonkeys.Add(new Monkey(new UInt64[] { 54, 97, 87, 70, 59, 82, 59 }, wl => wl + 8 , wl => ((wl % 17) == 0) ? 1 : 3));
}
InitAllMonkeys();
UInt64 MaxWorryLevel = 13 * 7 * 3 * 19 * 5 * 2 * 11 * 17;
for (int round = 1; round <= 20; ++round)
{
for (int monkeyID = 0; monkeyID < AllMonkeys.Count; ++monkeyID)
{ // Monkey turn
Monkey monkey = AllMonkeys[monkeyID];
while (monkey.Items.Count != 0)
{
++monkey.inspectionCount;
UInt64 wl = monkey.Items[0];
monkey.Items.RemoveAt(0);
wl = (monkey.operationOp(wl) / 3) % MaxWorryLevel;
AllMonkeys[monkey.nextMonkeyOp(wl)].Items.Add(wl);
}
}
}
Console.WriteLine("1 = " + AllMonkeys.Select( m => m.inspectionCount ).OrderBy( ic => ic ).Skip( AllMonkeys.Count-2 ).Aggregate( (a, b) => a*b ) ) ;
InitAllMonkeys();
for (int round = 1; round <= 10000; ++round)
{
for (int monkeyID = 0; monkeyID < AllMonkeys.Count; ++monkeyID)
{ // Monkey turn
Monkey monkey = AllMonkeys[monkeyID];
while (monkey.Items.Count != 0)
{
++monkey.inspectionCount;
UInt64 wl = monkey.Items[0];
monkey.Items.RemoveAt(0);
wl = monkey.operationOp(wl) % MaxWorryLevel;
AllMonkeys[monkey.nextMonkeyOp(wl)].Items.Add(wl);
}
}
}
Console.WriteLine("2 = " + AllMonkeys.Select(m => m.inspectionCount).OrderBy(ic => ic).Skip(AllMonkeys.Count - 2).Aggregate((a, b) => a * b));
public class Monkey
{
public UInt64 inspectionCount = 0;
public Collection<UInt64> Items = new Collection<UInt64>();
public delegate UInt64 OperationOp(UInt64 wl);
public OperationOp operationOp;
public delegate int NextMonkeyOp(UInt64 item);
public NextMonkeyOp nextMonkeyOp;
public Monkey(UInt64[] items, OperationOp op, NextMonkeyOp nm)
{
foreach (UInt64 i in items) Items.Add(i);
operationOp = op;
nextMonkeyOp = nm;
}
}