Skip to content

Commit

Permalink
day 15 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 16, 2024
1 parent cc65f97 commit f1dfca6
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions AdventOfCode/Day15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ public override ValueTask<string> Solve_2()
{
(char[][] map, List<char> moves, Position robot) = Init(expand: true);

Display(map);
Task.Delay(200).Wait();
foreach (char move in moves)
for (int i = 0; i < moves.Count; ++i)
{
robot = MoveExpanded(move, robot, map);
Display(map);
Task.Delay(200).Wait();
robot = MoveExpanded(moves[i], robot, map);

if (!AssertValid(map))
{
Display(map);
Console.WriteLine(i);
Console.ReadLine();
}
}

int gps = GetGPS(map, '[');
Expand Down Expand Up @@ -124,7 +127,22 @@ private Position Move(char move, Position robot, char[][] map)

private Position MoveExpanded(char move, Position robot, char[][] map)
{
List<Node> next = new List<Node> { new Node (GetNext(move, robot), robot) };
Position f = GetNext(move, robot);
List<Node> next = new List<Node> { new Node (f, robot) };

if (move == 'v' || move == '^')
{
char first = map[f.I][f.J];
if (first == ']')
{
next.Insert(0, new Node(new Position(f.I, f.J - 1), null));
}
if (first == '[')
{
next.Add(new Node(new Position(f.I, f.J + 1), null));
}
}

List<Node> nextRow = next;

while (true)
Expand Down Expand Up @@ -164,15 +182,20 @@ private Position MoveExpanded(char move, Position robot, char[][] map)
char last = map[nextRow.Last().Position.I][nextRow.Last().Position.J];
if (first == ']')
{
nextRow.Insert(0, new Node(new Position(nextRow.First().Position.I, nextRow.First().Position.J - 1), null));
Node n = new Node(new Position(nextRow.First().Position.I, nextRow.First().Position.J - 1), null);
nextRow.Insert(0, n);
next.Add(n);
}
if (last == '[')
{
nextRow.Add(new Node(new Position(nextRow.First().Position.I, nextRow.First().Position.J + 1), null));
Node n = new Node(new Position(nextRow.Last().Position.I, nextRow.Last().Position.J + 1), null);
nextRow.Add(n);
next.Add(n);
}
}

nextRow = nextRow.Select(n => new Node(GetNext(move, n.Position), n.Position)).ToList();
nextRow = nextRow.Where(n => map[n.Position.I][n.Position.J] != '.')
.Select(n => new Node(GetNext(move, n.Position), n.Position)).ToList();
next.AddRange(nextRow);
}
}
Expand Down Expand Up @@ -238,6 +261,25 @@ private void Display(char[][] map)
}
}

private bool AssertValid(char[][] map)
{
for (int i = 0; i < map.Length; ++i)
{
for (int j = 0; j < map[i].Length; ++j)
{
if (map[i][j] == '[' && map[i][j + 1] != ']')
{
return false;
}
if (map[i][j] == ']' && map[i][j - 1] != '[')
{
return false;
}
}
}
return true;
}

private record Position(int I, int J);

private record Node(Position Position, Position? Previous);
Expand Down

0 comments on commit f1dfca6

Please sign in to comment.