summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2023-01-02 11:45:01 +0000
committerAdam Spragg <adam@spra.gg>2023-01-02 11:45:01 +0000
commit6b2cac18fd1c8cba3149f42c2c458ea1e05c6fb4 (patch)
treea69bfef7bde9a7f64b5e3715f7a5da06c0dcd25c
parenta56b3551c19e4fb3a860d86bc2bf3e18a68080d9 (diff)
Puzzle 22: Allow for moving around changing our orientation
-rw-r--r--22.c50
1 files changed, 25 insertions, 25 deletions
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);