From 6b2cac18fd1c8cba3149f42c2c458ea1e05c6fb4 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Mon, 2 Jan 2023 11:45:01 +0000 Subject: Puzzle 22: Allow for moving around changing our orientation --- 22.c | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to '22.c') diff --git a/22.c b/22.c index f29f78e..bcde74a 100644 --- a/22.c +++ b/22.c @@ -78,38 +78,38 @@ elf_turn(struct elf * elf, char turn) int elf_move(struct elf * elf, char const * map, int cols, int rows, int distance) { - int skip, wrap, base; + while (distance > 0) { + int skip, wrap, base, newpos; - switch (elf->dir) { - case D_NORTH: - skip = -cols; - wrap = cols * rows; - break; + switch (elf->dir) { + case D_NORTH: + skip = -cols; + wrap = cols * rows; + break; - case D_EAST: - skip = 1; - wrap = cols; - break; + case D_EAST: + skip = 1; + wrap = cols; + break; - case D_SOUTH: - skip = cols; - wrap = cols * rows; - break; + case D_SOUTH: + skip = cols; + wrap = cols * rows; + break; - case D_WEST: - skip = -1; - wrap = cols; - break; + case D_WEST: + skip = -1; + wrap = cols; + break; - default: - fprintf(stderr, "Unexpected direction %d\n", elf->dir); - return -1; - } + default: + fprintf(stderr, "Unexpected direction %d\n", elf->dir); + return -1; + } - base = (elf->pos / wrap) * wrap; + base = (elf->pos / wrap) * wrap; - while (distance > 0) { - int newpos = base + modulo(elf->pos + skip, wrap); + newpos = base + modulo(elf->pos + skip, wrap); while (map[newpos] == ' ' || map[newpos] == '\n') newpos = base + modulo(newpos + skip, wrap); -- cgit v1.2.1