Advent of Code 2017 Catchup Journey in C++ (Day 2)

Nested FOR loops... gross or practical? You decide! (Spoiler alert they're both)

https://adventofcode.com/2017/day/2

For part 1 of this challenge, I wrote a simple min/max checker algo, you've probably run into this in CS classes or any number of tutorials. I just used a std::stringstream to spit the numbers into variables.

int main(int argc, char** argv)
{
    std::ifstream input_file;
    input_file.open("day2input.txt");
    std::string line_buffer;
    int checksum = 0;
    while (std::getline(input_file, line_buffer))
    {
        std::stringstream ss(line_buffer);
        int min, max, cur;
        min = 9999;
        max = 0;
        while (ss >> cur)
        {
            if (cur < min)
            {
                min = cur;
            }
            if (cur > max)
            {
                max = cur;
            }
        }
        checksum += (max - min);
    }
    std::cout << checksum << std::endl;
    return 0;   
}

See how it works? The while loop works while there's still info in the stringstream to be placed into the 'cur' or current number. The only difference here between a normal min or max algo is that you have to check for min AND max. Then we simply add the checksum and bikkity boom, we good.

Part 2

Part 2 was a bit trickier. I originally implemented it using iterators but I feel like the for-loop version is a bit easier to understand, hence why it made this blog. First I wrote a function, "line_solution()" that returns an int. It's -1 if the current two digits are not divisible evenly by each other, and it's the solution to the largest divided by the smallest if the numbers play nice with each other:

int line_solution(int dig1, int dig2)
{
    int return_num = -1;
    if (dig1 >= dig2 && dig1 % dig2 == 0)
    {
        return_num = dig1/dig2;
    }
    if (dig2 >= dig1 && dig2 % dig1 == 0)
    {
        return_num = dig2/dig1;
    }
    return return_num;
}

I'm sure there's a 'slicker' way to do this, but I'm not going for slick.

One thing this blog is going to demonstrate is my coding style: the big thing is that the code is easy to read, and easy to understand: I want a job when I graduate, not to be a celebrated genius that nobody can work with! So I try to keep my coding simple for the reader.
Here's the main driver program

int main(int argc, char** argv)
{
    std::ifstream input_file;
    input_file.open("day2input.txt");
    std::string line_buffer;
    std::vector<int> line_ints;
    int checksum = 0;
    while (std::getline(input_file, line_buffer))
    {
        std::stringstream ss(line_buffer);
        int cur;
        while (ss >> cur)
        {
            line_ints.push_back(cur);
        }
        for (size_t i = 0; i < line_ints.size(); i++)
        {
            int num1 = line_ints[i];
            for (size_t j = i; j < line_ints.size(); j++)
            {
                int num2 = line_ints[j];
                if (line_solution(num1, num2) != -1 && i != j)
                {
                    checksum += line_solution(num1, num2);
                    std::cout << line_solution(num1, num2) << std::endl;
                }
            }
        }    
        line_ints.clear();
    }
    std::cout << checksum << std::endl;
    return 0;   
}

I could've handled the input a number of ways: what I'm doing is using the stringstream from earlier and pushing it into a vector. (Note that I'm clearing the vector at the end of the loop, this is important) The vector starts empty, then every line it builds from the beginning of the line to the end, then clears at the end.

I then do some nested for loops: this always feels gross, but I'm looking for the simplest solution to these problems, not necessarily the fastest or the slickest. I set 'i' to the first number to check, and 'j' to the second number to check. If line_solution declares that these numbers are compatible, I add them to checksum and move on.

Another simple challenge, another simple solution. As I said, my first solution involved iterators, which requires more of an understanding of the C++ STL, but I wanted to be more generic and explicit in my offerings.

Day 2 down! I can't wait until these get more and more challenging. Until then, code graciously.