From de28311e6987f0d8717625c0235cde516edeb14e Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Tue, 12 Dec 2023 12:42:38 -0500 Subject: [PATCH] misc --- .../Problems/AOC2023/Day10/PipeMaze.cs | 71 +++++++++++++++---- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/AdventOfCode/Problems/AOC2023/Day10/PipeMaze.cs b/AdventOfCode/Problems/AOC2023/Day10/PipeMaze.cs index 3681bd0..bb84125 100644 --- a/AdventOfCode/Problems/AOC2023/Day10/PipeMaze.cs +++ b/AdventOfCode/Problems/AOC2023/Day10/PipeMaze.cs @@ -20,27 +20,71 @@ internal class PipeMaze : Problem public override void CalculatePart1() { - Console.WriteLine(GetNextPoint((1, 1), (1, 2))); - Console.WriteLine(GetNextPoint((1, 1), (2, 1))); + var start = GetStartPointPos(); + var nPoints = GetStartConnections(start); + var seen = new Dictionary<(int, int), int>(); - Console.WriteLine(GetNextPoint((3, 1), (3, 2))); - Console.WriteLine(GetNextPoint((3, 1), (2, 1))); + foreach (var point in nPoints) + { + var curPoint = start; + var prevPoint = point; + var dist = 0; + while (true) + { + dist++; + var next = GetNextPoint(curPoint, prevPoint); + prevPoint = curPoint; + curPoint = next; + if (next == start) + break; + if (seen.TryGetValue(next, out var d)) + { + if (d > dist) + seen[next] = dist; + else + break; + } + else + seen.Add(next, dist); + } + } + } - Console.WriteLine(GetNextPoint((3, 3), (3, 2))); - Console.WriteLine(GetNextPoint((3, 3), (2, 3))); + private (int x, int y) GetStartPointPos() + { + for (int y = 0; y < _maze.Length; y++) + { + var x = _maze[y].IndexOf('S'); + if (x >= 0) + return (x, y); + } + throw new Exception("Start point not found"); + } - Console.WriteLine(GetNextPoint((1, 3), (1, 2))); - Console.WriteLine(GetNextPoint((1, 3), (2, 3))); + private List<(int x, int y)> GetStartConnections((int x, int y) pos) + { + var points = new List<(int x, int y)>(); + if (_maze[pos.y + 1][pos.x] is '|' or 'J' or 'L') + points.Add((pos.x + 1, pos.y)); + if (_maze[pos.y - 1][pos.x] is '|' or 'F' or '7') + points.Add((pos.x - 1, pos.y)); + + if (_maze[pos.y][pos.x + 1] is '-' or 'J' or '7') + points.Add((pos.x, pos.y + 1)); + if (_maze[pos.y][pos.x - 1] is '-' or 'F' or 'L') + points.Add((pos.x, pos.y - 1)); + + return points; } private (int x, int y) GetNextPoint((int x, int y) pos, (int x, int y) prev) { var curPipe = _maze[pos.y][pos.x]; var dir = (x: pos.x - prev.x, y: pos.y - prev.y); - if(curPipe == 'S') - { - throw new Exception(); - } + + if (curPipe == 'S') + return GetStartConnections(pos).First(p => p != prev); + return curPipe switch { '|' => (pos.x, pos.y + dir.y), @@ -57,5 +101,4 @@ internal class PipeMaze : Problem { throw new NotImplementedException(); } - -} +} \ No newline at end of file