From cf2a2e1f39bb8773536d25679f38671a3acc3a83 Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Thu, 15 Dec 2022 09:07:39 +0100 Subject: [PATCH] Added 10 10, still got to complete day 9 p 2 --- Advend Of Code Runner/Program.cs | 15 +- .../2022/Day 09/RopeWalker.cs | 19 ++- .../2022/Day 10/Day10Part1.cs | 52 +++++++ .../2022/Day 10/Day10Part2.cs | 68 +++++++++ .../2022/Day 10/day-10-input.txt | 137 ++++++++++++++++++ 5 files changed, 281 insertions(+), 10 deletions(-) create mode 100644 Advent Of Code Library/2022/Day 10/Day10Part1.cs create mode 100644 Advent Of Code Library/2022/Day 10/Day10Part2.cs create mode 100644 Advent Of Code Library/2022/Day 10/day-10-input.txt diff --git a/Advend Of Code Runner/Program.cs b/Advend Of Code Runner/Program.cs index dd72e26..cf95727 100644 --- a/Advend Of Code Runner/Program.cs +++ b/Advend Of Code Runner/Program.cs @@ -3,13 +3,16 @@ using AdventOfCodeLibrary._2022; using AdventOfCodeLibrary.Shared; -string _demoData = @"30373 -25512 -65332 -33549 -35390"; +string _demoData = @"R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2"; -Answerable answerable = new Day08Part2(); +Answerable answerable = new Day10Part2(); 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 bf638cb..12f300b 100644 --- a/Advent Of Code Library/2022/Day 09/RopeWalker.cs +++ b/Advent Of Code Library/2022/Day 09/RopeWalker.cs @@ -25,6 +25,12 @@ { X--; } + + internal void Translate(int x, int y) + { + X += x; + Y += y; + } } internal class RopeWalker @@ -54,10 +60,10 @@ // do the actual move switch (direction) { - case 'U': HeadLocation.MoveUp(); break; - case 'D': HeadLocation.MoveDown(); break; - case 'R': HeadLocation.MoveRight(); break; - case 'L': HeadLocation.MoveLeft(); break; + case 'U': HeadLocation.Translate(0, 1); break; + case 'D': HeadLocation.Translate(0, -1); break; + case 'R': HeadLocation.Translate(1, 0); break; + case 'L': HeadLocation.Translate(-1, 0); break; } // check if the tail is touching @@ -90,5 +96,10 @@ _ => throw new AccessViolationException(), }; } + + private bool IsTailTouchingHead(VirtualPoint point1, VirtualPoint point2) + { + return !(Math.Abs(point1.X - point2.X) > 1) && !(Math.Abs(point1.Y - point2.Y) > 1); + } } } diff --git a/Advent Of Code Library/2022/Day 10/Day10Part1.cs b/Advent Of Code Library/2022/Day 10/Day10Part1.cs new file mode 100644 index 0000000..4543ef0 --- /dev/null +++ b/Advent Of Code Library/2022/Day 10/Day10Part1.cs @@ -0,0 +1,52 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day10Part1 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 10; + public override int Part { get; set; } = 1; + + private int Cycles = 0; + private int X = 1; + private readonly int[] measureCycles = { 20, 60, 100, 140, 180, 220 }; + + private int measureResult = 0; + + public override string GetAnswer(byte[] data) + { + string[] commands = GetAsStringArray(data); + + for (int commandIndex = 0; commandIndex < commands.Length; commandIndex++) + { + if (commands[commandIndex].Equals("noop")) + { + ProcessCycles(1, 0); + } + + // command should start with addx + string[] commandValues = commands[commandIndex].Split(' '); + if (commandValues[0].Equals("addx")) + { + ProcessCycles(2, Convert.ToInt32(commandValues[1])); + } + } + return measureResult.ToString(); + } + + internal void ProcessCycles(int cycles, int valueToAdd) + { + for (int i = 0; i < cycles; i++) + { + Cycles++; + if (measureCycles.Contains(Cycles)) + { + measureResult += Cycles * X; + } + } + + X += valueToAdd; + } + } +} diff --git a/Advent Of Code Library/2022/Day 10/Day10Part2.cs b/Advent Of Code Library/2022/Day 10/Day10Part2.cs new file mode 100644 index 0000000..67b942d --- /dev/null +++ b/Advent Of Code Library/2022/Day 10/Day10Part2.cs @@ -0,0 +1,68 @@ +namespace AdventOfCodeLibrary._2022 +{ + using AdventOfCodeLibrary.Shared; + + public class Day10Part2 : Answerable + { + public override int Year { get; set; } = 2022; + public override int Day { get; set; } = 10; + public override int Part { get; set; } = 2; + + private int Cycles = 0; + private int X = 1; + private readonly int[] measureCycles = { 20, 60, 100, 140, 180, 220 }; + + private int measureResult = 0; + + public override string GetAnswer(byte[] data) + { + string[] commands = GetAsStringArray(data); + + for (int commandIndex = 0; commandIndex < commands.Length; commandIndex++) + { + if (commands[commandIndex].Equals("noop")) + { + ProcessCycles(1, 0); + } + + // command should start with addx + string[] commandValues = commands[commandIndex].Split(' '); + if (commandValues[0].Equals("addx")) + { + ProcessCycles(2, Convert.ToInt32(commandValues[1])); + } + } + return measureResult.ToString(); + } + + internal void ProcessCycles(int cycles, int valueToAdd) + { + for (int i = 0; i < cycles; i++) + { + char render = '.'; + + int scanLocation = Cycles % 40; + + if (X - 1 == scanLocation || X == scanLocation || X + 1 == scanLocation) + { + render = '#'; + } + + Console.Write(render); + Cycles++; + + if (Cycles % 40 == 0) + { + Console.Write('\n'); + } + + if (measureCycles.Contains(Cycles)) + { + measureResult += Cycles * X; + } + } + + X += valueToAdd; + } + } +} diff --git a/Advent Of Code Library/2022/Day 10/day-10-input.txt b/Advent Of Code Library/2022/Day 10/day-10-input.txt new file mode 100644 index 0000000..033e54f --- /dev/null +++ b/Advent Of Code Library/2022/Day 10/day-10-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