Added day 11, created monkey parser

This commit is contained in:
Rob Stoffelen 2022-12-15 17:39:15 +01:00
parent 43c01b9db7
commit 389a85869b
3 changed files with 58 additions and 10 deletions

View File

@ -3,18 +3,37 @@
using AdventOfCodeLibrary._2022; using AdventOfCodeLibrary._2022;
using AdventOfCodeLibrary.Shared; using AdventOfCodeLibrary.Shared;
string _demoData = @"R 4 string _demoData = @"Monkey 0:
U 4 Starting items: 79, 98
L 3 Operation: new = old * 19
D 1 Test: divisible by 23
R 4 If true: throw to monkey 2
D 1 If false: throw to monkey 3
L 5
R 2";
Answerable answerable = new Day09Part2(); Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1";
Answerable answerable = new Day11Part1();
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
//dataArray = Encoding.UTF8.GetBytes(_demoData); dataArray = Encoding.UTF8.GetBytes(_demoData);
Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}"); Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}");
Console.ReadKey(true); Console.ReadKey(true);

View File

@ -1,5 +1,6 @@
namespace AdventOfCodeLibrary._2022 namespace AdventOfCodeLibrary._2022
{ {
using AdventOfCodeLibrary._2022.Day_11;
using AdventOfCodeLibrary.Shared; using AdventOfCodeLibrary.Shared;
public class Day11Part1 : Answerable public class Day11Part1 : Answerable
@ -10,6 +11,32 @@
public override string GetAnswer(byte[] data) 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();
monkeys[monkeyIndex].MonkeyId = monkeyIndex;
}
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
{
string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
int monkeyId = Convert.ToInt32(monkey[0].Remove(monkey[0].Length - 1).Split(' ')[^1]);
monkeys[monkeyId].Items = monkey[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList();
string[] operation = monkey[2].Split(' ');
monkeys[monkeyIndex].SetOperation(operation[^2][0], operation[^1]);
monkeys[monkeyIndex].SetTestValue(Convert.ToInt32(monkey[3].Split(' ')[^1]));
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
}
return string.Empty; return string.Empty;
} }
} }

View File

@ -2,6 +2,8 @@
{ {
internal class Monkey internal class Monkey
{ {
internal int MonkeyId;
internal List<int> Items; internal List<int> Items;
private int TestDevision; private int TestDevision;