From c1293f280fef0eb4fa50393738ef32688682af10 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Fri, 6 Dec 2019 03:16:13 -0500 Subject: Solve day 5 part 2 --- 2019/day05.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to '2019/day05.c') diff --git a/2019/day05.c b/2019/day05.c index 3b38124..ec8c471 100644 --- a/2019/day05.c +++ b/2019/day05.c @@ -1,13 +1,6 @@ #include #include - -static int input(void) { - return 1; -} - -static void output(int x) { - printf("%d\n", x); -} +#include static int ip; static int mem[1024]; @@ -16,7 +9,7 @@ static int *p(int mode, int param) { return (mode ? &mem[ip + param] : &mem[mem[ip + param]]); } -static void run(void) { +static void run(int input) { while (mem[ip] != 99) { int op = mem[ip] % 100; int m1 = mem[ip] / 100 % 10; @@ -25,14 +18,23 @@ static void run(void) { switch (op) { break; case 1: *p(m3, 3) = *p(m1, 1) + *p(m2, 2); ip += 4; break; case 2: *p(m3, 3) = *p(m1, 1) * *p(m2, 2); ip += 4; - break; case 3: *p(m1, 1) = input(); ip += 2; - break; case 4: output(*p(m1, 1)); ip += 2; + break; case 3: *p(m1, 1) = input; ip += 2; + break; case 4: printf("%d\n", *p(m1, 1)); ip += 2; + break; case 5: ip = (*p(m1, 1) ? *p(m2, 2) : ip + 3); + break; case 6: ip = (*p(m1, 1) ? ip + 3 : *p(m2, 2)); + break; case 7: *p(m3, 3) = (*p(m1, 1) < *p(m2, 2)); ip += 4; + break; case 8: *p(m3, 3) = (*p(m1, 1) == *p(m2, 2)); ip += 4; break; default: abort(); } } } int main(void) { - for (int i = 0; EOF != scanf("%d,", &mem[i]); ++i); - run(); + int a[1024]; + for (int i = 0; EOF != scanf("%d,", &a[i]); ++i); + memcpy(mem, a, sizeof(mem)); + run(1); + memcpy(mem, a, sizeof(mem)); + ip = 0; + run(5); } -- cgit 1.4.1 ethod='get' action='/torus/log/termcap.diff'>
Commit message (Expand)Author
2018-04-09Use anonymous unionJune McEnroe
2018-04-09Replace #define with enum or const where possibleJune McEnroe
2018-03-31Link against ncursesJune McEnroe
2018-03-05Miscellaneous code cleanupJune McEnroe
2018-03-05Pass message structs by valueJune McEnroe
2018-03-05Use stdio in merge and metaJune McEnroe
2018-03-05Clean up spawn constantsJune McEnroe
2018-03-05Rename Tile timestamps {create,modify,access}TimeJune McEnroe
2018-03-05Pack message type enumsJune McEnroe
2018-03-05Undef COLOR_ constants in torus.hJune McEnroe
2018-03-05Generate tagsJune McEnroe
2017-10-03Simplify Makefile with pattern ruleJune McEnroe
2017-09-27Remove leading blank linesJune McEnroe
2017-09-27Add merge.c to READMEJune McEnroe
2017-09-03Assert client coords are valid after movementJune McEnroe
2017-09-03Relicense AGPLJune McEnroe
2017-09-01Revert "Add client readOnly mode"June McEnroe
2017-09-01Remove clientRemove call from clientCastJune McEnroe
2017-09-01Add client readOnly modeJune McEnroe
2017-08-31Clean up merge toolJune McEnroe
2017-08-31Choose B for tiles with equal modify timesJune McEnroe
2017-08-31Add quick data file merge toolJune McEnroe
2017-08-30Use only foreground color for selecting spawnJune McEnroe
2017-08-29Add four additional spawnsJune McEnroe
2017-08-28Add respawningJune McEnroe
2017-08-26Move license above includesJune McEnroe
2017-08-26Snapshot metadataJune McEnroe
2017-08-26Add meta.c to READMEJune McEnroe
2017-08-26Use MakefileJune McEnroe