Day 10: Pipe Maze
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard
🔓 Unlocked after 40 mins
C# I had fun with this one and overbuilt it somewhat. Disappointed I didn’t get/need to implement Dijkstra’s algorithm.
For part two I did a x2 expand, so the example input expanded and found the middles like this (0s for the targets as their easier to distinguish):
Part 1
namespace AdventOfCode2023.Days.Ten { internal class Day10Task1 : IRunnable { private HashSet<(int, int)> _visitedNodes = new HashSet<(int, int)>(); public void Run() { //var inputLines = File.ReadAllLines("Days/Ten/Day10ExampleInput.txt"); //var inputLines = File.ReadAllLines("Days/Ten/Day10ExampleInput2.txt"); var inputLines = File.ReadAllLines("Days/Ten/Day10Input.txt"); var map = new PipeMap(inputLines); _visitedNodes.Add((map.XCoord, map.YCoord)); while (true) { bool haveMoved = false; for (int i = 0; i < 4; i++) { if (map.CanMove((Direction)i)) { map.Move((Direction)i); if (_visitedNodes.Contains((map.XCoord, map.YCoord))) { map.Move(GetOppositeDirection((Direction)i)); } else { _visitedNodes.Add((map.XCoord, map.YCoord)); haveMoved = true; } } } if (!haveMoved) { break; } } Console.WriteLine("Nodes Visited: "+ _visitedNodes.Count); Console.WriteLine("Furthest Node: "+ _visitedNodes.Count/2); } public class PipeMap { public int XCoord { get; private set; } public int YCoord { get; private set; } public char CurrentPipe { get { return Map[YCoord][XCoord]; } } private string[] Map { get; set; } public PipeMap(string[] mapString) { Map = mapString; for (int y = 0; y < mapString.Length; y++) { for (int x = 0; x < mapString[y].Length; x++) { if (mapString[y][x] == 'S') { YCoord = y; XCoord = x; } } } } public void Move(Direction direction) { var adjacent = GetAdjacent(direction); XCoord = adjacent.Item1; YCoord = adjacent.Item2; } public bool CanMove(Direction direction) { bool currentPositionAllow = CanMoveFromCoord(direction, XCoord, YCoord); if (!currentPositionAllow) return false; var adjacent = GetAdjacent(direction); if (adjacent.Item3 != '.' && CanMoveFromCoord(GetOppositeDirection(direction), adjacent.Item1, adjacent.Item2)) { return true; } return false; } private bool CanMoveFromCoord(Direction direction, int xCoord, int yCoord) { switch (Map[yCoord][xCoord]) { case '|': return direction == Direction.Up || direction == Direction.Down; case '-': return direction == Direction.Left || direction == Direction.Right; case 'L': return direction == Direction.Up || direction == Direction.Right; case 'J': return direction == Direction.Up || direction == Direction.Left; case '7': return direction == Direction.Left || direction == Direction.Down; case 'F': return direction == Direction.Right || direction == Direction.Down; case 'S': return true; case '.': default: throw new Exception("you dun fucked up"); } } private (int, int, char) GetAdjacent(Direction direction) { var newXCoord = XCoord; var newYCoord = YCoord; switch (direction) { case Direction.Up: newYCoord--; break; case Direction.Down: newYCoord++; break; case Direction.Left: newXCoord--; break; case Direction.Right: newXCoord++; break; } return (newXCoord, newYCoord, Map[newYCoord][newXCoord]); } } public enum Direction { Up, Right, Down, Left } public static Direction GetOppositeDirection(Direction direction) { switch (direction) { case Direction.Up: return Direction.Down; case Direction.Down: return Direction.Up; case Direction.Right: return Direction.Left; case Direction.Left: return Direction.Right; default: throw new Exception("You dun fucked up... again"); } } } }
Part 2 is too big to post… see pastebin link