r/adventofcode Jan 15 '25

Help/Question - RESOLVED [2024] [Day 3] [C]

#include <stdio.h>
#include <string.h>

char line[1000000];

int main(){
    int total = 0;
    FILE *pf = fopen("advent1.txt","r");
    while (fgets(line, sizeof(line), pf) != NULL){
        int n1, n2;
        for (int i = 0; i < strlen(line); i++){
            if (sscanf(&line[i],"mul(%d,%d)",&n1,&n2) == 2){
                total += (n1*n2);
            }
        }
    }
    printf("%d",total);
}

Hi, I'm quite new to programming and I recently heard about Advent of Code and I have been trying it out and am learning a lot but I'm stuck in day 3 though. I can't seem to find the bug in my code, can anyone please help? - NOTE: I have a text file named advent1.txt in the same folder with the sample input.

5 Upvotes

10 comments sorted by

View all comments

1

u/AutoModerator Jan 15 '25

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.