From 0e5a63d58a76b7972dfb70c9ebfd08a62e3c1604 Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Fri, 16 Dec 2022 10:52:57 +0100 Subject: [PATCH] Completed Day 11 p 2 --- Advend Of Code Runner/Program.cs | 2 +- .../2022/Day 11/Day11Part1.cs | 2 +- .../2022/Day 11/Day11Part2.cs | 47 +++++++++++------- Advent Of Code Library/2022/Day 11/Monkey.cs | 49 ++++++++++++------- 4 files changed, 63 insertions(+), 37 deletions(-) diff --git a/Advend Of Code Runner/Program.cs b/Advend Of Code Runner/Program.cs index 3c093bc..c7b515f 100644 --- a/Advend Of Code Runner/Program.cs +++ b/Advend Of Code Runner/Program.cs @@ -33,7 +33,7 @@ Monkey 3: Answerable answerable = new Day11Part2(); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); -dataArray = Encoding.UTF8.GetBytes(_demoData); +//dataArray = Encoding.UTF8.GetBytes(_demoData); Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}"); Console.ReadKey(true); \ No newline at end of file diff --git a/Advent Of Code Library/2022/Day 11/Day11Part1.cs b/Advent Of Code Library/2022/Day 11/Day11Part1.cs index 5ab32ef..2c73e92 100644 --- a/Advent Of Code Library/2022/Day 11/Day11Part1.cs +++ b/Advent Of Code Library/2022/Day 11/Day11Part1.cs @@ -37,7 +37,7 @@ } } - long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray(); + int[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray(); return $"{mostInspects[0] * mostInspects[1]}"; } diff --git a/Advent Of Code Library/2022/Day 11/Day11Part2.cs b/Advent Of Code Library/2022/Day 11/Day11Part2.cs index 7451709..d573675 100644 --- a/Advent Of Code Library/2022/Day 11/Day11Part2.cs +++ b/Advent Of Code Library/2022/Day 11/Day11Part2.cs @@ -12,12 +12,13 @@ public override string GetAnswer(byte[] data) { string[] monkeyString = GetAsString(data).Split(NewLine + NewLine); - + int lowestLcm = 1; Monkey[] monkeys = new Monkey[monkeyString.Length]; - + for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) { monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine)); + lowestLcm = CalculateLeastCommonMultiple(lowestLcm, monkeys[monkeyIndex].TestDevision); } for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) @@ -27,31 +28,43 @@ int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]); monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]); + + monkeys[monkeyIndex].lcm = lowestLcm; } - for (int round = 1; round <= 20; round++) + for (int round = 1; round <= 10_000; 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(); + monkeys[monkey].DoTurn(false); } } - long[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray(); + long[] mostInspects = monkeys.Select(m => (long)m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray(); return $"{mostInspects[0] * mostInspects[1]}"; } + + private int CalculateLeastCommonMultiple(int valueA, int valueB) + { + int num1, num2; + if (valueA > valueB) + { + num1 = valueA; num2 = valueB; + } + else + { + num1 = valueB; num2 = valueA; + } + + for (int i = 1; i <= num2; i++) + { + if ((num1 * i) % num2 == 0) + { + return i * num1; + } + } + return num2; + } } } diff --git a/Advent Of Code Library/2022/Day 11/Monkey.cs b/Advent Of Code Library/2022/Day 11/Monkey.cs index 854d63e..8da08e5 100644 --- a/Advent Of Code Library/2022/Day 11/Monkey.cs +++ b/Advent Of Code Library/2022/Day 11/Monkey.cs @@ -4,39 +4,46 @@ { internal int MonkeyId; - internal long TotalInspections = 0; + internal int TotalInspections = 0; - internal List Items; + internal List Items; - private int TestDevision; + internal int TestDevision; private int WorryDevision; + internal int lcm; private OperationEnum Operation; - private int? OperationValue = null; + + private long? OperationValue; private Monkey ThrowTrueMonkey; private Monkey ThrowFalseMonkey; - internal Monkey (int monkeyId, int worryDivirdNumber, string[] monkeyData) + /* + Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + */ + internal Monkey (int monkeyId, int worryDivideNumber, string[] monkeyData) { MonkeyId = monkeyId; - WorryDevision = worryDivirdNumber; + WorryDevision = worryDivideNumber; - Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList(); + Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt64(s.Trim())).ToList(); string[] operation = monkeyData[2].Split(' '); SetOperation(operation[^2][0], operation[^1]); SetTestValue(Convert.ToInt32(monkeyData[3].Split(' ')[^1])); } - internal void DoTurn() + internal void DoTurn(bool calcByWorryDivision = true) { - if (!Items.Any()) - return; - for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++) { - int item = Items[worryIndex]; - item = GetNewWorry(item); + long item = Items[worryIndex]; + item = GetNewWorry(item, calcByWorryDivision); ThrowToMonkey(item); TotalInspections++; } @@ -44,9 +51,9 @@ Items.Clear(); } - internal int GetNewWorry(int oldWorry) + internal long GetNewWorry(long oldWorry, bool calcByWorryDivision) { - int secondValue = OperationValue ?? oldWorry; + long secondValue = OperationValue ?? oldWorry; switch (Operation) { @@ -64,10 +71,16 @@ break; } - return secondValue / WorryDevision; + if (calcByWorryDivision) + { + return secondValue / WorryDevision; + } + + return secondValue % lcm; + } - internal void ThrowToMonkey(int worry) + internal void ThrowToMonkey(long worry) { if (worry % TestDevision == 0) { @@ -99,7 +112,7 @@ if (!value.Equals("old")) { - OperationValue = Convert.ToInt32(value); + OperationValue = Convert.ToInt64(value); } }