From ee6da383ca8c22a4dee85a3925449e763410cdbb Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 15 Dec 2022 22:56:10 +0100 Subject: [PATCH] worked on day 11 p 2 --- Advend Of Code Runner/Program.cs | 2 +- .../2022/Day 09/RopeWalker.cs | 3 - .../2022/Day 11/Day11Part1.cs | 22 +- .../2022/Day 11/Day11Part2.cs | 43 +++- Advent Of Code Library/2022/Day 11/Monkey.cs | 19 +- .../2022/Day 11/day-11-input.txt | 192 +++++------------- .../2022/Day 12/Day12Part1.cs | 16 ++ .../2022/Day 12/Day12Part2.cs | 16 ++ .../2022/Day 12/day-12-input.txt | 55 +++++ Advent Of Code Library/Shared/Answerable.cs | 6 +- 10 files changed, 217 insertions(+), 157 deletions(-) create mode 100644 Advent Of Code Library/2022/Day 12/Day12Part1.cs create mode 100644 Advent Of Code Library/2022/Day 12/Day12Part2.cs create mode 100644 Advent Of Code Library/2022/Day 12/day-12-input.txt diff --git a/Advend Of Code Runner/Program.cs b/Advend Of Code Runner/Program.cs index cfc3654..3c093bc 100644 --- a/Advend Of Code Runner/Program.cs +++ b/Advend Of Code Runner/Program.cs @@ -31,7 +31,7 @@ Monkey 3: If true: throw to monkey 0 If false: throw to monkey 1"; -Answerable answerable = new Day11Part1(); +Answerable answerable = new Day11Part2(); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); dataArray = Encoding.UTF8.GetBytes(_demoData); diff --git a/Advent Of Code Library/2022/Day 09/RopeWalker.cs b/Advent Of Code Library/2022/Day 09/RopeWalker.cs index 98f2c13..bbcc2a3 100644 --- a/Advent Of Code Library/2022/Day 09/RopeWalker.cs +++ b/Advent Of Code Library/2022/Day 09/RopeWalker.cs @@ -15,9 +15,6 @@ internal class RopeWalker { - private readonly VirtualPoint HeadLocation = new(); - private VirtualPoint TailLocation = new(); - private List TailVistedLocations = new(); internal int ProcessMovement(string[] movements, int ropeLength) diff --git a/Advent Of Code Library/2022/Day 11/Day11Part1.cs b/Advent Of Code Library/2022/Day 11/Day11Part1.cs index f3a8aab..5ab32ef 100644 --- a/Advent Of Code Library/2022/Day 11/Day11Part1.cs +++ b/Advent Of Code Library/2022/Day 11/Day11Part1.cs @@ -17,27 +17,29 @@ for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) { - monkeys[monkeyIndex] = new(); - monkeys[monkeyIndex].MonkeyId = monkeyIndex; + monkeys[monkeyIndex] = new(monkeyIndex, 3, monkeyString[monkeyIndex].Split(NewLine)); } 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; + for(int round = 0; round < 20; round++) + { + for (int monkey = 0; monkey < monkeys.Length; monkey++) + { + monkeys[monkey].DoTurn(); + } + } + + long[] 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 13f5cc0..7451709 100644 --- a/Advent Of Code Library/2022/Day 11/Day11Part2.cs +++ b/Advent Of Code Library/2022/Day 11/Day11Part2.cs @@ -1,5 +1,6 @@ namespace AdventOfCodeLibrary._2022 { + using AdventOfCodeLibrary._2022.Day_11; using AdventOfCodeLibrary.Shared; public class Day11Part2 : Answerable @@ -10,7 +11,47 @@ public override string GetAnswer(byte[] data) { - return string.Empty; + string[] monkeyString = GetAsString(data).Split(NewLine + NewLine); + + Monkey[] monkeys = new Monkey[monkeyString.Length]; + + for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) + { + monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine)); + } + + for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++) + { + string[] monkey = monkeyString[monkeyIndex].Split(NewLine); + int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]); + int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]); + + monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]); + } + + for (int round = 1; round <= 20; 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(); + } + } + + long[] 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/Monkey.cs b/Advent Of Code Library/2022/Day 11/Monkey.cs index a9df7a9..854d63e 100644 --- a/Advent Of Code Library/2022/Day 11/Monkey.cs +++ b/Advent Of Code Library/2022/Day 11/Monkey.cs @@ -4,9 +4,12 @@ { internal int MonkeyId; + internal long TotalInspections = 0; + internal List Items; private int TestDevision; + private int WorryDevision; private OperationEnum Operation; private int? OperationValue = null; @@ -14,6 +17,17 @@ private Monkey ThrowTrueMonkey; private Monkey ThrowFalseMonkey; + internal Monkey (int monkeyId, int worryDivirdNumber, string[] monkeyData) + { + MonkeyId = monkeyId; + WorryDevision = worryDivirdNumber; + + Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt32(s.Trim())).ToList(); + string[] operation = monkeyData[2].Split(' '); + SetOperation(operation[^2][0], operation[^1]); + SetTestValue(Convert.ToInt32(monkeyData[3].Split(' ')[^1])); + } + internal void DoTurn() { if (!Items.Any()) @@ -21,9 +35,10 @@ for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++) { - int item = Items[0]; + int item = Items[worryIndex]; item = GetNewWorry(item); ThrowToMonkey(item); + TotalInspections++; } Items.Clear(); @@ -49,7 +64,7 @@ break; } - return secondValue / 3; + return secondValue / WorryDevision; } internal void ThrowToMonkey(int worry) diff --git a/Advent Of Code Library/2022/Day 11/day-11-input.txt b/Advent Of Code Library/2022/Day 11/day-11-input.txt index 033e54f..4961e11 100644 --- a/Advent Of Code Library/2022/Day 11/day-11-input.txt +++ b/Advent Of Code Library/2022/Day 11/day-11-input.txt @@ -1,137 +1,55 @@ -addx 1 -noop -addx 2 -addx 5 -addx 3 -noop -addx -1 -addx 5 -noop -noop -addx 5 -noop -addx 3 -noop -addx 6 -addx -4 -noop -noop -addx 5 -noop -addx -32 -addx 35 -addx 5 -addx -31 -addx 7 -addx -13 -addx 2 -addx 2 -addx 5 -addx 6 -addx -5 -addx 2 -addx 5 -addx 2 -addx 2 -addx -17 -addx 18 -addx 5 -addx 2 -addx -30 -addx 31 -addx 2 -addx 2 -addx -32 -addx -1 -addx 10 -addx -8 -noop -noop -addx 6 -addx 16 -noop -addx -11 -addx 3 -addx -2 -addx 3 -noop -addx 6 -noop -addx -2 -noop -addx 7 -addx 3 -addx -2 -addx 4 -addx -20 -noop -addx -14 -addx -2 -addx 6 -addx 2 -addx 3 -noop -addx 2 -addx -1 -addx 4 -noop -addx 5 -noop -addx 2 -addx 3 -addx -2 -addx 3 -noop -addx 4 -noop -addx 5 -noop -addx 2 -addx -24 -addx -15 -addx 17 -addx -10 -addx 2 -addx -5 -addx 6 -noop -addx 3 -addx 2 -addx 2 -noop -addx 3 -addx 2 -addx 5 -addx -2 -addx 3 -addx 2 -addx 2 -addx 5 -addx 2 -addx -18 -addx -19 -noop -addx 1 -addx 2 -addx 5 -addx 3 -noop -noop -noop -noop -addx 2 -addx 5 -addx 2 -addx 3 -noop -addx -8 -addx 11 -noop -noop -addx 2 -addx 5 -addx 2 -addx 3 -noop -addx -34 -noop \ No newline at end of file +Monkey 0: + Starting items: 66, 71, 94 + Operation: new = old * 5 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 4 + +Monkey 1: + Starting items: 70 + Operation: new = old + 6 + Test: divisible by 17 + If true: throw to monkey 3 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 62, 68, 56, 65, 94, 78 + Operation: new = old + 5 + Test: divisible by 2 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 89, 94, 94, 67 + Operation: new = old + 2 + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 0 + +Monkey 4: + Starting items: 71, 61, 73, 65, 98, 98, 63 + Operation: new = old * 7 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 5: + Starting items: 55, 62, 68, 61, 60 + Operation: new = old + 7 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 1 + +Monkey 6: + Starting items: 93, 91, 69, 64, 72, 89, 50, 71 + Operation: new = old + 1 + Test: divisible by 13 + If true: throw to monkey 5 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 76, 50 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 6 \ No newline at end of file diff --git a/Advent Of Code Library/2022/Day 12/Day12Part1.cs b/Advent Of Code Library/2022/Day 12/Day12Part1.cs new file mode 100644 index 0000000..d65b250 --- /dev/null +++ b/Advent Of Code Library/2022/Day 12/Day12Part1.cs @@ -0,0 +1,16 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day12Part1 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 12; + public override int Part { get; set; } = 1; + + public override string GetAnswer(byte[] data) + { + return string.Empty; + } + } +} diff --git a/Advent Of Code Library/2022/Day 12/Day12Part2.cs b/Advent Of Code Library/2022/Day 12/Day12Part2.cs new file mode 100644 index 0000000..ed68cd9 --- /dev/null +++ b/Advent Of Code Library/2022/Day 12/Day12Part2.cs @@ -0,0 +1,16 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day12Part2 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 12; + public override int Part { get; set; } = 2; + + public override string GetAnswer(byte[] data) + { + return string.Empty; + } + } +} diff --git a/Advent Of Code Library/2022/Day 12/day-12-input.txt b/Advent Of Code Library/2022/Day 12/day-12-input.txt new file mode 100644 index 0000000..bc9319d --- /dev/null +++ b/Advent Of Code Library/2022/Day 12/day-12-input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 66, 71, 94 + Operation: new = old * 5 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 4 + +Monkey 1: + Starting items: 70 + Operation: new = old + 6 + Test: divisible by 17 + If true: throw to monkey 3 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 62, 68, 56, 65, 94, 78 + Operation: new = old + 5 + Test: divisible by 2 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 89, 94, 94, 67 + Operation: new = old + 2 + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 0 + +Monkey 4: + Starting items: 71, 61, 73, 65, 98, 98, 63 + Operation: new = old * 7 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 5: + Starting items: 55, 62, 68, 61, 60 + Operation: new = old + 7 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 1 + +Monkey 6: + Starting items: 93, 91, 69, 64, 72, 89, 50, 71 + Operation: new = old + 1 + Test: divisible by 13 + If true: throw to monkey 5 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 76, 50 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 6 \ No newline at end of file diff --git a/Advent Of Code Library/Shared/Answerable.cs b/Advent Of Code Library/Shared/Answerable.cs index 7d5d836..6702efd 100644 --- a/Advent Of Code Library/Shared/Answerable.cs +++ b/Advent Of Code Library/Shared/Answerable.cs @@ -14,13 +14,13 @@ namespace AdventOfCodeLibrary.Shared public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt"; - protected static string NewLine => "\n"; + protected static string NewLine => Environment.NewLine; public abstract string GetAnswer(byte[] data); - internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes); + internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes).ReplaceLineEndings(); - internal static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(NewLine).Select(StripControlChars).ToArray(); + internal static string[] GetAsStringArray(byte[] bytes) => GetAsString(bytes).Split(NewLine).ToArray(); private static string StripControlChars(string s) {