Advent of Code (Challenge)— Day 3 of December 2020

So I just discovered adventofcode.com and I am really digging the challenges. I did the first two of this month without much issue but the 3rd day was a bit of a challenge for me. So this blog will break down my solution. I finally got the answer after a couple of attempts.

The Problem

The title of this challenge is the “Toboggan Trajectory”. You are given text made up of characters “.” which are clear spaces, and “#” which are trees. Here is part of the text provided.

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

The full text is many more rows and the challenge also states that the provided text is the first part of the pattern. Each row repeats its pattern x number of times, though it is our task to do this. For example, if we repeat the first row once to the right we get.

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

You are provided move types so that you can traverse your extended map. For example, the first move type is move right 3 and move down 1. You are supposed to traverse this map using this single move type and count the trees (#) that you interact with at the end of each move/iteration. So for example, using this move type, the down part of the move doesn’t interact with the first tree till row 7.

Part 1

Part 1 asks you to provide the total number of trees that you interact with based on move type right 3/down 1. This should be done till you exhaust the bottom of the map, meaning you need to repeat the patterns to the right enough for this to happen.

I placed the original text in file mytext.txt and create a python file to work in. I import two libraries I plan on using. Both of these libraries will be used in part 2.

We create a function get_tree_count and it takes in arguments data (list of rows), right (number of spaces to move right) and down (number of spaces to move down). We have two variables in the function body. The count variable will increment by 1 when we interact with a tree. The num variable keeps track of how far right we are. We don’t have to keep track of how far down we are as that is done naturally by the loop itself.

Since we are concerned with the end of each move (after the down part of the move) and whether or not it interacts with a tree, we simply start iterating data on the row equal to the down variable, which is why we set num=right so that we start off in the correct rightward position. So if down=1, the iteration starts on the second row (remember 0 indexing) and since num=3 we are starting exactly at the end of the first move.

For each iteration (each row) we check to see if there is a tree in the current rightward position. If there is we add it to the count tally. Then we increment the num variable by the “right” amount”. We do this till we exhaust the rows and then return a value.

After the function definition we read in the text and then split it so that we get a list of rows. Next I arbitrarily repeated the pattern of each row 100 times. We call the function with the list and the move type values and get the number 178, which was the correct answer.

Part 2

The next part asks us to repeat part 1 for various move types:

  • right-1/down-1
  • right-3/down-1 (this was part 1)
  • right-5/down-1
  • right-7/down-1
  • right-1/down-2

Then it asks us to multiply these results and provide the answer.

I am a fan of the named tuple. It allows us to nicely organize things as a class would. So we set up a named tuple with right and down and then create a list over moves based on the challenge instructions.

We then loop over the moves and execute the function for each. Each move type is similar in that the down move is of step 1, with the exception of the last move. The function accounts for this with the step value of the list being set to the down value.

Lastly, we multiply the results with the reduce function. The outputs I got where [78, 178, 75, 86, 39] and the final answer was 3492520200, which was correct.

Senior Data Analyst at a Fortune 250. Passionate about developing data-driven apps and automated data solutions. My resume site is at www.thepythongeek.com.