• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Advent Of Code 2020 – Day 3 – Toboggan Trajectory

December 12, 2020 by varunshrivastava Leave a Comment

Great job reaching here so far. Finally, we’ve logged in to Toboggan successfully, now we have to travel to the airport through a jungle. The path is not very good and has a lot of trees. So, here comes our next task.

The trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (.) and trees (#) you can see. For example:

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

On further reading you will find that the given pattern is repeating.

..##.........##.........##.........##.........##.........##.......  --->
#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....  --->
.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
.#........#.#........#.#........#.#........#.#........#.#........#
#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
#...##....##...##....##...##....##...##....##...##....##...##....#
.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#  --->

Here’s the task that we have to perform.

You start on the open square (.) in the top-left corner and need to reach the bottom (below the bottom-most row on your map).

The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by counting all the trees you would encounter for the slope right 3, down 1:

From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map.

From the start till the end, how many trees will you encounter?

Part One – Count the trees in the path

The idea is to consider the input area as a 2-d plane. Let’s call it frame.

So, to read the input from the file and create a frame, we can use the following logic:

    ifstream day_3_input("inputs/day3.txt");
    string line;

    vector<vector<char>> frame;
    while (getline(day_3_input, line)) {
        vector<char> col;
        for (int j=0; j < line.length(); j++) {
            col.push_back(line[j]);
        }
        frame.push_back(col);
    }

The above code will create a 2d frame for us to work with.

Next, we need to define our trajectory/path through which we will be going through. As we know that toboggan moves one space down and then 3 spaces right, in plotting the same on a graph we get a slope. This slope is our path. So, let’s define a variable slopes. This will contain the slope of our path.

slopes = { {3,1} } // here we move 3 spaces to right and one space down

So far so good. We will see in a minute how the slopes are being used here.

Let’s start our journey with the given slope.

    for (auto slope: slopes) {
        int x = slope[1], y = slope[0];
    }

This above piece of code will give us the path on which we have to walk. So, we now have the code to read the slope from the array and tell us how to move but let’s write a little more code to actually move the toboggan.

    for (auto slope: slopes) {
        int x = slope[1], y = slope[0];
        while (x < frame.size()) {
            y += slope[0];
            x += slope[1];
        }
    }

Now, we are actually moving.

In the above code, we are instructing toboggan to move until we cross the jungle. And at each step, move down by slope[0] and then move straight by slope[1].

But wait, the solution for our problem was not to move the toboggan but also to count the trees that we encounter in our path. So, let’s quickly write some code for that. We’ve done the hard part, it’s just the matter of counting the trees now.

Let’s hire someone to count the trees on the path.

Here we know that whenever we find a # on our path then we have to increment the count. Because # represents a tree.

    for (auto slope: slopes) {
        count = 0;
        int x = slope[1], y = slope[0];
        while (x < frame.size()) {
            if (frame[x][y % frame[x].size()] == '#') count++;
            y += slope[0];
            x += slope[1];
        }
        cout << count << endl;
    }

Let’s run the code and see if we get the correct answer.

Viola!!! Got the right answer.

Let’s move to the part 2 of the problem.

Part 2 – Multiply the total trees encountered on each slope

This part only extends the above problem by adding more slopes. We already wrote the code in a way that is extensible.

The only change that we have to make in the existing code is to add the additional slopes to the vector and maintain the multiplication of all the trees that we encounter at each slope.

Let’s directly jump to the code:

    vector<vector<int>> slopes =  { {1,1}, {3,1}, {5,1}, {7,1}, {1,2} };
    int mul = 1;
    
    for (auto slope: slopes) {
        int count = 0;
        int x = slope[1], y = slope[0];
        while (x < frame.size()) {
            if (frame[x][y % frame[x].size()] == '#') count++;
            y += slope[0];
            x += slope[1];
        }

        cout << count << "," << endl;
        mul *= count;
    }
    

    cout << mul;

Let’s run this code again and see if we get the right answer or not.

Yes, we get the right answer. The above code solves the given problem.

I hope you learned something new from this exercise. Let me know your thoughts in the comments below.

Related

Filed Under: Programming Tagged With: aoc-2020, day-3, problem-solving, toboggan-trajectory

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version