aoc-2019-c

git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

commit 17ffe5782313190d2f2eced88436b2504c4e4e03
parent 0ba2f1d3d01cec0e51ccbe6b144009a7440f7b7b
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed,  3 Nov 2021 14:24:38 +0100

Add day 5 part 2

Diffstat:
Mlibs/include/icc.h | 4++++
Mlibs/src/icc.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/day4/main.c | 6++++--
Msrc/day5/main.c | 28+++++++++++++++++++++++++++-
Asrc/day5/test2.input | 1+
5 files changed, 110 insertions(+), 3 deletions(-)

diff --git a/libs/include/icc.h b/libs/include/icc.h @@ -14,6 +14,10 @@ enum { ICC_INST_MULT = 2, ICC_INST_STORE = 3, ICC_INST_LOAD = 4, + ICC_INST_JMPT = 5, + ICC_INST_JMPF = 6, + ICC_INST_TLT = 7, + ICC_INST_TEQ = 8, ICC_INST_HALT = 99 }; diff --git a/libs/src/icc.c b/libs/src/icc.c @@ -145,6 +145,68 @@ icc_inst_load(struct icc *icc) } void +icc_inst_jmp_true(struct icc *icc) +{ + int cond, addr; + + icc_get_param(icc, 1, &cond); + icc_get_param(icc, 2, &addr); + + icc_debug_op(icc, "JMPT", 2); + if (cond) icc->instp = addr; + else icc->instp += 3; + + icc->status = ICC_OK; +} + +void +icc_inst_jmp_false(struct icc *icc) +{ + int cond, addr; + + icc_get_param(icc, 1, &cond); + icc_get_param(icc, 2, &addr); + + icc_debug_op(icc, "JMPF", 2); + if (!cond) icc->instp = addr; + else icc->instp += 3; + + icc->status = ICC_OK; +} + +void +icc_inst_test_lt(struct icc *icc) +{ + int a, b, dst; + + icc_get_param(icc, 1, &a); + icc_get_param(icc, 2, &b); + icc_get_dest(icc, 3, &dst); + + icc_debug_op(icc, "TLT", 3); + icc_set_inst(icc, dst, a < b); + + icc->instp += 4; + icc->status = ICC_OK; +} + +void +icc_inst_test_eq(struct icc *icc) +{ + int a, b, dst; + + icc_get_param(icc, 1, &a); + icc_get_param(icc, 2, &b); + icc_get_dest(icc, 3, &dst); + + icc_debug_op(icc, "TEQ", 3); + icc_set_inst(icc, dst, a == b); + + icc->instp += 4; + icc->status = ICC_OK; +} + +void icc_step_inst(struct icc *icc) { int inst; @@ -165,6 +227,18 @@ icc_step_inst(struct icc *icc) case ICC_INST_LOAD: icc_inst_load(icc); break; + case ICC_INST_JMPT: + icc_inst_jmp_true(icc); + break; + case ICC_INST_JMPF: + icc_inst_jmp_false(icc); + break; + case ICC_INST_TLT: + icc_inst_test_lt(icc); + break; + case ICC_INST_TEQ: + icc_inst_test_eq(icc); + break; case ICC_INST_HALT: icc->status = ICC_HALT; break; diff --git a/src/day4/main.c b/src/day4/main.c @@ -85,7 +85,8 @@ part1(void) count = 0; count_valid(start, end, 1, 1, 0, &count, check_pass_1); - aoc.answer = aprintf("%i", count); + aoc.answer = CHKP(aprintf("%i", count)); + aoc.solution = "966"; } void @@ -102,5 +103,6 @@ part2(void) count = 0; count_valid(start, end, 1, 1, 0, &count, check_pass_2); - aoc.answer = aprintf("%i", count); + aoc.answer = CHKP(aprintf("%i", count)); + aoc.solution = "628"; } diff --git a/src/day5/main.c b/src/day5/main.c @@ -8,6 +8,7 @@ void part1(void) { struct icc icc; + int code; ASSERT(icc_init(&icc) == OK); ASSERT(icc_parse_inst(&icc, aoc.input, aoc.input_size) == OK); @@ -19,16 +20,41 @@ part1(void) icc.in = 1; break; case ICC_OUTPUT: - printf("%i\n", icc.out); + debug("OUTPUT: %i\n", icc.out); + code = icc.out; break; } } + aoc.answer = CHKP(aprintf("%i", code)); + aoc.solution = "15314507"; + icc_free(&icc); } void part2(void) { + struct icc icc; + int code; + + ASSERT(icc_init(&icc) == OK); + ASSERT(icc_parse_inst(&icc, aoc.input, aoc.input_size) == OK); + while (icc.status != ICC_HALT) { + icc_step_inst(&icc); + switch (icc.status) { + case ICC_INPUT: + icc.in = 5; + break; + case ICC_OUTPUT: + debug("OUTPUT: %i\n", icc.out); + code = icc.out; + break; + } + } + + aoc.answer = CHKP(aprintf("%i", code)); + + icc_free(&icc); } diff --git a/src/day5/test2.input b/src/day5/test2.input @@ -0,0 +1 @@ +3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99