diff --git a/Year2022/Day9.cs b/Year2022/Day9.cs new file mode 100644 index 0000000..b9794f5 --- /dev/null +++ b/Year2022/Day9.cs @@ -0,0 +1,87 @@ +namespace AdventOfCode.Year2022; + +public class Day9 +{ + private static readonly List<(char, int)> Moves = new(); + private static readonly Dictionary> Position = new() + { + ['H'] = new List(), + ['T'] = new List() + }; + + public Day9() + { + Console.WriteLine("Day9 Solution"); + File.ReadAllLines("inputs/day9.txt").ToList().ForEach(line => + { + string[] split = line.Split(" "); + Moves.Add((Convert.ToChar(split[0]), Convert.ToInt32(split[1]))); + }); + Console.WriteLine($"Part1 Result: {Part1()}"); + Console.WriteLine($"Part2 Result: {Part2()}"); + Console.WriteLine("\n=============================\n"); + } + + private static int Part1() + { + Position['H'] = new[] {0, 0}.ToList(); + Position['T'] = new[] {0, 0}.ToList(); + HashSet<(int, int)> usedPositions = new HashSet<(int, int)>(); + + foreach ((char, int) move in Moves) + { + List head = Position['H']; + List tail = Position['T']; + for (int i = 0; i < move.Item2; i++) + { + if (move.Item1 == 'U') + { + head[1]++; + if (ValidatePosition()) continue; + tail[1]++; + tail[0] += head[0] - tail[0]; + } + else if (move.Item1 == 'D') + { + head[1]--; + if (ValidatePosition()) continue; + tail[1]--; + tail[0] += head[0] - tail[0]; + } + else if (move.Item1 == 'R') + { + head[0]++; + if (ValidatePosition()) continue; + tail[0]++; + tail[1] += head[1] - tail[1]; + } + else if (move.Item1 == 'L') + { + head[0]--; + if (ValidatePosition()) continue; + tail[0]--; + tail[1] += head[1] - tail[1]; + } + usedPositions.Add((tail[0], tail[1])); + } + } + return usedPositions.Count; + } + + private static int Part2() + { + Position['H'] = new[] {0, 0}.ToList(); + Position['T'] = new[] {0, 0}.ToList(); + HashSet<(int, int)> usedPositions = new HashSet<(int, int)>(); + + foreach ((char, int) move in Moves) + { + + } + + return usedPositions.Count; + } + + private static bool ValidatePosition() + => Position['H'][0] - Position['T'][0] is > -2 and < 2 && Position['H'][1] - Position['T'][1] is > -2 and < 2; +} \ No newline at end of file diff --git a/Year2022/Loader2022.cs b/Year2022/Loader2022.cs index 3102c1f..861aefb 100644 --- a/Year2022/Loader2022.cs +++ b/Year2022/Loader2022.cs @@ -12,5 +12,6 @@ public static class Loader2022 new Day6(); new Day7(); // new Day8(); -- Commented because it's a slow solution + new Day9(); } } \ No newline at end of file