# AoC 2022 day 24 part 2

For some reason it leads to a Stack Overflow with the actual input (works fine with the test input), I can't find the mistake!
unknown
csharp
2 years ago
2.6 kB
39
Indexable
Never
```using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

{
public static class Day14
{
private static HashSet<(int x, int y)> _tiles = new();
private static int _bottom;
private static int _sandParticles = 0;
private static (int x, int y) _firstTile;
private static Stack<(int x, int y)> _previousTiles = new();
private static Queue<(int x, int y)> _lookup = new();
public static void Solve(int part)
{

foreach (string line in input) ParseRocks(line);
_bottom = _tiles.Max(t => t.y) + 2;

_firstTile = (500, 0);
_lookup.Enqueue(_firstTile);

BFS();

Console.WriteLine(_sandParticles);
}

private static void ParseRocks(string path)
{
(int x, int y)[] coords = path
.Split(" -> ")
.Select(c => (int.Parse(c.Split(',').First()),int.Parse(c.Split(',').Last())))
.ToArray();

for (int node = 1; node < coords.Length; node++)
{
var node1 = coords[node];
var node2 = coords[node - 1];

if (node1.x != node2.x)
{
int x1 = Math.Min(node1.x, node2.x);
int x2 = Math.Max(node1.x, node2.x);
for (int x = x1; x <= x2; x++) _tiles.Add((x, node1.y));
}

if (node1.y != node2.y)
{
int y1 = Math.Min(node1.y, node2.y);
int y2 = Math.Max(node1.y, node2.y);
for (int y = y1; y <= y2; y++) _tiles.Add((node1.x, y));
}
}
}

private static readonly (int x, int y)[] _directions = { (0, 1), (-1, 1), (1, 1)};

private static void BFS()
{
if (_lookup.Count == 0) return;

var current = _lookup.Dequeue();
_sandParticles++;

foreach (var d in _directions)
{
(int x, int y) neighbour = (current.x + d.x, current.y + d.y);
if (_tiles.Contains(neighbour) || _lookup.Contains(neighbour) || neighbour.y >= _bottom) continue;

_lookup.Enqueue(neighbour);
}

BFS();
}
}
}```