From 19625d5e7f22c64eca9c79c13f8eae9fbd8e3934 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Thu, 12 Jan 2023 14:10:28 +0000 Subject: 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. --- 22.c | 12 ++++++------ 1 file 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); -- cgit v1.2.1