diff options
Diffstat (limited to '22.c')
-rw-r--r-- | 22.c | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -86,7 +86,8 @@ int elf_move(struct elf * elf, char const * map, int cols, int rows, enum map_type mtype, int distance) { while (distance > 0) { - int skip, wrap, base, newpos; + int skip, wrap, base; + struct elf newelf; switch (elf->dir) { case D_NORTH: @@ -116,14 +117,15 @@ elf_move(struct elf * elf, char const * map, int cols, int rows, enum map_type m base = (elf->pos / wrap) * wrap; - newpos = base + modulo(elf->pos + skip, wrap); + newelf.pos = base + modulo(elf->pos + skip, wrap); + newelf.dir = elf->dir; - if (map[newpos] == ' ' || map[newpos] == '\n') { + if (map[newelf.pos] == ' ' || map[newelf.pos] == '\n') { switch (mtype) { case M_FLAT: // Just keep going until we wrap around to a non-blank position - while (map[newpos] == ' ' || map[newpos] == '\n') - newpos = base + modulo(newpos + skip, wrap); + while (map[newelf.pos] == ' ' || map[newelf.pos] == '\n') + newelf.pos = base + modulo(newelf.pos + skip, wrap); break; case M_NET: @@ -136,9 +138,9 @@ elf_move(struct elf * elf, char const * map, int cols, int rows, enum map_type m } } - switch (map[newpos]) { + switch (map[newelf.pos]) { case '.': - elf->pos = newpos; + *elf = newelf; --distance; break; @@ -147,7 +149,7 @@ elf_move(struct elf * elf, char const * map, int cols, int rows, enum map_type m break; default: - fprintf(stderr, "Unexpected map character %c\n", map[newpos]); + fprintf(stderr, "Unexpected map character %c\n", map[newelf.pos]); return -1; } } |