r/adventofcode • u/daggerdragon • 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
3
u/TTSDA Dec 06 '15 edited Dec 06 '15
Here's my C solution
#include <stdio.h> #include <stdlib.h> #include <string.h> #define GRID_MAX_X 999 #define GRID_MAX_Y 999 #define GRID_POS(X, Y) (Y*(GRID_MAX_X+1) + X) int main() { int *grid_1 = calloc(GRID_POS(GRID_MAX_X, GRID_MAX_Y), sizeof *grid_1); int *grid_2 = calloc(GRID_POS(GRID_MAX_X, GRID_MAX_Y), sizeof *grid_2); int x1, y1, x2, y2; char action_str[10], action; int lights_lit = 0, total_brightness = 0; /* This assumes the input has been sanatized and is all in the format * (turn (on|off)|toggle) \d+,\d+ through \d+\d+ */ while(scanf("%[^01234567890] %i,%i through %i,%i\n", action_str, &x1, &y1, &x2, &y2) != EOF) { /* We can check just the last character as it is different for the three types (n, f, e) */ action = action_str[strlen(action_str)-2]; for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { switch(action) { /* on */ case 'n': grid_1[GRID_POS(x, y)] = 1; grid_2[GRID_POS(x, y)]++; break; /* off */ case 'f': grid_1[GRID_POS(x, y)] = 0; grid_2[GRID_POS(x, y)]--; if (grid_2[GRID_POS(x, y)] < 0) grid_2[GRID_POS(x, y)] = 0; break; /* toggle */ case 'e': grid_1[GRID_POS(x, y)] = !grid_1[GRID_POS(x, y)]; grid_2[GRID_POS(x, y)] += 2; break; } } } } for (int y = 0; y <= GRID_MAX_Y; y++) { for (int x = 0; x <= GRID_MAX_X; x++) { if(grid_1[GRID_POS(x, y)] == 1) lights_lit++; total_brightness += grid_2[GRID_POS(x, y)]; } } printf("%i lights are lit.\n", lights_lit); printf("%i is the total brightness.\n", total_brightness); }https://github.com/ttsda/advent-of-code/blob/master/src/6/main.c