summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2023-01-12 14:10:28 +0000
committerAdam Spragg <adam@spra.gg>2023-01-12 14:10:28 +0000
commit19625d5e7f22c64eca9c79c13f8eae9fbd8e3934 (patch)
tree8445a85986dffa642ffba169e5f1945cdc66eddc
parent5e0e0d306116c348336a872e105d4396de7b0cfe (diff)
Puzzle 22: Change coordinate system so that 'y' goes up
Having 'y' go down was easier with navigating through the memory buffer we were using to store the map, but some of the work we'll have to do with the cube net is easier for me to get my head around with a more traditional coordinate system.
-rw-r--r--22.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/22.c b/22.c
index a021510..df55cdb 100644
--- a/22.c
+++ b/22.c
@@ -114,7 +114,7 @@ map_elem(struct map const * map, int x, int y)
|| y < 0 || y >= map->rows)
return ' ';
- return map->_buf[x + y * map->cols];
+ return map->_buf[x + (map->rows - 1 - y) * map->cols];
}
@@ -170,8 +170,8 @@ elf_move(struct elf * elf, struct map const * map, int distance)
struct elf newelf;
switch (elf->dir) {
- case D_NORTH: dy = -1; break;
- case D_SOUTH: dy = +1; break;
+ case D_NORTH: dy = +1; break;
+ case D_SOUTH: dy = -1; break;
case D_EAST: dx = +1; break;
case D_WEST: dx = -1; break;
default:
@@ -304,7 +304,7 @@ main(int argc, char ** argv)
// Set initial position
elf.x = 0;
- elf.y = 0;
+ elf.y = map.rows - 1;
elf.dir = D_EAST;
while (elf.x < map.cols && map_elem(&map, elf.x, elf.y) != '.')
++elf.x;
@@ -344,8 +344,8 @@ main(int argc, char ** argv)
// Done.
printf("Password is %d (%d,%d,%d)\n",
- 1000 * (1 + elf.y) + 4 * (1 + elf.x) + elf.dir,
- 1 + elf.x, 1 + elf.y, elf.dir);
+ 1000 * (map.rows - elf.y) + 4 * (1 + elf.x) + elf.dir,
+ 1 + elf.x, map.rows - elf.y, elf.dir);
// Tidy and exit.
map_tidy(&map);