From dedfdf6b12b4412ace9853e2bd1a3bf28c3e0ec9 Mon Sep 17 00:00:00 2001 From: KaterynaKateryna Date: Sun, 22 Dec 2024 21:55:08 +0100 Subject: [PATCH] day 21 part 1 --- AdventOfCode/Day21.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/AdventOfCode/Day21.cs b/AdventOfCode/Day21.cs index 254a14b..19b7788 100644 --- a/AdventOfCode/Day21.cs +++ b/AdventOfCode/Day21.cs @@ -6,7 +6,7 @@ public class Day21 : BaseDay public Day21() { - _codes = ["029A", "980A", "179A", "456A", "379A"]; //File.ReadAllLines(InputFilePath); + _codes = File.ReadAllLines(InputFilePath); } public override ValueTask Solve_1() @@ -16,7 +16,7 @@ public override ValueTask Solve_1() { int length = GetShortestPath(code); int num = int.Parse(code.Replace("A", "")); - Console.WriteLine($"{length} * {num}"); + //Console.WriteLine($"{length} * {num}"); res += length * num; } @@ -32,7 +32,6 @@ private int GetShortestPath(string code) { List one = GetAllPaths(code, _numpad, new Dictionary<(Position, Position), List>()); - var cache = new Dictionary<(Position, Position), List>(); List two = new List(); @@ -76,7 +75,7 @@ private List GetAllPaths( } else { - segments = GetAllPaths(from, to, ""); + segments = GetAllPaths(from, to, "", pad); cache[(from, to)] = segments; } @@ -109,7 +108,7 @@ private int GetPathLength( } else { - var segments = GetAllPaths(from, to, ""); + var segments = GetAllPaths(from, to, "", pad); lengthCache[(from, to)] = segments.Min(s => s.Length); length += lengthCache[(from, to)]; length++; // A @@ -124,7 +123,8 @@ private int GetPathLength( private List GetAllPaths( Position from, Position to, - string currentPath + string currentPath, + Dictionary pad ) { List paths = new List(); @@ -134,21 +134,21 @@ string currentPath return paths; } - if (from.I - to.I > 0) + if (from.I - to.I > 0 && pad.Values.Contains(new Position(from.I - 1, from.J))) { - paths.AddRange(GetAllPaths(new Position(from.I - 1, from.J), to, currentPath + "^")); + paths.AddRange(GetAllPaths(new Position(from.I - 1, from.J), to, currentPath + "^", pad)); } - if (from.I - to.I < 0) + if (from.I - to.I < 0 && pad.Values.Contains(new Position(from.I + 1, from.J))) { - paths.AddRange(GetAllPaths(new Position(from.I + 1, from.J), to, currentPath + "V")); + paths.AddRange(GetAllPaths(new Position(from.I + 1, from.J), to, currentPath + "V", pad)); } - if (from.J - to.J > 0) + if (from.J - to.J > 0 && pad.Values.Contains(new Position(from.I, from.J - 1))) { - paths.AddRange(GetAllPaths(new Position(from.I, from.J - 1), to, currentPath + "<")); + paths.AddRange(GetAllPaths(new Position(from.I, from.J - 1), to, currentPath + "<", pad)); } - if (from.J - to.J < 0) + if (from.J - to.J < 0 && pad.Values.Contains(new Position(from.I, from.J + 1))) { - paths.AddRange(GetAllPaths(new Position(from.I, from.J + 1), to, currentPath + ">")); + paths.AddRange(GetAllPaths(new Position(from.I, from.J + 1), to, currentPath + ">", pad)); } return paths;