136 lines
4.2 KiB
C#
136 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Kalender
|
|
{
|
|
internal class day11
|
|
{
|
|
static string file = "../../../Kalender/files/day11.txt";
|
|
static string[] input = File.ReadAllLines(file);
|
|
|
|
static List<Monkey> monkeyList;
|
|
public static void part1()
|
|
{
|
|
List<int> counterlist = new List<int>();
|
|
int index = 0;
|
|
monkeyList = new List<Monkey>();
|
|
while (index < input.Length)
|
|
{
|
|
var items = input[index +1].Substring(17).Split(',');
|
|
var op = input[index + 2][23];
|
|
var operationNumber = input[index + 2].Substring(25);
|
|
var divisionNumber = int.Parse(input[index + 3].Substring(20));
|
|
var trueCase = int.Parse(input[index + 4].Substring(input[4].Length - 2));
|
|
var falseCase = int.Parse(input[index + 5].Substring(input[5].Length - 2));
|
|
|
|
Monkey monkey = new Monkey(items,op,operationNumber,divisionNumber,trueCase,falseCase);
|
|
monkeyList.Add(monkey);
|
|
|
|
index += 7;
|
|
}
|
|
|
|
for (int i = 1; i <= 20; i++)
|
|
{
|
|
Console.WriteLine($"Round {i}");
|
|
foreach (Monkey monkey in monkeyList)
|
|
{
|
|
monkey.test(monkeyList);
|
|
}
|
|
foreach (Monkey monkey in monkeyList)
|
|
{
|
|
//Console.WriteLine(String.Join(",", monkey.startItems) + $" Counter: {monkey.counter}");
|
|
}
|
|
|
|
}
|
|
foreach (Monkey monkey in monkeyList)
|
|
{
|
|
Console.WriteLine($" Counter: {monkey.counter}");
|
|
counterlist.Add(monkey.counter);
|
|
}
|
|
counterlist.Sort();
|
|
counterlist.Reverse();
|
|
Console.WriteLine($"Part 1: {counterlist[0] * counterlist[1]}");
|
|
|
|
|
|
|
|
//Console.WriteLine($"op: {monkey.op}, opnumber: {monkey.operationNumber}, divisibleNumber: {monkey.divisiblNumber}, true:{monkey.trueCase}, false: {monkey.falseCase}");
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
class Monkey
|
|
{
|
|
public List<int> startItems;
|
|
public char op;
|
|
public string operationNumber;
|
|
public int divisiblNumber;
|
|
public int trueCase;
|
|
public int falseCase;
|
|
public int counter = 0;
|
|
|
|
public Monkey(string[] items, char op, string operationNumber, int divisiblNumber, int trueCase, int falseCase)
|
|
{
|
|
startItems = new List<int>();
|
|
this.op = op;
|
|
this.operationNumber = operationNumber;
|
|
this.divisiblNumber = divisiblNumber;
|
|
this.trueCase = trueCase;
|
|
this.falseCase = falseCase;
|
|
|
|
foreach (var item in items)
|
|
{
|
|
startItems.Add(int.Parse(item));
|
|
}
|
|
|
|
}
|
|
|
|
public void test(List<Monkey> monkeyList)
|
|
{
|
|
|
|
while(startItems.Count > 0)
|
|
{
|
|
int number = startItems[0];
|
|
int opNumb = 0;
|
|
if(operationNumber == "old")
|
|
{
|
|
opNumb = number;
|
|
}
|
|
else
|
|
{
|
|
opNumb = int.Parse(operationNumber);
|
|
}
|
|
int newNumber = 0;
|
|
switch (op)
|
|
{
|
|
case '*':
|
|
newNumber = number * opNumb;
|
|
break;
|
|
case '+':
|
|
newNumber = number + opNumb;
|
|
break;
|
|
}
|
|
newNumber = newNumber / 3;
|
|
if(newNumber % divisiblNumber == 0)
|
|
{
|
|
monkeyList[trueCase].startItems.Add(newNumber);
|
|
startItems.Remove(number);
|
|
}
|
|
else
|
|
{
|
|
monkeyList[falseCase].startItems.Add(newNumber);
|
|
startItems.Remove(number);
|
|
}
|
|
counter++;
|
|
}
|
|
}
|
|
}
|
|
}
|