summaryrefslogtreecommitdiffstats
path: root/6/1.c
blob: 2a8504ef30cb58ab22ef6ab8d749bfcb4d1c1788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

enum {
	WIDTH = 1000,
	HEIGHT = 1000,
};

struct instr {
	enum action {
		A_OFF,
		A_ON,
		A_TOGGLE,
	} act;
	struct pos {
		unsigned short x, y;
	} start, end;
};

unsigned short read_be16(void *_buf)
{
	unsigned char *buf = _buf;
	return (buf[0] << 8) | buf[1];
}

bool read_instr(struct instr *i, FILE *f)
{
	unsigned char buf[9];
	unsigned short v;

	assert(i != NULL);
	assert(f != NULL);

	if (fread(buf, sizeof buf, 1, f) != 1)
		return false;
	switch (buf[0]) {
	case 0xff: i->act = A_OFF; break;
	case 1: i->act = A_ON; break;
	case 2: i->act = A_TOGGLE; break;
	default: return false;
	}

#define SET(f, p) { \
		v = read_be16(&buf[p]); \
		if (v > 999) \
			return false; \
		i->f = v; \
	}
	SET(start.x, 1);
	SET(start.y, 3);
	SET(end.x, 5);
	SET(end.y, 7);

	return true;
}

int main(void)
{
	char *map[] = { "turn off", "turn on", "toggle" };
	bool screen[WIDTH][HEIGHT] = { 0 };
	struct instr i;
	int count = 0;
	FILE *f;

	f = fopen("inputbin", "rb");
	if (f == NULL)
		exit(EXIT_FAILURE);

	while (read_instr(&i, f)) {
		for (int y = i.start.y; y <= i.end.y; y++) {
			for (int x = i.start.x; x <= i.end.x; x++) {
				switch (i.act) {
				case A_OFF: screen[x][y] = false; break;
				case A_ON: screen[x][y] = true; break;
				case A_TOGGLE:
					screen[x][y] = !screen[x][y];
					break;
				}
			}
		}
	}

	fclose(f);

	for (int y = 0; y < HEIGHT; y++)
		for (int x = 0; x < WIDTH; x++)
			count += screen[x][y];

	printf("%d\n", count);
}