aoc-2019-c

Advent of Code 2019 Solutions in C
git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

main.c (1485B)


      1#include "aoc.h"
      2#include "util.h"
      3
      4#include <assert.h>
      5#include <stdio.h>
      6#include <string.h>
      7#include <stdlib.h>
      8
      9const size_t width = 25;
     10const size_t height = 6;
     11
     12const char *sol2 = "\
     13#  # ####  ##  #### #  # \n\
     14#  #    # #  #    # #  # \n\
     15####   #  #      #  #  # \n\
     16#  #  #   #     #   #  # \n\
     17#  # #    #  # #    #  # \n\
     18#  # ####  ##  ####  ##  \n\
     19";
     20
     21void
     22part1(void)
     23{
     24	size_t i, k;
     25	size_t count[3];
     26	int minz = -1, res;
     27
     28	i = 0;
     29	while (aoc.input[i] != '\n' && i < aoc.input_size) {
     30		memset(count, 0, sizeof(count));
     31		for (k = 0; k < width * height; k++)
     32			count[aoc.input[i+k] - '0'] += 1;
     33		if (minz == -1 || minz > (int) count[0]) {
     34			minz = (int) count[0];
     35			res = (int) (count[1] * count[2]);
     36		}
     37		i += k;
     38	}
     39
     40	assert(minz != -1);
     41
     42	aoc.answer = aprintf("%i", res);
     43	aoc.solution = "2016";
     44}
     45
     46void
     47part2(void)
     48{
     49	size_t i, k;
     50	int img[width * height];
     51	char *str;
     52
     53	for (k = 0; k < width * height; k++) {
     54		img[k] = 2;
     55		for (i = 0; aoc.input[i * width * height] != '\n'; i++) {
     56			if (aoc.input[i * width * height + k] != '2') {
     57				img[k] = aoc.input[i * width * height + k] - '0';
     58				break;
     59			}
     60		}
     61	}
     62
     63	str = malloc((width + 1) * height + 1);
     64	assert(str != NULL);
     65	for (i = 0; i < height; i++) {
     66		for (k = 0; k < width; k++) {
     67			assert(img[i * width + k] != 2);
     68			str[i * (width + 1) + k] = img[i * width + k] ? '#' : ' ';
     69		}
     70		str[i * (width + 1) + k] = '\n';
     71	}
     72	str[(width + 1) * height] = '\0';
     73
     74	aoc.answer = str;
     75	aoc.solution = sol2;
     76
     77}