commit 24c8d3a4e5f641a8f1ebe6d07145b87ae4135801
parent 16dca05dd6772ec075840fc46fc19771ea405023
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 7 Nov 2021 13:01:34 +0100
Stash day 9 before adding maxint to icc
Diffstat:
6 files changed, 151 insertions(+), 26 deletions(-)
diff --git a/libs/include/icc.h b/libs/include/icc.h
@@ -5,8 +5,16 @@
#include <stdlib.h>
enum {
+ ICC_OK,
+ ICC_INPUT,
+ ICC_OUTPUT,
+ ICC_HALT
+};
+
+enum {
ICC_PARAM_IMM,
- ICC_PARAM_POS
+ ICC_PARAM_POS,
+ ICC_PARAM_REL
};
enum {
@@ -18,22 +26,16 @@ enum {
ICC_INST_JMPF = 6,
ICC_INST_TLT = 7,
ICC_INST_TEQ = 8,
+ ICC_INST_BASE = 9,
ICC_INST_HALT = 99
};
-enum {
- ICC_OK,
- ICC_INPUT,
- ICC_OUTPUT,
- ICC_HALT
-};
-
struct icc {
int status;
int in, out;
- size_t instp;
+ size_t instp, base;
struct memvec instructions;
};
@@ -41,6 +43,9 @@ int icc_init(struct icc *icc);
void icc_free(struct icc *icc);
void icc_copy(struct icc *dst, struct icc *src);
+void * icc_inst_copy(struct icc *icc);
+void icc_reset(struct icc *icc, void *instcopy);
+
int icc_parse_inst(struct icc *icc, const char *str, size_t len);
void icc_step_inst(struct icc *icc);
@@ -52,6 +57,3 @@ void icc_get_param(struct icc *icc, int param, int *out);
void icc_get_dest(struct icc *icc, int param, int *out);
void icc_input_callback(struct icc *icc, int (*callback)(int));
-
-void * icc_inst_copy(struct icc *icc);
-void icc_reset(struct icc *icc, void *instcopy);
diff --git a/libs/src/icc.c b/libs/src/icc.c
@@ -5,6 +5,7 @@ icc_init(struct icc *icc)
{
icc->instp = 0;
icc->status = ICC_OK;
+ icc->base = 0;
return memvec_init(&icc->instructions, 0, sizeof(int));
}
@@ -21,9 +22,26 @@ icc_copy(struct icc *dst, struct icc *src)
dst->in = src->in;
dst->out = src->out;
dst->status = src->status;
+ dst->base = src->base;
memvec_copy(&dst->instructions, &src->instructions);
}
+void*
+icc_inst_copy(struct icc *icc)
+{
+ return CHKP(memdup(icc->instructions.data, icc->instructions.len));
+}
+
+void
+icc_reset(struct icc *icc, void *instcopy)
+{
+ icc->status = ICC_OK;
+ icc->instp = 0;
+ icc->base = 0;
+ if (instcopy)
+ memcpy(icc->instructions.data, instcopy, icc->instructions.len);
+}
+
int
icc_parse_inst(struct icc *icc, const char *str, size_t len)
{
@@ -218,6 +236,19 @@ icc_inst_test_eq(struct icc *icc)
}
void
+icc_inst_base(struct icc *icc)
+{
+ int off;
+
+ icc_debug_op(icc, "BASE", 1);
+ icc_get_param(icc, 1, &off);
+ icc->base += off;
+
+ icc->instp += 2;
+ icc->status = ICC_OK;
+}
+
+void
icc_inst_halt(struct icc *icc)
{
icc_debug_op(icc, "HALT", 0);
@@ -257,6 +288,9 @@ icc_step_inst(struct icc *icc)
case ICC_INST_TEQ:
icc_inst_test_eq(icc);
break;
+ case ICC_INST_BASE:
+ icc_inst_base(icc);
+ break;
case ICC_INST_HALT:
icc_inst_halt(icc);
break;
@@ -303,6 +337,9 @@ icc_get_param(struct icc *icc, int param, int *out)
case ICC_PARAM_POS:
icc_get_inst(icc, val, out);
break;
+ case ICC_PARAM_REL:
+ *out = icc->base + val;
+ break;
default:
die("ICC: Unknown parmeter mode\n");
};
@@ -314,18 +351,4 @@ icc_get_dest(struct icc *icc, int param, int *out)
icc_get_inst(icc, icc->instp + param, out);
}
-void*
-icc_inst_copy(struct icc *icc)
-{
- return CHKP(memdup(icc->instructions.data, icc->instructions.len));
-}
-
-void
-icc_reset(struct icc *icc, void *instcopy)
-{
- icc->status = ICC_OK;
- icc->instp = 0;
- if (instcopy)
- memcpy(icc->instructions.data, instcopy, icc->instructions.len);
-}
diff --git a/src/day9/Makefile b/src/day9/Makefile
@@ -0,0 +1,12 @@
+CFLAGS = -g -I ../../libs/include -L ../../libs/build
+LDLIBS = -laoc
+
+all: lib main
+
+clean:
+ rm main
+
+lib:
+ make -C ../../libs
+
+main: main.c ../../libs/build/libaoc.a
diff --git a/src/day9/input b/src/day9/input
@@ -0,0 +1 @@
+1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,608,1029,1102,1,29,1006,1101,39,0,1016,1101,1,0,1021,1101,37,0,1008,1101,0,25,1003,1102,32,1,1002,1101,0,35,1007,1102,1,28,1009,1101,0,31,1012,1101,22,0,1010,1101,319,0,1026,1102,1,23,1019,1102,423,1,1024,1101,27,0,1017,1101,0,36,1005,1101,0,0,1020,1101,681,0,1022,1102,1,30,1015,1101,0,24,1004,1102,312,1,1027,1102,1,21,1000,1102,1,34,1018,1101,0,678,1023,1101,0,38,1011,1102,1,418,1025,1102,1,20,1014,1101,33,0,1001,1101,0,26,1013,1102,1,613,1028,109,3,1202,5,1,63,1008,63,36,63,1005,63,205,1001,64,1,64,1105,1,207,4,187,1002,64,2,64,109,11,21108,40,40,0,1005,1014,229,4,213,1001,64,1,64,1105,1,229,1002,64,2,64,109,-19,1202,6,1,63,1008,63,33,63,1005,63,255,4,235,1001,64,1,64,1105,1,255,1002,64,2,64,109,3,1201,8,0,63,1008,63,29,63,1005,63,277,4,261,1106,0,281,1001,64,1,64,1002,64,2,64,109,10,21107,41,42,3,1005,1011,299,4,287,1106,0,303,1001,64,1,64,1002,64,2,64,109,19,2106,0,0,1001,64,1,64,1105,1,321,4,309,1002,64,2,64,109,-15,21107,42,41,-2,1005,1010,341,1001,64,1,64,1106,0,343,4,327,1002,64,2,64,109,6,2101,0,-9,63,1008,63,30,63,1005,63,363,1106,0,369,4,349,1001,64,1,64,1002,64,2,64,109,-11,1208,-5,29,63,1005,63,389,1001,64,1,64,1106,0,391,4,375,1002,64,2,64,109,15,1206,-2,409,4,397,1001,64,1,64,1105,1,409,1002,64,2,64,109,-3,2105,1,5,4,415,1105,1,427,1001,64,1,64,1002,64,2,64,109,-18,21101,43,0,10,1008,1011,42,63,1005,63,447,1106,0,453,4,433,1001,64,1,64,1002,64,2,64,109,19,1205,1,467,4,459,1105,1,471,1001,64,1,64,1002,64,2,64,109,-5,2107,34,-8,63,1005,63,489,4,477,1106,0,493,1001,64,1,64,1002,64,2,64,109,-11,2102,1,-1,63,1008,63,28,63,1005,63,517,1001,64,1,64,1105,1,519,4,499,1002,64,2,64,109,8,2108,37,-5,63,1005,63,539,1001,64,1,64,1106,0,541,4,525,1002,64,2,64,109,17,1206,-8,557,1001,64,1,64,1105,1,559,4,547,1002,64,2,64,109,-11,1205,2,571,1105,1,577,4,565,1001,64,1,64,1002,64,2,64,109,-14,1207,0,25,63,1005,63,599,4,583,1001,64,1,64,1105,1,599,1002,64,2,64,109,32,2106,0,-8,4,605,1105,1,617,1001,64,1,64,1002,64,2,64,109,-27,2102,1,-5,63,1008,63,24,63,1005,63,639,4,623,1105,1,643,1001,64,1,64,1002,64,2,64,109,-16,2101,0,10,63,1008,63,25,63,1005,63,669,4,649,1001,64,1,64,1105,1,669,1002,64,2,64,109,22,2105,1,8,1106,0,687,4,675,1001,64,1,64,1002,64,2,64,109,-21,1208,8,32,63,1005,63,705,4,693,1105,1,709,1001,64,1,64,1002,64,2,64,109,19,1207,-5,36,63,1005,63,729,1001,64,1,64,1105,1,731,4,715,1002,64,2,64,109,9,21101,44,0,-5,1008,1017,44,63,1005,63,753,4,737,1105,1,757,1001,64,1,64,1002,64,2,64,109,-12,21108,45,46,5,1005,1015,773,1105,1,779,4,763,1001,64,1,64,1002,64,2,64,109,-8,2108,25,1,63,1005,63,801,4,785,1001,64,1,64,1105,1,801,1002,64,2,64,109,-12,2107,22,10,63,1005,63,817,1106,0,823,4,807,1001,64,1,64,1002,64,2,64,109,23,1201,-8,0,63,1008,63,38,63,1005,63,847,1001,64,1,64,1106,0,849,4,829,1002,64,2,64,109,-3,21102,46,1,4,1008,1014,46,63,1005,63,871,4,855,1106,0,875,1001,64,1,64,1002,64,2,64,109,5,21102,47,1,2,1008,1017,46,63,1005,63,899,1001,64,1,64,1105,1,901,4,881,4,64,99,21101,0,27,1,21101,0,915,0,1105,1,922,21201,1,42136,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,0,942,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2105,1,0
diff --git a/src/day9/main.c b/src/day9/main.c
@@ -0,0 +1,16 @@
+#include "aoc.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+part1(void)
+{
+
+}
+
+void
+part2(void)
+{
+
+}
diff --git a/src/day9/part1 b/src/day9/part1
@@ -0,0 +1,71 @@
+--- Day 9: Sensor Boost ---
+
+You've just said goodbye to the rebooted rover and left Mars when you receive a faint distress
+signal coming from the asteroid belt. It must be the Ceres monitoring station!
+
+In order to lock on to the signal, you'll need to boost your sensors. The Elves send up the latest
+[1m[37mBOOST[0m program - Basic Operation Of System Test.
+
+While BOOST (your puzzle input) is capable of boosting your sensors, for tenuous safety reasons, it
+refuses to do so until the computer it runs on passes some checks to demonstrate it is a
+[1m[37mcomplete Intcode computer[0m.
+
+Your existing Intcode computer is missing one key feature: it needs support for parameters in
+[1m[37mrelative mode[0m.
+
+Parameters in mode 2, [1m[37mrelative mode[0m, behave very similarly to parameters in
+[1m[37mposition mode[0m: the parameter is interpreted as a position. Like position mode,
+parameters in relative mode can be read from or written to.
+
+The important difference is that relative mode parameters don't count from address 0. Instead, they
+count from a value called the [1m[37mrelative base[0m. The [1m[37mrelative base[0m starts at
+0.
+
+The address a relative mode parameter refers to is itself [1m[37mplus[0m the current
+[1m[37mrelative base[0m. When the relative base is 0, relative mode parameters and position mode
+parameters with the same value refer to the same address.
+
+For example, given a relative base of 50, a relative mode parameter of -7 refers to memory address
+50 + -7 = [1m[37m43[0m.
+
+The relative base is modified with the [1m[37mrelative base offset[0m instruction:
+
+
+ - Opcode 9 [1m[37madjusts the relative base[0m by the value of its only parameter. The relative
+base increases (or decreases, if the value is negative) by the value of the parameter.
+
+
+For example, if the relative base is 2000, then after the instruction 109,19, the relative base
+would be 2019. If the next instruction were 204,-34, then the value at address 1985 would be output.
+
+Your Intcode computer will also need a few other capabilities:
+
+
+ - The computer's available memory should be much larger than the initial program. Memory beyond the
+initial program starts with the value 0 and can be read or written like any other memory. (It is
+invalid to try to access memory at a negative address, though.)
+
+ - The computer should have support for large numbers. Some instructions near the beginning of the
+BOOST program will verify this capability.
+
+
+Here are some example programs that use these features:
+
+
+ - 109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 takes no input and produces a copy of
+itself as output.
+
+ - 1102,34915192,34915192,7,4,7,99,0 should output a 16-digit number.
+
+ - 104,1125899906842624,99 should output the large number in the middle.
+
+
+The BOOST program will ask for a single input; run it in test mode by providing it the value 1. It
+will perform a series of checks on each opcode, output any opcodes (and the associated parameter
+modes) that seem to be functioning incorrectly, and finally output a BOOST keycode.
+
+Once your Intcode computer is fully functional, the BOOST program should report no malfunctioning
+opcodes when run in test mode; it should only output a single value, the BOOST keycode.
+[1m[37mWhat BOOST keycode does it produce?[0m
+
+