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 (1541B)


      1#include "aoc.h"
      2#include "icc.h"
      3#include "util.h"
      4#include "allocator.h"
      5
      6#include <stdlib.h>
      7#include <stdio.h>
      8
      9void
     10part1(void)
     11{
     12	struct icc icc;
     13	int res, rc;
     14
     15	icc_init(&icc);
     16	icc_parse_inst(&icc, aoc.input, aoc.input_size);
     17
     18	icc_set_inst(&icc, 1, 12);
     19	icc_set_inst(&icc, 2, 2);
     20
     21	while (icc.state != ICC_HALT) {
     22		rc = icc_step_inst(&icc);
     23		if (rc) die("icc step failed");
     24	}
     25
     26	icc_get_inst(&icc, 0, &res);
     27	aoc.answer = aprintf("%i", res);
     28	aoc.solution = "5098658";
     29
     30	icc_deinit(&icc);
     31}
     32
     33void
     34part2(void)
     35{
     36	struct icc icc;
     37	struct dvec inst;
     38	int a, b, c;
     39	int rc;
     40
     41	icc_init(&icc);
     42	icc.abort_on_err = false;
     43	icc_parse_inst(&icc, aoc.input, aoc.input_size);
     44
     45	dvec_init(&inst, sizeof(int), 0, &stdlib_heap_allocator);
     46	dvec_copy(&inst, &icc.instructions);
     47	icc_debug_dump(&icc, &inst);
     48
     49	for (a = 0; a < 100; a++) {
     50		for (b = 0; b < 100; b++) {
     51			icc_reset(&icc, &inst);
     52
     53			icc_set_inst(&icc, 1, a);
     54			icc_set_inst(&icc, 2, b);
     55
     56			aoc_debug("\nTrying a:%i b:%i\n\n", a, b);
     57			while (icc.state != ICC_HALT) {
     58				rc = icc_step_inst(&icc);
     59				if (rc == ICC_OOB_WRITE) {
     60					dvec_reserve(&icc.instructions,
     61						(size_t) icc.write_addr + 1);
     62					icc.instructions.len = MAX(
     63						icc.instructions.len,
     64						(size_t) icc.write_addr + 1);
     65				} else if (rc != ICC_OK) {
     66					break;
     67				}
     68			}
     69
     70			icc_debug_dump(&icc, &inst);
     71
     72			icc_get_inst(&icc, 0, &c);
     73			if (c == 19690720)
     74				goto exit;
     75		}
     76	}
     77
     78exit:
     79	aoc.answer = aprintf("%02i%02i", a, b);
     80	aoc.solution = "5064";
     81
     82	dvec_deinit(&inst);
     83	icc_deinit(&icc);
     84}