aboutsummaryrefslogtreecommitdiffstats
path: root/src/08/main.c
blob: 7d4d9aa18eb3d6daa2a306745afd3d370dcb4508 (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
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
#include "aoc.h"
#include "util.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const size_t width = 25;
const size_t height = 6;

const char *sol2 = "\
#  # ####  ##  #### #  # \n\
#  #    # #  #    # #  # \n\
####   #  #      #  #  # \n\
#  #  #   #     #   #  # \n\
#  # #    #  # #    #  # \n\
#  # ####  ##  ####  ##  \n\
";

void
part1(void)
{
	size_t i, k;
	size_t count[3];
	int minz = -1, res;

	i = 0;
	while (aoc.input[i] != '\n' && i < aoc.input_size) {
		memset(count, 0, sizeof(count));
		for (k = 0; k < width * height; k++)
			count[aoc.input[i+k] - '0'] += 1;
		if (minz == -1 || minz > (int) count[0]) {
			minz = (int) count[0];
			res = (int) (count[1] * count[2]);
		}
		i += k;
	}

	assert(minz != -1);

	aoc.answer = aprintf("%i", res);
	aoc.solution = "2016";
}

void
part2(void)
{
	size_t i, k;
	int img[width * height];
	char *str;

	for (k = 0; k < width * height; k++) {
		img[k] = 2;
		for (i = 0; aoc.input[i * width * height] != '\n'; i++) {
			if (aoc.input[i * width * height + k] != '2') {
				img[k] = aoc.input[i * width * height + k] - '0';
				break;
			}
		}
	}

	str = malloc((width + 1) * height + 1);
	assert(str != NULL);
	for (i = 0; i < height; i++) {
		for (k = 0; k < width; k++) {
			assert(img[i * width + k] != 2);
			str[i * (width + 1) + k] = img[i * width + k] ? '#' : ' ';
		}
		str[i * (width + 1) + k] = '\n';
	}
	str[(width + 1) * height] = '\0';

	aoc.answer = str;
	aoc.solution = sol2;

}