Skip to content

Commit

Permalink
solve day 23 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lucafluri committed Dec 27, 2023
1 parent 11fd569 commit 9b44684
Showing 1 changed file with 103 additions and 113 deletions.
216 changes: 103 additions & 113 deletions AdventOfCode/Day23.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ namespace AdventOfCode;

public class Day23 : BaseDay
{
private static Random rng = new Random();
private readonly Dictionary<(int, int), char> _grid;
// private Dictionary<(int, int), bool> _visited = new();
// private bool[,] _visited; // assuming width and height are the dimensions of the grid
private bool[,] _visited; // assuming width and height are the dimensions of the grid

private Dictionary<(int, int), int> cache = new();
private readonly (int, int) _start;
private readonly (int, int) _end;
private readonly int width;
Expand All @@ -20,18 +17,8 @@ public class Day23 : BaseDay
private List<(int, int)> DefaultDirections = new(){ (0, 1), (-1, 0), (0, -1), (1, 0) };
private static readonly Dictionary<char, (int, int)> GridDirections = new() {{ '>', (1, 0) }, { '<', (-1, 0) }, { '^', (0, -1) }, { 'v', (0, 1) }};

void PrintGrid()
{
for (var y = 0; y <= _grid.Keys.Max(x => x.Item2); y++)
{
for (var x = 0; x <= _grid.Keys.Max(x => x.Item1); x++)
{
Console.Write(_grid[(x, y)]);

}
Console.WriteLine();
}
}
private List<(int, int)> intersections;


public Day23()
{
Expand All @@ -43,133 +30,136 @@ public Day23()
width = _grid.Keys.Max(x => x.Item1);
height = _grid.Keys.Max(x => x.Item2);

// _visited = new bool[width+1, height+1]; // assuming width and height are the dimensions of the grid
_visited = new bool[width+1, height+1]; // assuming width and height are the dimensions of the grid



Console.WriteLine($"Start: {_start}");
Console.WriteLine($"End: {_end}");


intersections = _grid.Where(x => x.Value == '.' &&
GridDirections.ContainsKey(_grid.GetValueOrDefault((x.Key.Item1, x.Key.Item2 - 1))) && GridDirections.ContainsKey(_grid.GetValueOrDefault((x.Key.Item1, x.Key.Item2 + 1))) ||
GridDirections.ContainsKey(_grid.GetValueOrDefault((x.Key.Item1 - 1, x.Key.Item2))) && GridDirections.ContainsKey(_grid.GetValueOrDefault((x.Key.Item1 + 1, x.Key.Item2)))).Select(x => x.Key).Distinct().ToList();

intersections.Add(_start);
intersections.Add(_end);

Console.WriteLine($"Intersections: {intersections.Count}");
Console.WriteLine($"Intersections: {string.Join(", ", intersections)}");



}

// private void DFS((int, int) pos, int steps, bool part2 = false)
// {
// if (pos == _end)
// {
// if(_maxSteps < steps) Console.WriteLine($"New Max steps: {steps}");
// _maxSteps = Math.Max(_maxSteps, steps);
// }
//
// // DefaultDirections = DefaultDirections.OrderBy(x => rng.Next()).ToList();
// var directions = part2 || !GridDirections.TryGetValue(_grid[pos], out var dir) ? DefaultDirections : new List<(int, int)> { dir };
//
// _visited[pos.Item1, pos.Item2] = true;
//
// foreach (var (dx, dy) in directions)
// {
// var newPos = (pos.Item1 + dx, pos.Item2 + dy);
// if (_grid.TryGetValue(newPos, out char value) && value != '#' && !_visited[newPos.Item1, newPos.Item2])
// {
// DFS(newPos, steps + 1, part2);
// }
// }
//
// _visited[pos.Item1, pos.Item2] = false;
// }

// private int IterativeDFS((int, int) start, bool part2 = false)
// {
// Stack<((int, int), int)> stack = new Stack<((int, int), int)>();
// stack.Push((start, 0));
//
// while (stack.Count > 0)
// {
// var (pos, steps) = stack.Pop();
//
// if (pos == _end)
// {
// _maxSteps = Math.Max(_maxSteps, steps);
// continue; // Continue to check other paths even after finding the end
// }
//
// _visited[pos.Item1, pos.Item2] = true;
//
// var directions = part2 || !GridDirections.TryGetValue(_grid[pos], out var dir) ? DefaultDirections : new List<(int, int)> { dir };
//
// foreach (var (dx, dy) in directions)
// {
// var newPos = (pos.Item1 + dx, pos.Item2 + dy);
// if (_grid.TryGetValue(newPos, out char value) && value != '#' && !_visited[newPos.Item1, newPos.Item2])
// {
// stack.Push((newPos, steps + 1));
// }
// }
//
// _visited[pos.Item1, pos.Item2] = false; // Unmark visited after processing to allow other paths
// }
//
// return _maxSteps;
// }
//

private HashSet<(int, int)> _visited = new HashSet<(int, int)>();
private Dictionary<(int, int), List<(int, int)>> _validMovesCache = new Dictionary<(int, int), List<(int, int)>>();

private void DFS((int, int) pos, int steps, bool part2 = false)
private void DFS((int, int) pos, int steps, (int, int) end, bool part2 = false)
{
if (pos == _end)
if (pos == end)
{
if (_maxSteps < steps) Console.WriteLine($"New Max steps: {steps}");
if(_maxSteps < steps) Console.WriteLine($"New Max steps: {steps}");
_maxSteps = Math.Max(_maxSteps, steps);
return;
}

_visited.Add(pos);

List<(int, int)> directions;
if (!_validMovesCache.TryGetValue(pos, out directions))
{
// Console.WriteLine($"Cache miss for {pos}");
directions = part2 || !GridDirections.TryGetValue(_grid[pos], out var dir) ? DefaultDirections : new List<(int, int)> { dir };
_validMovesCache[pos] = directions;
}


var directions = part2 || !GridDirections.TryGetValue(_grid[pos], out var dir) ? DefaultDirections : new List<(int, int)> { dir };

_visited[pos.Item1, pos.Item2] = true;

foreach (var (dx, dy) in directions)
{
{
var newPos = (pos.Item1 + dx, pos.Item2 + dy);
if (_grid.TryGetValue(newPos, out char value) && value != '#' && !_visited.Contains(newPos))
if (_grid.TryGetValue(newPos, out char value) && value != '#' && !_visited[newPos.Item1, newPos.Item2])
{
DFS(newPos, steps + 1, part2);
DFS(newPos, steps + 1, end,part2);
}
}

_visited[pos.Item1, pos.Item2] = false;
}


private List<((int, int), int)> FindAllConnectedIntersections((int, int) start)
{
var connectedIntersections = new List<((int, int), int)>();
var visited = new HashSet<(int, int)>();
var stack = new Stack<((int, int), int)>();
stack.Push((start, 0));

while (stack.Count > 0)
{
var (pos, steps) = stack.Pop();

if (intersections.Contains(pos) && start != pos)
{
connectedIntersections.Add((pos, steps));
continue;
}

visited.Add(pos);

var directions = DefaultDirections;

foreach (var (dx, dy) in directions)
{
var newPos = (pos.Item1 + dx, pos.Item2 + dy);
if (_grid.TryGetValue(newPos, out char value) && value != '#' && !visited.Contains(newPos))
{
stack.Push((newPos, steps + 1));
}
}
}

_visited.Remove(pos);
return connectedIntersections;
}


private void DFS_adj(Dictionary<(int, int), List<((int, int), int)>> adj, (int, int) start, (int, int) end)
{
Stack<((int, int), int, HashSet<(int, int)>)> stack = new Stack<((int, int), int, HashSet<(int, int)>)>();
stack.Push((start, 0, new HashSet<(int, int)>() { start }));

while (stack.Count > 0)
{
var (pos, steps, visited) = stack.Pop();

if (pos == end)
{
if(_maxSteps < steps) Console.WriteLine($"New Max steps: {steps}");
_maxSteps = Math.Max(_maxSteps, steps);
continue;
}

foreach (var (newPos, dist) in adj[pos])
{
if (!visited.Contains(newPos))
{
var newVisited = new HashSet<(int, int)>(visited) { newPos };
stack.Push((newPos, steps + dist, newVisited));
}
}
}
}




public override ValueTask<string> Solve_1()
{
// IterativeDFS(_start);
DFS(_start, 0,false);
DFS(_start, 0,_end,false);
Console.WriteLine($"Max steps Part 1: {_maxSteps}");
return new (_maxSteps.ToString()); //2214
}

public override ValueTask<string> Solve_2()
{
// _visited = new bool[width+1, height+1]; // assuming width and height are the dimensions of the grid
_visited = new HashSet<(int, int)>();
_validMovesCache = new Dictionary<(int, int), List<(int, int)>>();
cache = new();
// _memo = new Dictionary<(int, int), int>();
// Build direct Adjancency List from Intersections with first connected other intersection with distance between them
var adjList = new Dictionary<(int, int), List<((int, int), int)>>();

foreach (var intersection in intersections)
adjList[intersection] = FindAllConnectedIntersections(intersection);

_maxSteps = 0;
DFS(_start, 0, true);
// IterativeDFS(_start, true);
return new (_maxSteps.ToString());
//4646, 6552 LOW


DFS_adj(adjList, _start, _end);
Console.WriteLine($"Max steps Part 2: {_maxSteps}");
return new (_maxSteps.ToString()); // 6594
}
}

0 comments on commit 9b44684

Please sign in to comment.