Skip to content

Commit

Permalink
day 16 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 18, 2024
1 parent 42246c3 commit b314ba9
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions AdventOfCode/Day16.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace AdventOfCode;
namespace AdventOfCode;

internal class Day16 : BaseDay
{
Expand Down Expand Up @@ -78,10 +77,12 @@ public override ValueTask<string> Solve_2()
}

GetCost(map, costs, start, 0, end, Direction.East);
DisplayCosts(costs);
//DisplayCosts(costs);
long finalCost = costs[end.I][end.J];

List<Position> paths = GetPath(map, costs, start, 0, end, Direction.East, new List<Position>() { start }, finalCost);
var stack = new Stack<Position>();
stack.Push(start);
List<Position> paths = GetPath(map, costs, start, 0, end, Direction.East, stack, finalCost);
long path = paths.Distinct().Count();

return new(path.ToString());
Expand Down Expand Up @@ -136,50 +137,50 @@ private List<Position> GetPath(
Position current,
long currentCost,
Position end,
Direction direction,
List<Position> path,
Direction direction,
Stack<Position> path,
long finalCost
)
{
if (current == end)
{
return path;
return path.ToList();
}

List<Position> positions = new List<Position>();

Position straight = GetNext(current, direction);
if (map[straight.I][straight.J] != '#')
{
if (currentCost + 1 <= finalCost)
if (currentCost + 1 <= finalCost && currentCost + 1 <= costs[straight.I][straight.J] + 1000 && !path.Contains(straight))
{
path.Add(straight);
path.Push(straight);
positions.AddRange(GetPath(map, costs, straight, currentCost + 1, end, direction, path, finalCost));
path.RemoveAt(path.Count - 1);
path.Pop();
}
}

Direction clockwiseDirection = RotateClockwise(direction);
Position clockwise = GetNext(current, clockwiseDirection);
if (map[clockwise.I][clockwise.J] != '#')
{
if (currentCost + 1001 <= finalCost)
if (currentCost + 1001 <= finalCost && currentCost + 1001 <= costs[clockwise.I][clockwise.J] + 1000 && !path.Contains(clockwise))
{
path.Add(clockwise);
path.Push(clockwise);
positions.AddRange(GetPath(map, costs, clockwise, currentCost + 1001, end, clockwiseDirection, path, finalCost));
path.RemoveAt(path.Count - 1);
path.Pop();
}
}

Direction counterDirection = RotateCounterClockwise(direction);
Position counter = GetNext(current, counterDirection);
if (map[counter.I][counter.J] != '#')
{
if (currentCost + 1001 <= finalCost)
if (currentCost + 1001 <= finalCost && currentCost + 1001 <= costs[counter.I][counter.J] + 1000 && !path.Contains(counter))
{
path.Add(counter);
path.Push(counter);
positions.AddRange(GetPath(map, costs, counter, currentCost + 1001, end, counterDirection, path, finalCost));
path.RemoveAt(path.Count - 1);
path.Pop();
}
}

Expand Down

0 comments on commit b314ba9

Please sign in to comment.