3

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?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Naslausky
  • 3,443
  • 1
  • 14
  • 24
  • 4
    Try using `%d` instead. `%i` will interpret a number with a leading zero as octal. And since 9 is not an octal digit, bad stuff happens. – user3386109 Dec 05 '18 at 01:16
  • 1
    Thanks! That worked. I knew it was something silly, but couldn't find it anywhere else. – Naslausky Dec 05 '18 at 01:26
  • 1
    `linha++`? That is *not* the code you are using because *that* code won't even compile. – paxdiablo Dec 05 '18 at 01:27
  • That is an old timestamp [1518 11 01 00 00 10](https://en.wikipedia.org/wiki/1518). – chux - Reinstate Monica Dec 05 '18 at 04:35
  • I fixed the "linha++" to "line++". I just translated the code for better understanding. Regarding the old timestamp, its correct. The puzzle refers to old timestamps, its a fictional problem. The question, however, has been solved. It was something silly, said in the comments. Thank you for all your help. – Naslausky Dec 06 '18 at 14:32

1 Answers1

2

Change this:

scanf("%i %i %i %i %i %i",&year, &month,&day,&hour,&minute, &action);

to this:

scanf("%d %d %d %d %d %d", &year, &month, &day, &hour, &minute, &action);

The format specifier i will interpret a number with a leading zero as octal. And since 9 is not an octal digit, it will be unable to process it, as @user3386109 said.

For printing, it doesn't really matter which one you choose, but I would go with %d, in order to be homogeneous.

Read more in What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)?

gsamaras
  • 71,951
  • 46
  • 188
  • 305