main.c (1777B)
1#include "iccmp.h" 2#include "maxint.h" 3#include "aoc.h" 4#include "util.h" 5 6#include "assert.h" 7#include <stdlib.h> 8 9void 10consume_output(struct icc *icc) 11{ 12 struct maxint mi; 13 mi_ul imm; 14 15 mi.cap = 0; 16 mi.data = &imm; 17 mi.size = 1; 18 mi.sign = MI_POS; 19 imm = 128; 20 21 while (icc->state != ICC_HALT) { 22 switch (icc->state) { 23 case ICC_OUTPUT: 24 if (mi_cmp(&icc->out, &mi) >= 0) { 25 aoc_debug("LARGE\n"); 26 return; 27 } 28 aoc_debug("%c", (char) mi_cast_ul(&icc->out)); 29 break; 30 case ICC_INPUT: 31 return; 32 } 33 icc_step_inst(icc); 34 } 35} 36 37void 38feed_input(struct icc *icc, const char *line) 39{ 40 const char *c; 41 42 consume_output(icc); 43 44 c = line; 45 while (icc->state != ICC_HALT) { 46 switch (icc->state) { 47 case ICC_INPUT: 48 mi_setv(&icc->in, (mi_ul) *c, MI_POS); 49 c += 1; 50 break; 51 } 52 icc_step_inst(icc); 53 if (!*c) return; 54 } 55} 56 57void 58part1(void) 59{ 60 struct icc icc; 61 62 icc_init(&icc); 63 icc_parse_inst(&icc, aoc.input, aoc.input_size); 64 65 feed_input(&icc, "NOT C J\n"); 66 feed_input(&icc, "AND D J\n"); 67 feed_input(&icc, "NOT A T\n"); 68 feed_input(&icc, "OR T J\n"); 69 feed_input(&icc, "WALK\n"); 70 71 consume_output(&icc); 72 assert(icc.state == ICC_OUTPUT); 73 74 aoc.answer = aprintf("%lu", mi_cast_ul(&icc.out)); 75 aoc.solution = "19358262"; 76 77 icc_deinit(&icc); 78} 79 80void 81part2(void) 82{ 83 struct icc icc; 84 85 icc_init(&icc); 86 icc_parse_inst(&icc, aoc.input, aoc.input_size); 87 88 feed_input(&icc, "NOT C J\n"); 89 feed_input(&icc, "NOT B T\n"); 90 feed_input(&icc, "OR T J\n"); 91 feed_input(&icc, "AND D J\n"); 92 feed_input(&icc, "AND H J\n"); 93 feed_input(&icc, "NOT A T\n"); 94 feed_input(&icc, "OR T J\n"); 95 feed_input(&icc, "RUN\n"); 96 97 consume_output(&icc); 98 assert(icc.state == ICC_OUTPUT); 99 100 aoc.answer = aprintf("%lu", mi_cast_ul(&icc.out)); 101 aoc.solution = "1142686742"; 102 103 icc_deinit(&icc); 104}