From a52c78cebcb47106f900b933b3bc5c65a94e459e Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Wed, 28 Dec 2022 15:07:24 +0000 Subject: Cache nearby elf positions --- 23.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to '23.c') diff --git a/23.c b/23.c index 99a3fb7..bc5291d 100644 --- a/23.c +++ b/23.c @@ -176,50 +176,46 @@ int trymove(char * buf, int cols, int rows, int round, int i, int j) { static char const dirs[4] = { DIR_N, DIR_S, DIR_W, DIR_E }; + int e_n, e_s, e_w, e_e, e_nw, e_ne, e_sw, e_se; int d, dir; if (!loc_elf(*pos(buf, cols, rows, i, j))) // ??? return -1; - if (!loc_elf(*posd(buf, cols, rows, i, j, DIR_N)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_S)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_W)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_E)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NW)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NE)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SW)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SE))) + e_n = loc_elf(*posd(buf, cols, rows, i, j, DIR_N)); + e_s = loc_elf(*posd(buf, cols, rows, i, j, DIR_S)); + e_w = loc_elf(*posd(buf, cols, rows, i, j, DIR_W)); + e_e = loc_elf(*posd(buf, cols, rows, i, j, DIR_E)); + e_nw = loc_elf(*posd(buf, cols, rows, i, j, DIR_NW)); + e_ne = loc_elf(*posd(buf, cols, rows, i, j, DIR_NE)); + e_sw = loc_elf(*posd(buf, cols, rows, i, j, DIR_SW)); + e_se = loc_elf(*posd(buf, cols, rows, i, j, DIR_SE)); + + if (!e_n && !e_s && !e_w && !e_e + && !e_nw && !e_ne && !e_sw && !e_se) // No elves nearby, do not move. return 0; for (d = 0, dir = 0; d < arrlen(dirs) && dir == 0; ++d) { switch (dirs[(round + d) % 4]) { case DIR_N: - if (!loc_elf(*posd(buf, cols, rows, i, j, DIR_N)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NW)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NE))) + if (!e_n && !e_nw && !e_ne) dir = DIR_N; break; case DIR_S: - if (!loc_elf(*posd(buf, cols, rows, i, j, DIR_S)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SW)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SE))) + if (!e_s && !e_sw && !e_se) dir = DIR_S; break; case DIR_W: - if (!loc_elf(*posd(buf, cols, rows, i, j, DIR_W)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NW)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SW))) + if (!e_w && !e_nw && !e_sw) dir = DIR_W; break; case DIR_E: - if (!loc_elf(*posd(buf, cols, rows, i, j, DIR_E)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_NE)) - && !loc_elf(*posd(buf, cols, rows, i, j, DIR_SE))) + if (!e_e && !e_ne && !e_se) dir = DIR_E; break; -- cgit v1.2.1