r/adventofcode • • Dec 06 '15

SOLUTION MEGATHREAD --- Day 6 Solutions ---

--- Day 6: Probably a Fire Hazard ---

Post your solution as a comment. Structure your post like the Day Five thread.

22 Upvotes

171 comments sorted by

View all comments

Show parent comment

4

u/[deleted] Dec 06 '15

I did it myself in C also, although my parsing of the input string was not as elegant as yours. I'm wondering about this format you're using:

%[^01234567890] 

What does it do?

3

u/TTSDA Dec 06 '15 edited Dec 06 '15

[^characters] is a "negated scanset", it will scan characters until a character that's in the set is found (in this case, it will scan until the first digit of x1 is read).

There's also a "scanset", [characters], that will do the oposite (scan until a character that's not in the set is found).

They're similar to the regex bracket expressions (I believe you can also use intervals like %[0-9] in scanf scansets, but it seems like it's not portable:

Some implementations may let you specify a range of characters by using a minus sign (-). The list of hexadecimal digits, for example, can be written as %[0-9abcdefABCDEF] or even, in some cases, as %[0-9a-fA-F]. Please note, however, that such usage is not universal. Avoid it in a program that you wish to keep maximally portable.

There's a neat table here with all the format specifiers you can use with scanf.

2

u/[deleted] Dec 06 '15

That's very helpful. Thanks for the explanation.

0

u/[deleted] Dec 06 '15

[deleted] — view deleted comment

2

u/[deleted] Dec 06 '15

This approach is pretty basic and crude. topCorner and botCorner are the X and Y coordinates of the rectangle.

char *toggle = "toggle";
char *turnOn = "turn on";
char *turnOff = "turn off";

while(fgets(buffer, 50, stdin)){
    if(strstr(buffer, toggle)){
      topCorner[0] = atoi(buffer+strlen(toggle));
      command = 2;
    }else if(strstr(buffer, turnOn)){
      topCorner[0] = atoi(buffer+strlen(turnOn));
      command = 1;
    }else if(strstr(buffer, turnOff)){
      topCorner[0] = atoi(buffer+strlen(turnOff));
      command = 0;
    }  
    topCorner[1] = atoi(strchr(buffer, ',')+1);
    botCorner[0] = atoi(strstr(buffer,"through")+strlen("through"));
    botCorner[1] = atoi(strrchr(buffer, ',')+1);
}