summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2023-01-02 13:05:56 +0000
committerAdam Spragg <adam@spra.gg>2023-01-02 13:05:56 +0000
commit05a0668a056ce5d67a956ff1b84531602eb32cba (patch)
tree41b6fdd0753f0339350800e2b01ee17234566b29
parent103129a8c766dd213f90b7c23a7550005c3c20d3 (diff)
Puzzle 22: Store proposed next position as an elf
That way we will keep a proposed orientation change alongside the position change.
-rw-r--r--22.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/22.c b/22.c
index cd4a15d..83da420 100644
--- a/22.c
+++ b/22.c
@@ -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;
}
}