From fb84fd04675c2b6c7e283f07ee3771b03e49d87b Mon Sep 17 00:00:00 2001 From: June 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') 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 den' name='showmsg' value='1'/>
Commit message (Collapse)Author
2021-02-04Use owner-filter to add Liberapay donation buttonsJune McEnroe
2021-02-04Use owner-filter for repo page headersJune McEnroe
Previously it was only used if owners were displayed on the index.
2021-02-04Warn twice about stopped jobsJune McEnroe
Not really sure why this previously set job_warning to 2 rather than 1. Anyway I often just press ^D again after the warning without really thinking, so do it twice.
2021-02-04Remove cgit logo using cgitrcJune McEnroe
It's not documented that you can set logo to empty string to remove it, I just noticed by reading print_header.
2021-01-31Add The Rosewater RedemptionJune McEnroe
Remembering how to read 2021.
2021-01-29Use int when checking for EOFJune McEnroe
2021-01-28Move gpl.c and agpl.c templates out of etcJune McEnroe
2021-01-27Set para and sect for mdocJune McEnroe
Copied from the OpenBSD defaults.
2021-01-26Use First state to match keyword at beginning of lineJune McEnroe
Otherwise keywords at the beginnings of lines with preceeding whitespace are not matched.
2021-01-25Install vi on Linux and nvi on macOSJune McEnroe
2021-01-25Use vi and lessJune McEnroe
I haven't used neovim in a couple weeks by now, so I may as well commit this.
2021-01-20Use mtags for sh in source-filterJune McEnroe
2021-01-20Generate tags for sh files in mtagsJune McEnroe
Only matches functions declared at the beginnings of lines, but I'm fine with that.
2021-01-20Add messy sh lexerJune McEnroe
Surprisingly seems to work for everything I looked at in my repos.
2021-01-20Add all target to git.causal.agency MakefileJune McEnroe
2021-01-20Remove Lua supportJune McEnroe
Lua support is unused and the dlsym fwrite/write hacks horrify me. Clean it up.
2021-01-20Fix tests for diff spansJune McEnroe
2021-01-20Avoid matching ':' in make tagsJune McEnroe
Otherwise a :: rule includes one of the ':'s in the tag name.
2021-01-19Prefer tag matches not preceded by [[:alnum:]]June McEnroe
Otherwise the "id" in "void" matches for "void id".
2021-01-19Escape \ and / in mtags search patternsJune McEnroe
2021-01-20Use mtags in source-filterJune McEnroe
2021-01-19Add mtags to generate tags for make and mdocJune McEnroe
2021-01-19Map tags to IDs using only [[:alnum:]-._]June McEnroe
2021-01-19Don't use a pager if reading standard inputJune McEnroe
2021-01-19Support BSD make syntax and match *.amJune McEnroe
These don't really go together, but...
2021-01-19Match tab following escaped newline in make assignmentsJune McEnroe
Otherwise it ends up going into Shell state.
2021-01-18Allow matching lexers using first input lineJune McEnroe