From e3c6ffbf7306cca7d940b90e5fa8da17b9095510 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 12 Dec 2020 20:54:33 +0000 Subject: [PATCH] impl moves --- day12/Makefile | 2 +- day12/day12.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/day12/Makefile b/day12/Makefile index dcb9b4c..cfb6e96 100644 --- a/day12/Makefile +++ b/day12/Makefile @@ -4,7 +4,7 @@ OPT_FLAGS?= -march=native -flto \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -fno-stack-check -fno-strict-aliasing -CFLAGS+= -Wall -pedantic --std=gnu11 -Wextra -Wstrict-aliasing +CFLAGS+= -Wall -pedantic --std=gnu11 -Wstrict-aliasing CFLAGS+= $(OPT_FLAGS) CFLAGS+= -O3 -pipe LDFLAGS?= -O3 -flto diff --git a/day12/day12.c b/day12/day12.c index 800ea09..273ae85 100644 --- a/day12/day12.c +++ b/day12/day12.c @@ -64,6 +64,34 @@ inline static void modsign(struct ship* restrict ship, i64 by) *mod += sign * by; } +noglobal static inline enum direction rotr(enum direction s) +{ +#define DIRECT(n) return n; case n + switch(s) + { + case DIR_NORTH: + DIRECT(DIR_EAST): + DIRECT(DIR_SOUTH): + DIRECT(DIR_WEST): + return DIR_NORTH; + default: panic("Cannot rotate direction '%c', (%d)", (char)s, (int) s); + } +} +noglobal static inline enum direction rotl(enum direction s) +{ + switch(s) + { + case DIR_NORTH: + DIRECT(DIR_WEST): + DIRECT(DIR_SOUTH): + DIRECT(DIR_EAST): + return DIR_NORTH; + default: panic("Cannot rotate direction '%c', (%d)", (char)s, (int) s); + } +} +#undef DIRECT + + static void handle_com(struct ship* restrict ship, command_t com) { switch(com.dir) @@ -73,11 +101,10 @@ static void handle_com(struct ship* restrict ship, command_t com) case DIR_NORTH: ship->pos.y -= com.num; break; case DIR_SOUTH: ship->pos.y += com.num; break; - case DIR_FORWARD: modsign(ship, com.num); break; - - case DIR_LEFT: //TODO: - case DIR_RIGHT: //TODO: + case DIR_LEFT: ship->facing = rotl(ship->facing); if(0) + case DIR_RIGHT: ship->facing = rotr(ship->facing); + case DIR_FORWARD: modsign(ship, com.num); break; default: panic("Unknown command direction '%c' (%d)", (char)com.dir, (int)com.dir); } }