summary refs log tree commit diff homepage
path: root/2019/day08.c
blob: 4ddbc60db6167b6941921e2f4bf72ca926b9b45d (plain) (blame)
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
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
	char layers[128][6][25];
	size_t len = fread(layers, sizeof(layers[0]), 128, stdin);
	int min, min0 = INT_MAX;
	for (int l = 0; l < len; ++l) {
		int zero = 0;
		for (int y = 0; y < 6; ++y) {
			for (int x = 0; x < 25; ++x) {
				if (layers[l][y][x] == '0') zero++;
			}
		}
		if (zero < min0) {
			min = l;
			min0 = zero;
		}
	}
	int one = 0, two = 0;
	for (int y = 0; y < 6; ++y) {
		for (int x = 0; x < 25; ++x) {
			if (layers[min][y][x] == '1') one++;
			if (layers[min][y][x] == '2') two++;
		}
	}
	printf("%d\n", one * two);

	char img[6][25];
	for (size_t l = len - 1; l < len; --l) {
		for (int y = 0; y < 6; ++y) {
			for (int x = 0; x < 25; ++x) {
				switch (layers[l][y][x]) {
					break; case '0': img[y][x] = '.';
					break; case '1': img[y][x] = '#';
				}
			}
		}
	}
	for (int y = 0; y < 6; ++y) {
		printf("%.*s\n", 25, img[y]);
	}
}