AdventOfCode 2023 Day10 Part1

This commit is contained in:
2023-12-10 07:09:40 +01:00
parent 599bf0a23e
commit dbf75e005d
3 changed files with 494 additions and 0 deletions

331
AdventOfCode2023/Day10.cs Normal file
View File

@@ -0,0 +1,331 @@
namespace AdventOfCode2023;
/*
--- Day 10: Pipe Maze ---
You use the hang glider to ride the hot air from Desert Island all the way up to the floating metal island. This island is surprisingly cold and there definitely aren't any thermals to glide on, so you leave your hang glider behind.
You wander around for a while, but you don't find any people or animals. However, you do occasionally find signposts labeled "Hot Springs" pointing in a seemingly consistent direction; maybe you can find someone at the hot springs and ask them where the desert-machine parts are made.
The landscape here is alien; even the flowers and trees are made of metal. As you stop to admire some metal grass, you notice something metallic scurry away in your peripheral vision and jump into a big pipe! It didn't look like any animal you've ever seen; if you want a better look, you'll need to get ahead of it.
Scanning the area, you discover that the entire field you're standing on is densely packed with pipes; it was hard to tell at first because they're the same metallic silver color as the "ground". You make a quick sketch of all of the surface pipes you can see (your puzzle input).
The pipes are arranged in a two-dimensional grid of tiles:
| is a vertical pipe connecting north and south.
- is a horizontal pipe connecting east and west.
L is a 90-degree bend connecting north and east.
J is a 90-degree bend connecting north and west.
7 is a 90-degree bend connecting south and west.
F is a 90-degree bend connecting south and east.
. is ground; there is no pipe in this tile.
S is the starting position of the animal; there is a pipe on this tile, but your sketch doesn't show what shape the pipe has.
Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is one large, continuous loop.
For example, here is a square loop of pipe:
.....
.F-7.
.|.|.
.L-J.
.....
If the animal had entered this loop in the northwest corner, the sketch would instead look like this:
.....
.S-7.
.|.|.
.L-J.
.....
In the above diagram, the S tile is still a 90-degree F bend: you can tell because of how the adjacent pipes connect to it.
Unfortunately, there are also many pipes that aren't connected to the loop! This sketch shows the same loop as above:
-L|F7
7S-7|
L|7||
-L-J|
L|-JF
In the above diagram, you can still figure out which pipes form the main loop: they're the ones connected to S, pipes those pipes connect to, pipes those pipes connect to, and so on. Every pipe in the main loop connects to its two neighbors (including S, which will have exactly two pipes connecting to it, and which is assumed to connect back to those two pipes).
Here is a sketch that contains a slightly more complex main loop:
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
Here's the same example sketch with the extra, non-main-loop pipe tiles also shown:
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ
If you want to get out ahead of the animal, you should find the tile in the loop that is farthest from the starting position. Because the animal is in the pipe, it doesn't make sense to measure this by direct distance. Instead, you need to find the tile that would take the longest number of steps along the loop to reach from the starting point - regardless of which way around the loop the animal went.
In the first example with the square loop:
.....
.S-7.
.|.|.
.L-J.
.....
You can count the distance each tile in the loop is from the starting point like this:
.....
.012.
.1.3.
.234.
.....
In this example, the farthest point from the start is 4 steps away.
Here's the more complex loop again:
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
Here are the distances for each tile on that loop:
..45.
.236.
01.78
14567
23...
Find the single giant loop starting at S. How many steps along the loop does it take to get from the starting position to the point farthest from the starting position?
*/
public class Day10 : IDay
{
public string ResolvePart1(string[] inputs)
{
PipeMaze maze = new(inputs);
Point startPoint = maze.SearchCell('S') ?? new Point(0, 0);
maze.SetDistance(startPoint, 0);
maze.FollowPipeSettingDistance(new Point(startPoint.X + 1, startPoint.Y), startPoint);
maze.FollowPipeSettingDistance(new Point(startPoint.X - 1, startPoint.Y), startPoint);
maze.FollowPipeSettingDistance(new Point(startPoint.X, startPoint.Y + 1), startPoint);
maze.FollowPipeSettingDistance(new Point(startPoint.X, startPoint.Y - 1), startPoint);
int maxDistance = maze.GetMaxDistance();
return maxDistance.ToString();
}
public string ResolvePart2(string[] inputs)
{
throw new NotImplementedException();
}
private readonly struct Point
{
public readonly int X;
public readonly int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
private class PipeMaze
{
private readonly string[] _maze;
private readonly int _width;
private readonly int _height;
private readonly int?[] _distances;
public PipeMaze(string[] maze)
{
_maze = maze;
_height = _maze.Length;
_width = _maze.Max(s => s.Length);
_distances = Enumerable.Repeat<int?>(null, _width * _height).ToArray();
}
public Point? SearchCell(char cell)
{
for (int j = 0; j < _maze.Length; j++)
{
for (int i = 0; i < _maze[j].Length; i++)
{
if (_maze[j][i] == cell)
{
return new Point(i, j);
}
}
}
return null;
}
public char? GetCell(Point point)
{
if (point.Y < 0 || point.Y >= _maze.Length) { return null; }
string mazeRow = _maze[point.Y];
if (point.X < 0 || point.X >= mazeRow.Length) { return null; }
return mazeRow[point.X];
}
public int? GetDistance(Point point)
{
if (point.X < 0 || point.X >= _width) { return null; }
if (point.Y < 0 || point.Y >= _height) { return null; }
int offset = (point.Y * _width) + point.X;
return _distances[offset];
}
public void SetDistance(Point point, int distance)
{
if (point.X < 0 || point.X >= _width) { return; }
if (point.Y < 0 || point.Y >= _height) { return; }
int offset = (point.Y * _width) + point.X;
_distances[offset] = distance;
}
public Point? FollowPipe(Point point, Point prevPoint)
{
char? cell = GetCell(point);
if (cell == null)
{
return null;
}
if (cell == '|')
{
if (point.X != prevPoint.X) { return null; }
return new Point(
x: point.X,
y: prevPoint.Y > point.Y ? point.Y - 1 : point.Y + 1);
}
if (cell == '-')
{
if (point.Y != prevPoint.Y) { return null; }
return new Point(
x: prevPoint.X > point.X ? point.X - 1 : point.X + 1,
y: point.Y);
}
if (cell == 'L')
{
if (point.X == prevPoint.X && prevPoint.Y < point.Y)
{
return new Point(
x: point.X + 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X > point.X)
{
return new Point(
x: point.X,
y: point.Y - 1);
}
return null;
}
if (cell == 'J')
{
if (point.X == prevPoint.X && prevPoint.Y < point.Y)
{
return new Point(
x: point.X - 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X < point.X)
{
return new Point(
x: point.X,
y: point.Y - 1);
}
return null;
}
if (cell == '7')
{
if (point.X == prevPoint.X && prevPoint.Y > point.Y)
{
return new Point(
x: point.X - 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X < point.X)
{
return new Point(
x: point.X,
y: point.Y + 1);
}
return null;
}
if (cell == 'F')
{
if (point.X == prevPoint.X && prevPoint.Y > point.Y)
{
return new Point(
x: point.X + 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X > point.X)
{
return new Point(
x: point.X,
y: point.Y + 1);
}
return null;
}
if (cell == '.')
{
return null;
}
if (cell == 'S')
{
return null;
}
return null;
}
public void FollowPipeSettingDistance(Point point, Point prevPoint)
{
int distance = 0;
do
{
Point? nextPoint = FollowPipe(point, prevPoint);
if (nextPoint == null) { break; }
distance++;
int? currentDistance = GetDistance(point);
if (currentDistance > distance || currentDistance == null)
{
SetDistance(point, distance);
}
prevPoint = point;
point = nextPoint.Value;
} while (true);
}
public int GetMaxDistance()
{
int max = int.MinValue;
int len = _width * _height;
for (int i = 0; i < len; i++)
{
int? distance = _distances[i];
if (distance > max)
{
max = distance.Value;
}
}
return max;
}
}
}