Introduction

This post is part of my Advent of Code 2026 series. Before committing to this article, I hope you, the reader, have solved or attempted the puzzle and are using this article as additional learning material to see how I went about solving mine. Whatever the case, happy reading 🎉

I read the questions for part 1 and I did understand the English alright, but! I found myself lost on where to even start. I knew deep inside of me that this was some grid-based problem, but where and how? Looking back now, I know what confused me. While completing day two's problem and looking through other people's process on Reddit, I saw an animation of the solution, and my mind tried to think of the solution from the POV of the animation I had seen.

Like many a problem in life, what it took was a few steps back and a hand-drawn plan that spelled out the pattern my brain picks up trivially by sight.

The puzzle: In plain words

Part 1

The goal of the puzzle is to count the number of squares that can be mapped out from a grid where each node can either be a dot representing a spot in a forest without a tree or a # representing a spot with a tree. In other words, the goal is to count the number of times that four trees occur exactly at the four corners of a grid-aligned square.

An example forest:

#...#
.....
.....
.....
#...#

This forest contains one square.

#..#
.##.
.##.
#..#

This forest contains two squares.

Counting by hand, it is quite easy to get the count for the number of squares formed by the trees, but as is the nature of the puzzles in this series, we have to develop an algorithm for solving this programmatically.

The solution: In pseudocode-ish Python

I structure my solution scripts so that they read from a file that contains the input and then perform the analysis on that input.

Step 1: Read input from a file

Here is a short excerpt of a sample input for the puzzle:

#..#
.##.
.##.
#..#

After reading this from a file, in order to better reflect the grid-based nature of the problem, I converted the input from a string to a list of lists, where the inner lists represent rows and each element in them a column.

Step 2 (Part 1): Implement the main logic for the count

Attempt 1 - Which was the only attempt even though it stretched out a bit

Before writing any code logic, I tried to make sense of what it meant to be a square, I mean, of course it's obvious from looking: four sides of equal length and a 90° angle at every corner. While tracing out a few points by hand, I developed a decision tree: a node A was the top-left corner of a square only if another node at a horizontal distance x to the right of A was also a #, another node x distance below A was also a #, and lastly a third node x distance to the right of A and x distance below was also a #.

From this little decision tree, all I really cared about was finding the top left node for a potential square and validating that it's part of a square by checking the same x distance for the other three nodes. For each valid one, I would keep a record of it and continue with the check until the array of arrays was exhausted.

Animation scanning a 6×6 forest grid row by row: each tree is tried as the top-left corner A, and for every distance x the right, below and diagonal corners are checked in turn until four squares are found

Solution pseudocode:

forest = read_input_into_an_array_of_array()

squares count = 0
rows, cols = number of lines in the puzzle input, length of each line in the puzzle input

for each row_index, line in enumerated(forest):
  for each col_index, spot in enumerated(line):
  
    if the spot is not a tree ignore it and continue the inner loop 
    # this line below assumes that the range has an exclusive upper bound like in Python and Rust
    for each x in the range of 1 to minimum_of(rows - row_index, cols - col_index)

      if it 
        ( 
          has_a_tree_to_the_right_at_distance(x) 
          and has_a_tree_to_the_bottom_at_distance(x) 
          and has_a_tree_to_the_right_bottom_diagonal(x)
        ):
          increment the squares count by 1

the final squares count is the total number of squares

Step 3 (Part 2): Coming soon

Yet to complete this 🫠

Conclusion

Day three was, well, day three. As of the moment of writing this, I am yet to solve the second part. I felt very good after solving the first part, and as part of my commitment to problem-solving muscle training, all solutions are my own, from my own research, and not AI-generated. If you want to see the actual code, you can find my solution on GitHub.

Merry Christmas 🎉✨

Advent of Code Day Three - 2026
https://brianobot.tech/blog/advent-of-code-2026-day-three/
Author Brian Obot
Published December 4, 2026