From f1dfca6df1d6c305c0f46771328ad10544de68ed Mon Sep 17 00:00:00 2001 From: KaterynaKateryna Date: Mon, 16 Dec 2024 08:48:06 +0100 Subject: [PATCH] day 15 WIP --- AdventOfCode/Day15.cs | 62 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/AdventOfCode/Day15.cs b/AdventOfCode/Day15.cs index c4af5fd..2831931 100644 --- a/AdventOfCode/Day15.cs +++ b/AdventOfCode/Day15.cs @@ -76,13 +76,16 @@ public override ValueTask Solve_2() { (char[][] map, List 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, '['); @@ -124,7 +127,22 @@ private Position Move(char move, Position robot, char[][] map) private Position MoveExpanded(char move, Position robot, char[][] map) { - List next = new List { new Node (GetNext(move, robot), robot) }; + Position f = GetNext(move, robot); + List next = new List { 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 nextRow = next; while (true) @@ -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); } } @@ -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);