Skip to content

Commit

Permalink
day 21 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 22, 2024
1 parent b3f3974 commit dedfdf6
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions AdventOfCode/Day21.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Solve_1()
Expand All @@ -16,7 +16,7 @@ public override ValueTask<string> Solve_1()
{
int length = GetShortestPath(code);
int num = int.Parse(code.Replace("A", ""));
Console.WriteLine($"{length} * {num}");
//Console.WriteLine($"{length} * {num}");
res += length * num;
}

Expand All @@ -32,7 +32,6 @@ private int GetShortestPath(string code)
{
List<string> one = GetAllPaths(code, _numpad, new Dictionary<(Position, Position), List<string>>());


var cache = new Dictionary<(Position, Position), List<string>>();

List<string> two = new List<string>();
Expand Down Expand Up @@ -76,7 +75,7 @@ private List<string> GetAllPaths(
}
else
{
segments = GetAllPaths(from, to, "");
segments = GetAllPaths(from, to, "", pad);
cache[(from, to)] = segments;
}

Expand Down Expand Up @@ -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
Expand All @@ -124,7 +123,8 @@ private int GetPathLength(
private List<string> GetAllPaths(
Position from,
Position to,
string currentPath
string currentPath,
Dictionary<char, Position> pad
)
{
List<string> paths = new List<string>();
Expand All @@ -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;
Expand Down

0 comments on commit dedfdf6

Please sign in to comment.