Rickard Lindberg | Blog | Projects
All Posts | 2025 | 2025-12 | 2025-12-15 | All Bibs | All Tags | #aoc

Maze Math Puzzles

Written by Rickard Lindberg .

Inspired by Advent of Code 2025, I wanted to make some kind of similar puzzle for my son. He usually takes some interest in me solving the problems. He even helped me program some solutions last year. But it mostly meant I told him what keys to press. Programming is a little hard. But he likes math.

So I came up with an idea to generate maze math problems.

Your task is to walk from start to end in a maze. You also carry a backpack with a number in it. That number is initially 1. When you cross another number on your way to the end, you add the number that you have in your backpack with the number you just encountered and put the result back in your backpack. When you cross an operator, it changes what operation you perform the next time you cross a number. The puzzle answer is the number that you have in your backpack when you reach the end.

Here is the first puzzle that I generated that I had my son solve:

#########################################
# S     #                               #
#####   #   #############################
#       #                               #
#   #####   #########################   #
#   #       #     4                     #
#   #########   #####################   #
#         *   2 # *                 # + #
#################   #########   #   #   #
#     3             #           #   #   #
#########   #########   #########   #   #
# 4     # *     #       #   #     + #   #
#   #   #########   #####   #   #####   #
#   #               #         4 # 3     #
#   #############################   #####
#     2                     #       # 2 #
#########################   #   #####   #
#     3               +     #     E     #
#   #################################   #
#                                       #
#########################################

I had to explain the rules a few times before he got it. But then he was able to solve it. It was both a little challenging to remember the number in the backpack and to remember what operation to perform. The mazes don't seem to be too hard to solve right now. Perhaps I have done something wrong in the generation code? There are often few choices that you can make.

Can you figure out the answer?

The program that generates the mazes also helpfully shows the answer and the path from start to end:

Answer = 11
#########################################
# S   . #                               #
#####   #   #############################
# .   . #                               #
#   #####   #########################   #
# . #       # .   4   .   .   .   .   . #
#   #########   #####################   #
# .   .   *   2 # *                 # + #
#################   #########   #   #   #
#     3             #           #   # . #
#########   #########   #########   #   #
# 4     # *     #       #   #     + # . #
#   #   #########   #####   #   #####   #
#   #               #         4 # 3   . #
#   #############################   #####
#     2                     # .   . # 2 #
#########################   #   #####   #
#     3               +     # .   E     #
#   #################################   #
#                                       #
#########################################

Right now only numbers 1, 2, 3, and 4 are used and operators + and *. We can easily make the problems harder by adding bigger numbers and more operators.

We can also get harder problems by generating mazes a few times at random. Here is one for example:

#########################################
# S       2 #                           #
#########   #   #########   #############
#       #   #   #       #     3         #
#   #   #   #####   #   #############   #
#   #   #   #       #         +   * # 4 #
#   #   #   #   #################   #   #
#   #   #       #               # 3 #   #
#   #################   #####   #   #   #
#         2         #   #       #       #
#############   #   #####   #   #####   #
#             3 # +   4     #   #       #
#   #############################   #####
#                               #       #
#############################   #####   #
#                 *         #   #     4 #
#   #####################   #   #   #####
#   # *                       + # E #   #
#   #############################   #   #
#                                 2     #
#########################################
Answer = 36
#########################################
# S   .   2 #                           #
#########   #   #########   #############
#       # . #   # .   . #     3         #
#   #   #   #####   #   #############   #
#   #   # . # .   . # .   .   +   * # 4 #
#   #   #   #   #################   #   #
#   #   # .   . #               # 3 #   #
#   #################   #####   #   #   #
#         2         #   #       # .   . #
#############   #   #####   #   #####   #
#             3 # +   4     #   # .   . #
#   #############################   #####
#                               # .   . #
#############################   #####   #
#                 *         #   # .   4 #
#   #####################   #   #   #####
#   # *                       + # E #   #
#   #############################   #   #
#                                 2     #
#########################################

And of course we can play around with larger size:

#################################################################################
# S       *     #                       #           #                       #   #
#############   #   #################   #   #####   #   #################   #   #
#   #           #           #               #           #       #           # + #
#   #   #################   #   #############################   #   #########   #
#       #                   #   # 4                         #   #   #           #
#   #############   #   #########   #####################   #   #   #########   #
# 3         #       #   #                   #               #       #         4 #
#########   #   #########   #################   #############   #####   #########
#       #   #                   #               #           #   #   #     4     #
#####   #   #####################   #############   #########   #   #   #####   #
#       #             *             #           #   #         3 #       #       #
#   #   #################################   #   #   #   #################   #####
#   #       #                               #       #           #       #       #
#   #####   #####################   #########################   #   #   #####   #
#   #       #           #       #       #                   #   #   #           #
#   #   #####   #####   #   #   #####   #   #####   #########   #   #############
#   #           #   #       #     E #       #       # *         #               #
#   #############   #############   #########   #####   #####################   #
#           #               #       #       # 2 #       #                       #
#########   #   #############   #####   #   #   #   #########   #################
#           #         +                 # 3     #   #       #             *     #
#   #####   #################################   #   #   #   #   #############   #
#   #       #                               #   #       #   #   #           #   #
#   #########   #########################   #############   #####   #####   #   #
#     *         #                           #           #           #   #       #
#   #############   #########################   #####   #############   #####   #
#   #               #                   #       #                 2         #   #
#####   #####   #####   #############   #   #####   #########   #############   #
#     3 #       #       #           #       #       #         4 #               #
#   #############   #####   #####   #########   #####   #########   #############
#             4     # 3     #   #             2 #   # +     #       #           #
#####################   #####   #################   #####   #   #####   #####   #
#                       #               #         2         #           #       #
#   #################################   #   #############################   #####
# 2                       + #           #                           #       #   #
#########################   #   #################################   #   #####   #
#                           #                                       #   #       #
#   #################################################################   #####   #
#                                                                             + #
#################################################################################
Answer = 50
#################################################################################
# S   .   *   . # .   .   .   .   .   . # .   .   . # .   .   .   .   .   . #   #
#############   #   #################   #   #####   #   #################   #   #
#   # .   .   . # .   .   . #         .   . #     .   . #       # .   .   . # + #
#   #   #################   #   #############################   #   #########   #
# .   . #         .   .   . #   # 4   .   .   .   .   .   . #   # . #           #
#   #############   #   #########   #####################   #   #   #########   #
# 3   .   . # .   . #   # .   .   .         # .   .   .   . # .   . #         4 #
#########   #   #########   #################   #############   #####   #########
#       # . # .   .   .   .     # .   .   .   . #           # . #   #     4     #
#####   #   #####################   #############   #########   #   #   #####   #
# .   . # .   .   .   *   .   .   . #           #   # .   .   3 #       #       #
#   #   #################################   #   #   #   #################   #####
# . # .   . #                               #       # .   .   . #       #       #
#   #####   #####################   #########################   #   #   #####   #
# . # .   . # .   .   . # .   . #       #                   # . #   #           #
#   #   #####   #####   #   #   #####   #   #####   #########   #   #############
# . # .   .   . #   # .   . # .   E #       #       # *   .   . #               #
#   #############   #############   #########   #####   #####################   #
# .   .   . #               #       #       # 2 # .   . #                       #
#########   #   #############   #####   #   #   #   #########   #################
# .   .   . #         +                 # 3     # . # .   . #             *     #
#   #####   #################################   #   #   #   #   #############   #
# . #       # .   .   .   .   .   .   .   . #   # .   . # . #   # .   .   . #   #
#   #########   #########################   #############   #####   #####   #   #
# .   *   .   . # .   .   .   .   .   .   . # .   .   . # .   .   . #   # .   . #
#   #############   #########################   #####   #############   #####   #
#   # .   .   .   . # .   .   .   .   . # .   . # .   .           2         # . #
#####   #####   #####   #############   #   #####   #########   #############   #
# .   3 #       # .   . # .   .   . # .   . # .   . #         4 # .   .   .   . #
#   #############   #####   #####   #########   #####   #########   #############
# .   .   .   4   . # 3   . #   # .   .   .   2 #   # +     # .   . # .   .   . #
#####################   #####   #################   #####   #   #####   #####   #
# .   .   .   .   .   . #               #         2         # .   .   . # .   . #
#   #################################   #   #############################   #####
# 2   .   .   .   .   .   + #           #                           # .   . #   #
#########################   #   #################################   #   #####   #
# .   .   .   .   .   .   . #                                       # . #       #
#   #################################################################   #####   #
# .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .       + #
#################################################################################

What links here?


Mentions:

What is Rickard working on and thinking about right now?

Every month I write a newsletter about just that. You will get updates about my current projects and thoughts about programming, and also get a chance to hit reply and interact with me. Subscribe to it below.

Powered by Buttondown (My Newsletter)

I'm Rickard Lindberg from Sweden. This is my home on the web. I like programming. I like both the craft of it and also to write software that solves problems. I also like running.

Me elsewhere: GitHub, Mastodon.