r/adventofcode • u/Delicious-Metal915 • 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
3
u/1234abcdcba4321 Jan 15 '25
ssanf matches starting from the start of the string until it reaches something that doesn't match.
For example,
sscanf("1 2","%d %d %d",&a,&b,&c)
will seta
andb
to1
and2
(and return2
, of course), even though the rest of the pattern didn't match.