AdventOfCode2022/Kalender/day11.cs

138 lines
4.2 KiB
C#
Raw Normal View History

2022-12-11 13:27:55 +01:00
using System;
using System.Collections.Generic;
2022-12-11 21:18:45 +01:00
using System.Diagnostics;
2022-12-11 13:27:55 +01:00
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Kalender
{
internal class day11
{
2022-12-12 12:27:41 +01:00
static string file = "../../../files/day11.txt";
2022-12-11 13:27:55 +01:00
static string[] input = File.ReadAllLines(file);
static List<Monkey> monkeyList;
public static void part1()
{
2022-12-11 21:18:45 +01:00
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();
2022-12-12 07:19:00 +01:00
2022-12-11 21:18:45 +01:00
Console.WriteLine($"Part 1: {counterlist[0] * counterlist[1]}");
2022-12-11 13:27:55 +01:00
//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;
2022-12-11 21:18:45 +01:00
public string operationNumber;
2022-12-11 13:27:55 +01:00
public int divisiblNumber;
public int trueCase;
public int falseCase;
2022-12-11 21:18:45 +01:00
public int counter = 0;
2022-12-11 13:27:55 +01:00
2022-12-11 21:18:45 +01:00
public Monkey(string[] items, char op, string operationNumber, int divisiblNumber, int trueCase, int falseCase)
2022-12-11 13:27:55 +01:00
{
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));
}
}
2022-12-11 21:18:45 +01:00
public void test(List<Monkey> monkeyList)
2022-12-11 13:27:55 +01:00
{
2022-12-11 21:18:45 +01:00
while(startItems.Count > 0)
2022-12-11 13:27:55 +01:00
{
2022-12-11 21:18:45 +01:00
int number = startItems[0];
int opNumb = 0;
if(operationNumber == "old")
{
opNumb = number;
}
else
{
opNumb = int.Parse(operationNumber);
}
2022-12-11 13:27:55 +01:00
int newNumber = 0;
switch (op)
{
case '*':
2022-12-11 21:18:45 +01:00
newNumber = number * opNumb;
2022-12-11 13:27:55 +01:00
break;
case '+':
2022-12-11 21:18:45 +01:00
newNumber = number + opNumb;
2022-12-11 13:27:55 +01:00
break;
}
2022-12-11 21:18:45 +01:00
newNumber = newNumber / 3;
2022-12-11 13:27:55 +01:00
if(newNumber % divisiblNumber == 0)
{
2022-12-11 21:18:45 +01:00
monkeyList[trueCase].startItems.Add(newNumber);
startItems.Remove(number);
2022-12-11 13:27:55 +01:00
}
else
{
2022-12-11 21:18:45 +01:00
monkeyList[falseCase].startItems.Add(newNumber);
startItems.Remove(number);
2022-12-11 13:27:55 +01:00
}
2022-12-11 21:18:45 +01:00
counter++;
2022-12-11 13:27:55 +01:00
}
}
}
}