I've been trying to solve the Advent of Code 2018 Day 4 puzzle.
I'm struggling a lot with reading the problem input and even though I've managed to work around it, I still would like to understand what was the cause of my issue.
Each line of the input has a timestamp and an event, like the following example:
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
The real input had more than 1000 lines.
To make things easier, I've manipulated this input to contain only numbers, with "1" and "2" representing sleeping and waking up, etc.
With the same example from above, I got:
1518 11 01 00 00 10
1518 11 01 00 05 1
1518 11 01 00 25 2
I've checked before and there is no guard number 1 or 2. My reading code follows, written in C:
#include <stdio.h>
#include <string.h>
int main () {
int line=0;
int year, month, day, hour,minute, action=0;
while (line<1110){
scanf("%i %i %i %i %i %i",&year, &month,&day,&hour,&minute, &action);
printf("%i %i %i %i %i %i\n",year, month,day,hour,minute, action);
}
line++;
}
return 0;
For some reason, after 27 correctly read lines, the variables started not being stored correctly:
1518 2 17 23 56 1297 //last correct line read
1518 2 18 0 0 9 //First incorrect line read
1518 2 18 00 09 -1 //What the first incorrect line should be.
I made it work eventually by removing all initial zeros (i.e. replacing 00 for 0, 01 for 1 and so on...) but it didnt make any sense since there were values with initial zeros on the first lines which were read correctly.
I've tried to use fscanf and fgets with sscanf, but both of the alternative ways had the same result.
I looked for this question a lot on the Internet but could not find a good explanation.
Can someone help me understand what was the problem?