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

part1.c (2473B)


      1#include "allocator.h"
      2#include "aoc.h"
      3#include "dvec_s.h"
      4#include "util.h"
      5
      6#include <assert.h>
      7#include <stdlib.h>
      8
      9static void
     10deck_cut(struct dvec *deck, struct dvec *table, ssize_t in)
     11{
     12	int *src, *dst;
     13	size_t i, split, n;
     14
     15	n = deck->len;
     16	split = in < 0 ? (size_t) ((ssize_t) deck->len + in) : (size_t) in;
     17	src = deck->data;
     18	dst = table->data;
     19
     20	for (i = 0; i < split; i++)
     21		dst[i + n - split] = src[i];
     22	for (i = split; i < n; i++)
     23		dst[i - split] = src[i];
     24
     25	dvec_swap(deck, table);
     26}
     27
     28static void
     29deal_increment(struct dvec *deck, struct dvec *table, size_t inc)
     30{
     31	int *src, *dst;
     32	size_t i, n, p;
     33
     34	n = deck->len;
     35	src = deck->data;
     36	dst = table->data;
     37
     38	p = 0;
     39	for (i = 0; i < n; i++) {
     40		dst[p] = src[i];
     41		p = (p + inc) % n;
     42	}
     43
     44	dvec_swap(deck, table);
     45}
     46
     47static void
     48deal_new_stack(struct dvec *deck, struct dvec *table)
     49{
     50	int *src, *dst;
     51	size_t i, n;
     52
     53	n = deck->len - 1;
     54	src = deck->data;
     55	dst = table->data;
     56
     57	for (i = 0; i <= n; i++)
     58		dst[n - i] = src[i];
     59
     60	dvec_swap(deck, table);
     61}
     62
     63static size_t
     64find_card(struct dvec *deck, int card)
     65{
     66	int *data;
     67	size_t i, n;
     68
     69	n = deck->len;
     70	data = deck->data;
     71	for (i = 0; i < n; i++) {
     72		if (data[i] == card)
     73			return i;
     74	}
     75	return 0;
     76}
     77
     78void
     79part1(void)
     80{
     81	char line[256];
     82	const char *lpos, *lend;
     83	struct dvec deck, table;
     84	size_t i, count;
     85	size_t answer;
     86	ssize_t val;
     87	int target;
     88	int *data;
     89
     90	if (!strcmp(aoc.inputfile, "input")) {
     91		count = 10007;
     92		target = 2019;
     93	} else {
     94		count = 10;
     95		target = 9;
     96	}
     97
     98	dvec_init(&deck, sizeof(int), count, &stdlib_strict_heap_allocator);
     99	dvec_init(&table, sizeof(int), count, &stdlib_strict_heap_allocator);
    100
    101	dvec_add_slots(&deck, count);
    102	dvec_add_slots(&table, count);
    103	data = deck.data;
    104	for (i = 0; i < count; i++)
    105		data[i] = (int) i;
    106
    107	lpos = aoc.input;
    108	lend = lpos + aoc.input_size;
    109	while (readtok(line, sizeof(line), '\n', &lpos, lend)) {
    110		if (sscanf(line, "deal with increment %li", &val) == 1) {
    111			deal_increment(&deck, &table, (size_t) val);
    112		} else if (sscanf(line, "cut %li", &val) == 1) {
    113			deck_cut(&deck, &table, val);
    114		} else if (!strcmp(line, "deal into new stack")) {
    115			deal_new_stack(&deck, &table);
    116		} else if (strspn(line, " \n\r\t\v") == strlen(line)) {
    117			continue;
    118		} else {
    119			assert(false);
    120		}
    121		aoc_debug("%i pos: %lu\n", target, find_card(&deck, target));
    122	}
    123
    124	answer = find_card(&deck, target);
    125	aoc.answer = aprintf("%lu", answer);
    126	aoc.solution = "7171";
    127
    128	dvec_deinit(&table);
    129	dvec_deinit(&deck);
    130}