Skip to content

Commit

Permalink
Day 21 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 22, 2024
1 parent b04a93c commit b3f3974
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions AdventOfCode/Day21.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ private int GetShortestPath(string code)
two.AddRange(GetAllPaths(path, _directionalPad, cache));
}

var lengthCache = cache.ToDictionary(x => x.Key, y => y.Value.Min(s => s.Length));

int shortest = int.MaxValue;
foreach (string path in two)
{
var t = GetPathLength(path, _directionalPad, cache);
var t = GetPathLength(path, _directionalPad, lengthCache);
if (t < shortest)
{
shortest = t;
Expand Down Expand Up @@ -74,7 +76,7 @@ private List<string> GetAllPaths(
}
else
{
segments = GetAllPaths(from, to, "", cache);
segments = GetAllPaths(from, to, "");
cache[(from, to)] = segments;
}

Expand All @@ -90,7 +92,7 @@ private List<string> GetAllPaths(
private int GetPathLength(
string code,
Dictionary<char, Position> pad,
Dictionary<(Position, Position), List<string>> cache
Dictionary<(Position, Position), int> lengthCache
)
{
Position from = pad['A'];
Expand All @@ -100,17 +102,17 @@ private int GetPathLength(
{
Position to = pad[button];

if (cache.ContainsKey((from, to)))
if (lengthCache.ContainsKey((from, to)))
{
length += cache[(from, to)][0].Length;
length++;
length += lengthCache[(from, to)];
length++; // A
}
else
{
var segments = GetAllPaths(from, to, "", cache);
cache[(from, to)] = segments;
length += cache[(from, to)][0].Length;
length++;
var segments = GetAllPaths(from, to, "");
lengthCache[(from, to)] = segments.Min(s => s.Length);
length += lengthCache[(from, to)];
length++; // A
}

from = to;
Expand All @@ -122,8 +124,7 @@ private int GetPathLength(
private List<string> GetAllPaths(
Position from,
Position to,
string currentPath,
Dictionary<(Position, Position), List<string>> cache
string currentPath
)
{
List<string> paths = new List<string>();
Expand All @@ -135,19 +136,19 @@ private List<string> GetAllPaths(

if (from.I - to.I > 0)
{
paths.AddRange(GetAllPaths(new Position(from.I - 1, from.J), to, currentPath + "^", cache));
paths.AddRange(GetAllPaths(new Position(from.I - 1, from.J), to, currentPath + "^"));
}
if (from.I - to.I < 0)
{
paths.AddRange(GetAllPaths(new Position(from.I + 1, from.J), to, currentPath + "V", cache));
paths.AddRange(GetAllPaths(new Position(from.I + 1, from.J), to, currentPath + "V"));
}
if (from.J - to.J > 0)
{
paths.AddRange(GetAllPaths(new Position(from.I, from.J - 1), to, currentPath + "<", cache));
paths.AddRange(GetAllPaths(new Position(from.I, from.J - 1), to, currentPath + "<"));
}
if (from.J - to.J < 0)
{
paths.AddRange(GetAllPaths(new Position(from.I, from.J + 1), to, currentPath + ">", cache));
paths.AddRange(GetAllPaths(new Position(from.I, from.J + 1), to, currentPath + ">"));
}

return paths;
Expand Down

0 comments on commit b3f3974

Please sign in to comment.