aoc-2019-c

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

commit c8754bf04f3274926474b81b7e28dba748a53eb4
parent 17ffe5782313190d2f2eced88436b2504c4e4e03
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri,  5 Nov 2021 17:09:33 +0100

Add day 7

Diffstat:
Mdata/helper/template/Makefile | 2+-
Mlibs/include/icc.h | 1+
Mlibs/include/memvec.h | 8++++++--
Mlibs/src/icc.c | 28+++++++++++++++++++++++-----
Mlibs/src/memvec.c | 28+++++++++++++++++++++++-----
Mlibs/src/wrapper.c | 5+++--
Asrc/day6/Makefile | 12++++++++++++
Asrc/day6/input | 933+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day6/main.c | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day6/part1 | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day6/part2 | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day6/test.input | 11+++++++++++
Asrc/day6/test.input2 | 13+++++++++++++
Asrc/day7/2 | 35+++++++++++++++++++++++++++++++++++
Asrc/day7/Makefile | 12++++++++++++
Asrc/day7/input | 1+
Asrc/day7/main.c | 187+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day7/part1 | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day7/part2 | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day7/test.input | 1+
Asrc/day7/test.input2 | 1+
Asrc/day7/test.input3 | 1+
22 files changed, 1671 insertions(+), 15 deletions(-)

diff --git a/data/helper/template/Makefile b/data/helper/template/Makefile @@ -9,4 +9,4 @@ clean: lib: make -C ../../libs -main: main.c ../../libs/build/libaoc.so +main: main.c ../../libs/build/libaoc.a diff --git a/libs/include/icc.h b/libs/include/icc.h @@ -39,6 +39,7 @@ struct icc { int icc_init(struct icc *icc); void icc_free(struct icc *icc); +void icc_copy(struct icc *dst, struct icc *src); int icc_parse_inst(struct icc *icc, const char *str, size_t len); void icc_step_inst(struct icc *icc); diff --git a/libs/include/memvec.h b/libs/include/memvec.h @@ -3,6 +3,8 @@ #include "util.h" +#define MEMVEC_GET(mv, i, type) (*(type*) memvec_get(mv, i)) + struct memvec { void *data; size_t cap, len, itemsize; @@ -10,7 +12,9 @@ struct memvec { int memvec_init(struct memvec *v, size_t cap, size_t itemsize); void memvec_free(struct memvec *v); -int memvec_add(struct memvec *v, void *item); -void* memvec_get(struct memvec *v, size_t index); +void * memvec_add(struct memvec *v, void *item); +void * memvec_get(struct memvec *v, size_t index); void memvec_set(struct memvec *v, size_t index, void *item); int memvec_in_range(struct memvec *v, size_t index); +size_t memvec_len(struct memvec *v); +void memvec_copy(struct memvec *dst, struct memvec *src); diff --git a/libs/src/icc.c b/libs/src/icc.c @@ -14,6 +14,16 @@ icc_free(struct icc *icc) memvec_free(&icc->instructions); } +void +icc_copy(struct icc *dst, struct icc *src) +{ + dst->instp = src->instp; + dst->in = src->in; + dst->out = src->out; + dst->status = src->status; + memvec_copy(&dst->instructions, &src->instructions); +} + int icc_parse_inst(struct icc *icc, const char *str, size_t len) { @@ -27,7 +37,7 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len) val = strtoul(line, &endptr, 10); if (endptr && !memchr(",\n", *endptr, 3)) return FAIL; - if (memvec_add(&icc->instructions, &val) == FAIL) + if (!memvec_add(&icc->instructions, &val)) return FAIL; line = nline; } while (nline); @@ -50,7 +60,7 @@ icc_param_str(struct icc *icc, int param) void icc_debug_op(struct icc *icc, const char *opstr, int n) { - int i, val, inst; + int i, val, val2, inst; if (!aoc.debug) return; @@ -66,7 +76,8 @@ icc_debug_op(struct icc *icc, const char *opstr, int n) fprintf(stderr, "%i", val); break; case ICC_PARAM_POS: - fprintf(stderr, "[%i]", val); + icc_get_inst(icc, val, &val2); + fprintf(stderr, "[%i]=%i", val, val2); break; default: die("ICC: Unknown parmeter mode\n"); @@ -207,6 +218,13 @@ icc_inst_test_eq(struct icc *icc) } void +icc_inst_halt(struct icc *icc) +{ + icc_debug_op(icc, "HALT", 0); + icc->status = ICC_HALT; +} + +void icc_step_inst(struct icc *icc) { int inst; @@ -240,7 +258,7 @@ icc_step_inst(struct icc *icc) icc_inst_test_eq(icc); break; case ICC_INST_HALT: - icc->status = ICC_HALT; + icc_inst_halt(icc); break; default: die("ICC: Unknown instruction: '%i'\n", inst); @@ -299,7 +317,7 @@ icc_get_dest(struct icc *icc, int param, int *out) void* icc_inst_copy(struct icc *icc) { - return memdup(icc->instructions.data, icc->instructions.len); + return CHKP(memdup(icc->instructions.data, icc->instructions.len)); } void diff --git a/libs/src/memvec.c b/libs/src/memvec.c @@ -3,7 +3,8 @@ int memvec_init(struct memvec *v, size_t cap, size_t itemsize) { - v->cap = cap ? cap : 10 * itemsize; + if (!cap) cap = 10; + v->cap = cap * itemsize; v->itemsize = itemsize; v->len = 0; v->data = malloc(v->cap); @@ -15,9 +16,10 @@ void memvec_free(struct memvec *v) { free(v->data); + v->data = NULL; } -int +void * memvec_add(struct memvec *v, void *item) { void *tmp; @@ -25,16 +27,16 @@ memvec_add(struct memvec *v, void *item) if (v->len == v->cap) { v->cap *= 2; tmp = realloc(v->data, v->cap); - if (!tmp) return FAIL; + if (!tmp) return NULL; v->data = tmp; } memcpy(v->data + v->len, item, v->itemsize); v->len += v->itemsize; - return OK; + return v->data + v->len - v->itemsize; } -void* +void * memvec_get(struct memvec *v, size_t index) { return v->data + v->itemsize * index; @@ -53,3 +55,19 @@ memvec_in_range(struct memvec *v, size_t index) { return index < v->len / v->itemsize; } + +size_t +memvec_len(struct memvec *v) +{ + return v->len / v->itemsize; +} + +void +memvec_copy(struct memvec *dst, struct memvec *src) +{ + dst->itemsize = src->itemsize; + dst->len = src->len; + dst->cap = src->cap; + dst->data = CHKP(malloc(src->cap)); + memcpy(dst->data, src->data, src->cap); +} diff --git a/libs/src/wrapper.c b/libs/src/wrapper.c @@ -35,8 +35,8 @@ main(int argc, const char **argv) /* call solution */ aoc.answer = NULL; aoc.solution = NULL; - if (aoc.part == 1) part1(&aoc); - else if (aoc.part == 2) part2(&aoc); + if (aoc.part == 1) part1(); + else if (aoc.part == 2) part2(); else die("Invalid part number\n"); /* check answer if given */ @@ -46,5 +46,6 @@ main(int argc, const char **argv) die("\nIncorrect solution!\n"); } + free(aoc.answer); free(aoc.input); } diff --git a/src/day6/Makefile b/src/day6/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/day6/input b/src/day6/input @@ -0,0 +1,933 @@ +6WF)DRK +2PT)PSM +H42)FN8 +1XR)LQD +HRK)9KL +TD6)H8W +98Z)BJM +RCQ)LVG +RWQ)Q7H +2PS)X94 +NHB)25X +PXC)W57 +L8L)MVX +CFK)D8K +R1B)43T +PDY)QKX +FQK)82K +JJ6)MQJ +FB6)6V1 +R28)5MZ +BN2)5HN +6BQ)JVC +W57)22C +MQJ)DL2 +MTC)84R +RH8)CRN +Y27)3GN +CKQ)31C +R7V)9BK +ZDY)PDY +X2Q)Y6S +Q8B)SAN +1Z3)PVT +R87)57R +KCJ)44X +PWQ)9CB +HLC)VYW +HFP)9XS +X33)MC3 +RYS)R7R +JRF)VHW +79R)FXZ +YQQ)STV +8J6)JWX +Q6D)RV6 +LL9)B4D +6R1)T1Z +VK9)42M +PQP)17N +K6C)HMK +GLY)N47 +KDW)CDC +DQ4)RY5 +SND)FDR +7YF)1VN +MDT)B3S +D3F)98Z +5VH)MR7 +KNR)2L8 +CJW)QDL +FWY)14X +SJD)79R +COM)BXW +T2B)FPB +B2Q)BRJ +Z21)HYC +VHW)5XR +WZ4)2JM +8HF)342 +PYR)X9Y +RKF)P43 +S1S)9WT +2PB)BSB +QF7)M9T +HML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ff --git a/src/day6/main.c b/src/day6/main.c @@ -0,0 +1,138 @@ +#include "aoc.h" +#include "memvec.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +struct planet { + char *name; + struct planet *parent; + struct memvec children; +}; + +struct memvec planets; + +// TODO: implement hashmap to replace array search + +struct planet * +get_planet(const char *name) +{ + struct planet *p; + int i; + + for (i = 0; i < memvec_len(&planets); i++) { + p = MEMVEC_GET(&planets, i, struct planet *); + if (!strcmp(p->name, name)) + return p; + } + + return NULL; +} + +struct planet * +add_planet(const char *name) +{ + struct planet *p; + + p = CHKP(malloc(sizeof(struct planet))); + p->name = CHKP(strdup(name)); + p->parent = NULL; + ASSERT(memvec_init(&p->children, 3, sizeof(struct planet *)) == OK); + CHKP(memvec_add(&planets, &p)); + + return p; +} + +void +init_planets() +{ + char *tok, *sep, *next; + struct planet *parent, *child; + + memvec_init(&planets, 100, sizeof(struct planet *)); + + tok = aoc.input; + do { + next = strchr(tok, '\n'); + if (!next) break; + + sep = CHKP(strchr(tok, ')')); + *next = *sep = '\0'; + + parent = get_planet(tok); + if (!parent) parent = add_planet(tok); + + child = get_planet(sep+1); + if (!child) child = add_planet(sep+1); + + CHKP(memvec_add(&parent->children, child)); + ASSERT(child->parent == NULL); + child->parent = parent; + + tok = next + 1; + } while (next); +} + +void +free_planets() +{ + struct planet *p; + int i; + + for (i = 0; i < memvec_len(&planets); i++) { + p = MEMVEC_GET(&planets, i, struct planet *); + memvec_free(&p->children); + free(p->name); + free(p); + } + memvec_free(&planets); +} + +void +part1(void) +{ + struct planet *p; + int i, orbits; + + init_planets(); + + orbits = 0; + for (i = 0; i < memvec_len(&planets); i++) { + p = MEMVEC_GET(&planets, i, struct planet *); + while (p->parent) { + orbits += 1; + p = p->parent; + } + } + + aoc.answer = CHKP(aprintf("%i", orbits)); + aoc.solution = "119831"; + + free_planets(); +} + +void +part2(void) +{ + struct planet *p1, *p2, *san, *you; + int i, s1, s2; + + init_planets(); + + san = CHKP(get_planet("SAN")); + you = CHKP(get_planet("YOU")); + + for (s1 = 0, p1 = you; p1; p1 = p1->parent, s1++) { + for (s2 = 0, p2 = san; p2; p2 = p2->parent, s2++) { + if (!strcmp(p1->name, p2->name)) + goto exit; + } + } +exit: + + aoc.answer = CHKP(aprintf("%i", s1 + s2 - 2)); + aoc.solution = "322"; + + free_planets(); +} diff --git a/src/day6/part1 b/src/day6/part1 @@ -0,0 +1,71 @@ +--- Day 6: Universal Orbit Map --- + +You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often +involves transferring between orbits, the orbit maps here are useful for finding efficient routes +between, for example, you and Santa. You download a map of the local orbits (your puzzle input). + +Except for the universal Center of Mass (COM), every object in space is in orbit around exactly one +other object. An orbit looks roughly like this: + + \ + \ + | + | +AAA--> o o <--BBB + | + | + / + / + +In this diagram, the object BBB is in orbit around AAA. The path that BBB takes around AAA (drawn +with lines) is only partly shown. In the map data, this orbital relationship is written AAA)BBB, +which means "BBB is in orbit around AAA". + +Before you use your map data to plot a course, you need to make sure it wasn't corrupted during the +download. To verify maps, the Universal Orbit Map facility uses orbit count checksums +- the total number of direct orbits (like the one shown above) and indirect +orbits. + +Whenever A orbits B and B orbits C, then A indirectly orbits C. This chain can be any +number of objects long: if A orbits B, B orbits C, and C orbits D, then A indirectly orbits D. +For example, suppose you have the following map: + +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L + +Visually, the above map of orbits looks like this: + + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I + +In this visual representation, when two objects are connected by a line, the one on the right +directly orbits the one on the left. + +Here, we can count the total number of orbits as follows: + + + - D directly orbits C and indirectly orbits B and COM, a total of 3 orbits. + + - L directly orbits K and indirectly orbits J, E, D, C, B, and COM, a total of 7 orbits. + + - COM orbits nothing. + + +The total number of direct and indirect orbits in this example is 42. + +What is the total number of direct and indirect orbits in your map data? + + + diff --git a/src/day6/part2 b/src/day6/part2 @@ -0,0 +1,62 @@ +--- Part Two --- + +Now, you just need to figure out how many orbital transfers you (YOU) need to take to +get to Santa (SAN). + +You start at the object YOU are orbiting; your destination is the object SAN is orbiting. An orbital +transfer lets you move from any object to an object orbiting or orbited by that object. + +For example, suppose you have the following map: + +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN + +Visually, the above map of orbits looks like this: + + YOU + / + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I - SAN + +In this example, YOU are in orbit around K, and SAN is in orbit around I. To move from K to I, a +minimum of 4 orbital transfers are required: + + + - K to J + + - J to E + + - E to D + + - D to I + + +Afterward, the map of orbits looks like this: + + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I - SAN + \ + YOU + +What is the minimum number of orbital transfers required to move from the object YOU +are orbiting to the object SAN is orbiting? (Between the objects they are orbiting - +not between YOU and SAN.) + + diff --git a/src/day6/test.input b/src/day6/test.input @@ -0,0 +1,11 @@ +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L diff --git a/src/day6/test.input2 b/src/day6/test.input2 @@ -0,0 +1,13 @@ +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN diff --git a/src/day7/2 b/src/day7/2 @@ -0,0 +1,35 @@ +#include "aoc.h" +#include "icc.h" + +#include <stdlib.h> +#include <stdio.h> + +void +run_amp(int phase, int input, int *out) +{ + +} + +void +part1(void) +{ + struct icc icc; + void *inst; + + ASSERT(icc_init(&icc) == OK); + ASSERT(icc_parse_inst(&icc, aoc.input, aoc.input_size) == OK); + inst = icc_inst_copy(&icc); + + + icc_reset(&icc, inst); + while (icc.status != ICC_HALT) + icc_step_inst(&icc); + + icc_free(&icc); +} + +void +part2(void) +{ + +} diff --git a/src/day7/Makefile b/src/day7/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/day7/input b/src/day7/input @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,42,67,76,89,110,191,272,353,434,99999,3,9,102,2,9,9,1001,9,2,9,1002,9,2,9,1001,9,2,9,4,9,99,3,9,1001,9,4,9,102,4,9,9,101,3,9,9,1002,9,2,9,1001,9,4,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,1001,9,3,9,1002,9,3,9,4,9,99,3,9,102,3,9,9,101,2,9,9,1002,9,3,9,101,5,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99 diff --git a/src/day7/main.c b/src/day7/main.c @@ -0,0 +1,187 @@ +#include "aoc.h" +#include "icc.h" + +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> + +void +run_amp(struct icc *icc, int phase, int input, int *out) +{ + int in_cnt, out_cnt; + + in_cnt = out_cnt = 0; + while (icc->status != ICC_HALT) { + icc_step_inst(icc); + switch (icc->status) { + case ICC_INPUT: + ASSERT(in_cnt <= 1); + icc->in = (in_cnt == 0) ? phase : input; + in_cnt++; + break; + case ICC_OUTPUT: + ASSERT(out_cnt == 0); + *out = icc->out; + out_cnt++; + break; + } + } +} + +void +bf_phase(struct icc *icc, void *inst, int *state, + int max_depth, int depth, int in, int *max, int *maxstate) +{ + int i, k, out; + + if (depth == max_depth) { + debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1], + state[2], state[3], state[4], in); + if (*max == 0 || in > *max) { + *max = in; + memcpy(maxstate, state, sizeof(int) * max_depth); + } + return; + } + + for (i = 0; i < max_depth; i++) { + /* dont reuse phase setting */ + for (k = 0; k < depth; k++) + if (state[k] == i) break; + if (k != depth) continue; + + state[depth] = i; + icc_reset(icc, inst); + debug("START AMP %i (%i):\n", depth, i); + run_amp(icc, i, in, &out); + debug("END AMP %i (%i): %i -> %i\n", depth, i, in, out); + bf_phase(icc, inst, state, max_depth, depth + 1, out, max, maxstate); + } +} + +int +run_phase_loop(struct icc *iccs, void *inst, int *state, int n) +{ + int i, passes, done; + int *ins, out, in_cnt; + + ins = CHKP(malloc(n * sizeof(int))); + + for (i = 0; i < n; i++) + icc_reset(&iccs[i], inst); + + ins[0] = 0; + passes = done = 0; + while (!done) { + for (i = 0; i < n; i++) { + in_cnt = 0; + debug("START AMP %i (%i):\n", i, state[i]); + while (iccs[i].status != ICC_HALT) { + icc_step_inst(&iccs[i]); + switch (iccs[i].status) { + case ICC_INPUT: + iccs[i].in = (!passes && !in_cnt) ? state[i] : ins[i]; + in_cnt++; + break; + case ICC_OUTPUT: + ins[(i+1) % n] = iccs[i].out; + break; + } + if (iccs[i].status == ICC_OUTPUT) + break; + } + if (iccs[i].status == ICC_HALT) + done = 1; + debug("END AMP %i (%i): %i -> %i\n", i, + state[i], ins[i], ins[(i+1) % n]); + } + passes += 1; + } + + out = ins[0]; + free(ins); + + return out; +} + +void +bf_phase_loop(int *state, int max_depth, int depth, + struct icc *iccs, void *inst, int *max, int *maxstate) +{ + int i, k, out; + + if (depth == max_depth) { + out = run_phase_loop(iccs, inst, state, max_depth); + debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1], + state[2], state[3], state[4], out); + if (*max == 0 || out > *max) { + *max = out; + memcpy(maxstate, state, sizeof(int) * max_depth); + } + return; + } + + for (i = 0; i < max_depth; i++) { + for (k = 0; k < depth; k++) + if (state[k] - 5 == i) break; + if (k != depth) continue; + + state[depth] = i + 5; + bf_phase_loop(state, max_depth, depth + 1, iccs, inst, max, maxstate); + } +} + +// TODO: step inst should give err on fail not die +// TODO: create better disassembly (showing values in position mode and values received on input) +// TODO: add icc initializer from instruction memvec +// TODO: add function to parse input instructions into singe memvec +// TODO: pad icc debug op output and add operation specific pseudocode + +void +part1(void) +{ + struct icc icc; + void *inst; + int max, state[5], maxstate[5]; + + ASSERT(icc_init(&icc) == OK); + ASSERT(icc_parse_inst(&icc, aoc.input, aoc.input_size) == OK); + inst = icc_inst_copy(&icc); + + max = 0; + bf_phase(&icc, inst, state, 5, 0, 0, &max, maxstate); + + debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1], + maxstate[2], maxstate[3], maxstate[4]); + aoc.answer = CHKP(aprintf("%i", max)); + aoc.solution = "24625"; + + free(inst); + icc_free(&icc); +} + +void +part2(void) +{ + struct icc iccs[5]; + void *inst; + int i, max, state[5], maxstate[5]; + + ASSERT(icc_init(&iccs[0]) == OK); + ASSERT(icc_parse_inst(&iccs[0], aoc.input, aoc.input_size) == OK); + inst = icc_inst_copy(&iccs[0]); + for (i = 1; i < 5; i++) + icc_copy(&iccs[i], &iccs[0]); + + max = 0; + bf_phase_loop(state, 5, 0, iccs, inst, &max, maxstate); + + debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1], + maxstate[2], maxstate[3], maxstate[4]); + aoc.answer = CHKP(aprintf("%i", max)); + aoc.solution = "36497698"; + + free(inst); + for (i = 0; i < 5; i++) + icc_free(&iccs[i]); +} diff --git a/src/day7/part1 b/src/day7/part1 @@ -0,0 +1,81 @@ +--- Day 7: Amplification Circuit --- + +Based on the navigational maps, you're going to need to send more power to your ship's thrusters to +reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed +on the ship. + +There are five amplifiers connected in series; each one receives an input signal and produces an +output signal. They are connected such that the first amplifier's output leads to the second +amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. +The first amplifier's input value is 0, and the last amplifier's output leads to your ship's +thrusters. + + O-------O O-------O O-------O O-------O O-------O +0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters) + O-------O O-------O O-------O O-------O O-------O + +The Elves have sent you some Amplifier Controller Software (your puzzle input), a +program that should run on your existing Intcode computer. Each amplifier will need to run a copy of +the program. + +When a copy of the program starts running on an amplifier, it will first use an input instruction to +ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase +setting is used exactly once, but the Elves can't remember which amplifier needs which +phase setting. + +The program will then call another input instruction to get the amplifier's input signal, compute +the correct output signal, and supply it back to the amplifier with an output instruction. (If the +amplifier has not yet received an input signal, it waits until one arrives.) + +Your job is to find the largest output signal that can be sent to the thrusters by +trying every possible combination of phase settings on the amplifiers. Make sure that memory is not +shared or reused between copies of the program. + +For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting +amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you +could determine the output signal that gets sent from amplifier E to the thrusters with the +following steps: + + + - Start the copy of the amplifier controller software that will run on amplifier A. At its first +input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, +provide it the input signal, 0. After some calculations, it will use an output instruction to +indicate the amplifier's output signal. + + - Start the software for amplifier B. Provide it the phase setting (1) and then whatever output +signal was produced from amplifier A. It will then produce a new output signal destined for +amplifier C. + + - Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, +then collect its output signal. + + - Run amplifier D's software, provide the phase setting (4) and input value, and collect its output +signal. + + - Run amplifier E's software, provide the phase setting (0) and input value, and collect its output +signal. + + +The final output signal from amplifier E would be sent to the thrusters. However, this phase setting +sequence may not have been the best one; another sequence might have sent a higher signal to the +thrusters. + +Here are some example programs: + + + - Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0): +3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 + + - Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4): +3,23,3,24,1002,24,10,24,1002,23,-1,23, +101,5,23,23,1,24,23,23,4,23,99,0,0 + + - Max thruster signal 65210 (from phase setting sequence 1,0,4,3,2): +3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33, +1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0 + + +Try every combination of phase settings on the amplifiers. What is the highest signal that +can be sent to the thrusters? + + diff --git a/src/day7/part2 b/src/day7/part2 @@ -0,0 +1,55 @@ +--- Part Two --- + +It's no good - in this configuration, the amplifiers can't generate a large enough output signal to +produce the thrust you'll need. The Elves quickly talk you through rewiring the amplifiers into a +feedback loop: + + O-------O O-------O O-------O O-------O O-------O +0 -+->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-. + | O-------O O-------O O-------O O-------O O-------O | + | | + '--------------------------------------------------------+ + | + v + (to thrusters) + +Most of the amplifiers are connected as they were before; amplifier A's output is connected to +amplifier B's input, and so on. However, the output from amplifier E is now connected +into amplifier A's input. This creates the feedback loop: the signal will be sent through the +amplifiers many times. + +In feedback loop mode, the amplifiers need totally different phase settings: integers +from 5 to 9, again each used exactly once. These settings will cause the Amplifier Controller +Software to repeatedly take input and produce output many times before halting. Provide each +amplifier its phase setting at its first input instruction; all further input/output instructions +are for signals. + +Don't restart the Amplifier Controller Software on any amplifier during this process. Each one +should continue receiving and sending signals until it halts. + +All signals sent or received in this process will be between pairs of amplifiers except the very +first signal and the very last signal. To start the process, a 0 signal is sent to amplifier A's +input exactly once. + +Eventually, the software on the amplifiers will halt after they have processed the final loop. When +this happens, the last output signal from amplifier E is sent to the thrusters. Your job is to +find the largest output signal that can be sent to the thrusters using the new phase +settings and feedback loop arrangement. + +Here are some example programs: + + + - Max thruster signal 139629729 (from phase setting sequence 9,8,7,6,5): +3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26, +27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5 + + - Max thruster signal 18216 (from phase setting sequence 9,7,8,5,6): +3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54, +-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4, +53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10 + + +Try every combination of the new phase settings on the amplifier feedback loop. What is +the highest signal that can be sent to the thrusters? + + diff --git a/src/day7/test.input b/src/day7/test.input @@ -0,0 +1 @@ +3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 diff --git a/src/day7/test.input2 b/src/day7/test.input2 @@ -0,0 +1 @@ +3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0 diff --git a/src/day7/test.input3 b/src/day7/test.input3 @@ -0,0 +1 @@ +3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5