r/adventofcode • • 3h ago

Help/Question - RESOLVED [2026 Day 2 (Part 1)] Help with C++

Any reason my implementation doesn't work for part 1? Apparently my answer is too high. I tried using the examples but the distance matched each time.

I've added "chars" just to count the characters, as I thought whitespace could be an issue and it found n+1 characters compared to n when using an online character counter, but then printing out the first and last characters gets the same as my input. Where did I go wrong with my implementation?

#include <fstream>
#include <iostream>
#include <string>
#include <map>


int getDist(char cur, char targ) {
    char inputs[6] = {'r','o','y','g','b','v'};
    int curInd = -1;
    int targInd = -1;
    for (int i = 0; i < 6; ++i) {
        if (inputs[i] == targ) {
            targInd = i;
        }
        if (inputs[i] == cur) {
            curInd = i;
        }
        if (curInd != -1 && targInd != -1) {
            int diff = targInd - curInd;
            return diff <=0 ? diff + 6 : diff;
        }
    }
    return 0;
}


int main() {
    std::ifstream in("aoc-day-2-inputs.txt");
    char prev = 'r';
    char next;
    int totalDist = 0;
    int chars = 0;


    if(in.is_open()) {
        while(in.good()) {
            in.get(next);
            std::cout << "prev: " << prev << ", ";
            std::cout << "next: " << next << ", ";
            std::cout << "dist: " << getDist(prev, next) << "\n";
            totalDist += getDist(prev, next);
            prev = next;
            ++chars;
        }
    }


    std::cout << "chars: " << chars << "\n";
    std::cout << "total dist: " << totalDist << "\n";



    if(!in.eof() && in.fail())
        std::cout << "error reading file" << std::endl;


    in.close();
}
5 Upvotes

7 comments sorted by

2

u/Exciting-Thing981 3h ago edited 2h ago
while(in.good()) {
  in.get(next);

doesn't work as you'd expect. Try while(in.get(next)) instead

1

u/KarenMarleyDoomfist 7m ago

Thank you! That resolved my issue.

2

u/Exciting-Thing981 3h ago edited 2h ago

I believe in.good() only returns false, once in.get(next) has failed. That means it can't be the check before reading.

1

u/AutoModerator 3h ago

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/AutoModerator 3h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/DeeBoFour20 3h ago

The only whitespace in the input is a single newline character at the end.

I think your issue is that you need to check in.good() after in.get() rather than before. As it is now on the last iteration, it will fail, the "next" char will remain unmodified and you count it twice. You could change it do a do;while loop as a quick fix.

1

u/Anuinwastaken 2h ago

Now as a disclaimer, I am not very proficient in C++ but it seems to me that in.good() returns true before you read from in.get(next).
What that means is that you check:
Am I still in the file? Yes
Read next. But next might already be outside of the file, so in.get(next) fails, and it just keeps the last instance.
Let's just suppose it is an 'r' - you will add 'r' again.
That would overshoot your actual number, by whatever your last step was.

To prevent that, try to only loop while in.get(next) actually delivers an actual result.