From fd794bab1624cbb20bacc5ba8226d0badab966d8 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Thu, 15 Dec 2022 09:03:11 +0100 Subject: [PATCH] Add day 11 part 2 --- Days/Day11.cs | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/Days/Day11.cs b/Days/Day11.cs index a25c214..df4024d 100644 --- a/Days/Day11.cs +++ b/Days/Day11.cs @@ -6,15 +6,15 @@ namespace AdventOfCode.Days; public class Monkey { public int Number { get; } - public int InspectionCount { get; private set; } + public long InspectionCount { get; private set; } - private readonly Queue _items; - private readonly Func _operation; - private readonly int _testDivider; + private readonly Queue _items; + private readonly Func _operation; + private readonly long _testDivider; private readonly int _testFailMonkey; private readonly int _testSuccessMonkey; - public Monkey(int number, IEnumerable items, Func operation, int testDivider, int testFailMonkey, int testSuccessMonkey) + public Monkey(int number, IEnumerable items, Func operation, long testDivider, int testFailMonkey, int testSuccessMonkey) { Number = number; _operation = operation; @@ -23,15 +23,15 @@ public class Monkey _testSuccessMonkey = testSuccessMonkey; InspectionCount = 0; - _items = new Queue(items); + _items = new Queue(items); } - public void AddItem(int item) + public void AddItem(long item) { _items.Enqueue(item); } - public bool ThrowItem(IImmutableList monkeys) + public bool ThrowItem(IImmutableList monkeys, bool divideWorry = true) { // Return false if there's no item to throw if (_items.Count < 1) @@ -49,7 +49,14 @@ public class Monkey var newValue = _operation(item); // Divide worry level by 3 before test - newValue = newValue / 3; + if (divideWorry) + { + newValue = newValue / 3; + } + + // Even using this simplification, it's still needed to use longs because + // they sometime overflow before being simplified in part 2 + newValue = newValue % (2*3*5*7*11*13*17*19); // Test is a success if (newValue % _testDivider == 0) @@ -93,7 +100,23 @@ public class Day11 : Day public override void RunPart2() { - throw new NotImplementedException(); + const int roundsCount = 10_000; + + var monkeys = ParseMonkeys(); + + // Do 10 000 rounds + for (int round = 0; round < roundsCount; round++) + { + foreach (var monkey in monkeys) + { + // Throw items while there's some + while (monkey.ThrowItem(monkeys, false)) { } + } + } + + var topMonkeys = monkeys.OrderByDescending(m => m.InspectionCount).Take(2).ToList(); + + AnsiConsole.MarkupLine($"[green]Monkey business: [yellow]{topMonkeys[0].InspectionCount * topMonkeys[1].InspectionCount}[/][/]"); } private static IImmutableList ParseMonkeys() @@ -113,13 +136,13 @@ public class Day11 : Day // Monkey attributes var number = int.Parse(numberLine[numberLine.LastIndexOf(' ')..^1]); - var items = itemsLine[(itemsLine.IndexOf(':') + 2)..].Split(", ").Select(int.Parse); + var items = itemsLine[(itemsLine.IndexOf(':') + 2)..].Split(", ").Select(long.Parse); // Read operation operationLine = operationLine[(operationLine.IndexOf('=') + 2)..]; - int? rightOperand = operationLine.EndsWith("old") ? null : int.Parse(operationLine.Split('+', '*')[1].Trim()); + long? rightOperand = operationLine.EndsWith("old") ? null : long.Parse(operationLine.Split('+', '*')[1].Trim()); - Func operation = operationLine.Contains('+') switch + Func operation = operationLine.Contains('+') switch { true => rightOperand is null ? old => old + old @@ -129,7 +152,7 @@ public class Day11 : Day : old => old * (int)rightOperand }; - var testDivider = int.Parse(testDividerLine[testDividerLine.LastIndexOf(' ')..]); + var testDivider = long.Parse(testDividerLine[testDividerLine.LastIndexOf(' ')..]); var testSuccessMonkey = int.Parse(testSuccessLine[testSuccessLine.LastIndexOf(' ')..]); var testFailMonkey = int.Parse(testFailLine[testFailLine.LastIndexOf(' ')..]);