From 43c01b9db79fe2501ba0a54f747133baea63cd69 Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Thu, 15 Dec 2022 10:26:47 +0100 Subject: [PATCH] added day 11 --- .../2022/Day 11/Day11Part1.cs | 16 ++ .../2022/Day 11/Day11Part2.cs | 16 ++ Advent Of Code Library/2022/Day 11/Monkey.cs | 108 ++++++++++++++ .../2022/Day 11/day-11-input.txt | 137 ++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 Advent Of Code Library/2022/Day 11/Day11Part1.cs create mode 100644 Advent Of Code Library/2022/Day 11/Day11Part2.cs create mode 100644 Advent Of Code Library/2022/Day 11/Monkey.cs create mode 100644 Advent Of Code Library/2022/Day 11/day-11-input.txt diff --git a/Advent Of Code Library/2022/Day 11/Day11Part1.cs b/Advent Of Code Library/2022/Day 11/Day11Part1.cs new file mode 100644 index 0000000..6ade840 --- /dev/null +++ b/Advent Of Code Library/2022/Day 11/Day11Part1.cs @@ -0,0 +1,16 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day11Part1 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 11; + 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 11/Day11Part2.cs b/Advent Of Code Library/2022/Day 11/Day11Part2.cs new file mode 100644 index 0000000..13f5cc0 --- /dev/null +++ b/Advent Of Code Library/2022/Day 11/Day11Part2.cs @@ -0,0 +1,16 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day11Part2 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 11; + 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 11/Monkey.cs b/Advent Of Code Library/2022/Day 11/Monkey.cs new file mode 100644 index 0000000..8d1f6db --- /dev/null +++ b/Advent Of Code Library/2022/Day 11/Monkey.cs @@ -0,0 +1,108 @@ +namespace AdventOfCodeLibrary._2022.Day_11 +{ + internal class Monkey + { + internal List Items; + + private int TestDevision; + + private OperationEnum Operation; + private int? OperationValue = null; + + private Monkey ThrowTrueMonkey; + private Monkey ThrowFalseMonkey; + + internal void DoTurn() + { + if (!Items.Any()) + return; + + for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++) + { + int item = Items[0]; + item = GetNewWorry(item); + ThrowToMonkey(item); + } + + Items.Clear(); + } + + internal int GetNewWorry(int oldWorry) + { + int secondValue = OperationValue ?? oldWorry; + + switch (Operation) + { + case OperationEnum.Add: + secondValue = oldWorry + secondValue; + break; + case OperationEnum.Subtract: + secondValue = oldWorry - secondValue; + break; + case OperationEnum.Divide: + secondValue = oldWorry / secondValue; + break; + case OperationEnum.Multiply: + secondValue = oldWorry * secondValue; + break; + } + + return secondValue / 3; + } + + internal void ThrowToMonkey(int worry) + { + if (worry % TestDevision == 0) + { + ThrowTrueMonkey.Items.Add(worry); + } + else + { + ThrowFalseMonkey.Items.Add(worry); + } + + } + + internal void SetOperation(char operation, string value) + { + switch (operation) + { + case '+': Operation = OperationEnum.Add; + break; + case '-': + Operation = OperationEnum.Subtract; + break; + case '/': + Operation = OperationEnum.Divide; + break; + case '*': + Operation = OperationEnum.Multiply; + break; + } + + if (!value.Equals("old")) + { + OperationValue = Convert.ToInt32(value); + } + } + + internal void SetTestValue(int value) + { + TestDevision = value; + } + + internal void SetThrowTargets(Monkey trueMonkey, Monkey falseMonkey) + { + ThrowTrueMonkey = trueMonkey; + ThrowFalseMonkey = falseMonkey; + } + } + + internal enum OperationEnum + { + Add, + Subtract, + Multiply, + Divide + } +} 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 new file mode 100644 index 0000000..033e54f --- /dev/null +++ b/Advent Of Code Library/2022/Day 11/day-11-input.txt @@ -0,0 +1,137 @@ +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