Advent Of Code Catchup Journey in C++ (Day 1)

/H2 I spent the summer taking an intense course in IBM HLASM, letting my C++/Javascript fall by the wayside.

That's what I was staring at/working with for the past few months. I absolutely loved working in assembler, but it's time to get back to more normal work to prepare for my final two semesters of work in primarily C++ and working with RCC in Javascript among others.

So, I've decided to go back and do some Advent of Code puzzles that I haven't finished. I finished all of 2015, and am starting 2017 this evening. NOTE This is spoilery.

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

There's the problem for day 1. I'm working with C++ at present. My solution to part 1 was to initialize two location counters, called ptr1 and ptr2.

ptr1 keeps track of the 'first' of a batch of digits, ptr2 keeps advancing until the digits don't match, adding to the sum if matches are found. Then ptr1 is advanced to where ptr2 was, and the next batch is computed. It's pretty simple, but it works. I could've done this with iterators, and string streams, but this seemed simple and effective. Then for part 2, a simple check of each digit uses the two ptrs. I did them as separate sections of the program below.

int main(int argc, char** argv)
{
    {
    std::ifstream input_file;
    input_file.open("day1input.txt");
    std::string line_buffer;
    while (std::getline(input_file, line_buffer))
    {
        size_t ptr1 = 0;
        size_t ptr2 = 0;
        long sum = 0;

        while (ptr1 < line_buffer.length())
        {
            while (line_buffer[ptr2] == line_buffer[ptr1] && ptr2 < line_buffer.length())
            {
                std::string substr = line_buffer.substr(ptr2, 1);
                sum += stoi(substr);
                ptr2++;
                ptr2 > line_buffer.length() - 1? ptr2 = line_buffer.length()-1:ptr2 = ptr2;
            }
            ptr1 = ptr2;
            ptr2 = ptr1 + 1;
        }
        std::cout << sum << std::endl;
        }
    }
    return 0;  
}

Simple and effective. But it's fun to think of other ways this could be done: recursion seems possible, as well as using a stringstream format, or simply converting your ptrs to interators and comparing that way. But I generally keep it simple doing Advent of Code. Hell you could even make a digit class that has a value, a pointer to a neighboring digit, and a bool that says whether it's counted or not and run from that. Probably the 'simplest' OOP method and the kind of thing I'm always tempted to implement tbh, but it's very wasteful.

For part 2, I swapped to a for-loop for iterating through the string one char at a time, making J point ahead the value of advancement_value, and making the string wrap around. Worked first try, yay.

int main(int argc, char** argv)
{
    {
        std::ifstream input_file;
        input_file.open("day1input.txt");
        std::string line_buffer;
        while (std::getline(input_file, line_buffer))
        {
            long sum = 0;
            const int advancement_value = line_buffer.length() / 2;
            for (int i = 0; i < line_buffer.length(); i++)
            {
                std::string num1 = line_buffer.substr(i, 1);
                int j = i + advancement_value;
                if (j > line_buffer.length()) {j -= line_buffer.length();}
                if (line_buffer[i] == line_buffer[j])
                {
                    sum += stoi(num1);
                }
            }
        std::cout << sum << std::endl;  
        }
    }
    return 0;  
}

These early days are fun and simple and are a great way to brush up on a language, use another one to learn, or challenge yourself entirely (I was even thinking of trying to do it in HLASM, but no thanks!)

Feel free to respond with optimizations, or other methods to achieve the result. As always, I'm documenting this as documenting my learning, but hopefully it can help somebody else out too. Thanks for reading! And I highly recommend signing up for Advent Of Code over at https://adventofcode.com/ During December I'll hopefully be caught up and on to working on the advent calendar as it's released! It's a blast.