* This blog post is a summary of this video.

Solving Advent of Code 2022 Day 10 Map Pathfinding Puzzles

Author: Next TurnTime: 2024-01-03 17:20:00

Table of Contents

Understanding Map Pathfinding in Day 10 Coding Challenge

The Day 10 coding challenge involves map pathfinding puzzles. We are given a map made up of pipe symbols that connect in different directions, along with ground symbols that we treat as impassable. One pipe symbol is replaced with an S to mark the starting location. Our goal is to find the longest continuous pipe path that starts from the S location.

There are a few key things to understand about the input format and rules for solving these map pathfinding puzzles, covered in the next sections.

Input Format for the Map Pathfinding Puzzles

The pipe symbols in the input map connect up/down or left/right. For example:

  • A pipe symbol pointing up/down can only connect to other up/down pipes above or below it
  • A left/right pipe can only connect left or right Any ground symbols (periods) are impassable - pipes cannot connect through them. Finally, one of the pipe symbols is replaced by an S to mark the start location. The S can be any of the pipe symbols, and we need to figure out which directions it connects.

Key Rules for Connecting Pipes

With the map input, there are some key rules we need to follow to connect pipes:

  • Pipes can only connect in their inherent directions (up/down or left/right)
  • Connection paths cannot pass through ground/period symbols
  • We are looking for a single continuous loop that starts from the S location
  • If a path reaches an already visited pipe or a dead-end, that path ends Following these rules, we try to find the longest single continuous pipe loop starting from S by exploring all possible paths.

Finding the Longest Path Through the Maze

To actually find the longest path, we can use a recursive depth-first search approach:

  • Replace the S start location with whichever pipe symbol is there

  • Set the current location to next possible connected neighbor based on pipe directions

  • Recursively explore paths from that neighbor, backtracking when we hit already visited nodes or dead-ends

  • Keep track of the longest path found and return its length divided by 2 (to get furthest distance)

Counting Fully Enclosed Spaces Within the Longest Path

After finding the longest path, we also need to count the number of fully enclosed spaces inside that path. This involves some trickiness:

  • Spaces that connect to the outer edges, even barely, cannot be counted

  • Even if pipes are touching, spaces between them are still considered connected to the outside if there is a single opening

To handle this, we can:

  • Double the size of the grid so there are gaps between any touching pipes

  • Fill in all connected spaces, starting from outside edge

  • Count leftover spaces which were fully enclosed within the pipe loop

This allows us to accurately count the fully enclosed spaces within the longest pipe path.

Optimizing Overall Solution

One last key optimization is that we actually need to try replacing S with each pipe symbol and direction to find the longest path:

  • Replace S with possible pipe connections and do full search from each

  • Return the longest path of all those checked, not just first one

This ensures we actually find the absolute longest path and fully handle all possible test case map configurations.


Q: What programming language and concepts are used to solve this challenge?
A: The preferred language is Python, using concepts like grids/matrices, recursion, pathfinding algorithms like DFS, etc.

Q: What is the main goal for Part 1 of this puzzle?
A: The goal of Part 1 is to find the longest continuous path starting from the given start point 'S' through the pipe maze.

Q: What is the main goal for Part 2 of this puzzle?
A: The goal of Part 2 is to count the number of spots that are fully enclosed within the longest path found in Part 1.

Q: What is a key insight for handling all test cases?
A: A key insight is that you need to try all 6 possible pipe symbols for the start point 'S', since each may produce different maximum path lengths.