leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 744caf9e9a66e71b0f52bdc32a11b48ed5f18a73
parent 4fe15f34625c6c58cd08695a0b657860a863abc7
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon,  6 Apr 2026 21:15:59 -0600

2069. walking robot simulation 2

Diffstat:
Awalking_robot_simulation2.c | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Awalking_robot_simulation2.py | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 184 insertions(+), 0 deletions(-)

diff --git a/walking_robot_simulation2.c b/walking_robot_simulation2.c @@ -0,0 +1,108 @@ +// 2069. Walking Robot Simulation II +#include "leetcode.h" + +typedef struct { + int pos; + int len; + int height; + int width; + int c1; + int c2; + int c3; + int c4; + bool init; +} Robot; + +Robot *robotCreate(int width, int height) { + Robot *obj = (Robot *)malloc(sizeof(Robot)); + obj->pos = 0; + obj->len = (height + width - 2) * 2; + obj->width = width - 1; + obj->height = height - 1; + obj->c1 = width - 1; + obj->c2 = width + height - 2; + obj->c3 = width * 2 + height - 3; + obj->c4 = (width + height - 2) * 2; + obj->init = true; + return obj; +} + +void robotStep(Robot *obj, int num) { + obj->pos = (obj->pos + num) % obj->len; + if (num > 0) + obj->init = false; +} + +int *robotGetPos(Robot *obj, int *retSize) { + int *pos = (int *)malloc(2 * sizeof(int)); + *retSize = 2; + if (obj->pos <= obj->c1) { + pos[0] = obj->pos; + pos[1] = 0; + } else if (obj->pos <= obj->c2) { + pos[0] = obj->width; + pos[1] = obj->pos - obj->c1; + } else if (obj->pos <= obj->c3) { + pos[0] = obj->c3 - obj->pos; + pos[1] = obj->height; + } else { + pos[0] = 0; + pos[1] = obj->c4 - obj->pos; + } + return pos; +} + +char *robotGetDir(Robot *obj) { + char *dir = (char *)malloc(6 * sizeof(char)); + if (!obj->pos && obj->init) { + strcpy(dir, "East"); + return dir; + } + if (!obj->pos) { + strcpy(dir, "South"); + return dir; + } + if (obj->pos <= obj->c1) + strcpy(dir, "East"); + else if (obj->pos <= obj->c2) + strcpy(dir, "North"); + else if (obj->pos <= obj->c3) + strcpy(dir, "West"); + else + strcpy(dir, "South"); + return dir; +} + +void robotFree(Robot *obj) { free(obj); } + +int main() { + Robot *obj = robotCreate(6, 3); + robotStep(obj, 2); + robotStep(obj, 2); + int r1[] = {4, 0}, rs1, *rgp1 = robotGetPos(obj, &rs1); + for (int i = 0; i < rs1; i++) { + printf("%d ", rgp1[i]); + assert(rgp1[i] == r1[i]); + } + printf("\n"); + char *r2 = "East", *rgd1 = robotGetDir(obj); + printf("%s\n", rgd1); + assert(!strcmp(rgd1, r2)); + robotStep(obj, 2); + robotStep(obj, 1); + robotStep(obj, 4); + int r3[] = {1, 2}, rs2, *rgp2 = robotGetPos(obj, &rs2); + for (int i = 0; i < rs2; i++) { + printf("%d ", rgp2[i]); + assert(rgp2[i] == r3[i]); + } + printf("\n"); + char *r4 = "West", *rgd2 = robotGetDir(obj); + printf("%s\n", rgd2); + assert(!strcmp(rgd2, r4)); + free(rgd1); + free(rgd2); + free(rgp1); + free(rgp2); + robotFree(obj); +} diff --git a/walking_robot_simulation2.py b/walking_robot_simulation2.py @@ -0,0 +1,76 @@ +# 2069. Walking Robot Simulation II +from typing import List + + +class Robot: + def __init__(self, width: int, height: int): + self.width = width + self.height = height + self.x = 0 + self.y = 0 + self.dir = 0 + self.per = 2 * (width + height) - 4 + + def step(self, num: int) -> None: + if self.per == 0: + return + num %= self.per + if num == 0: + if self.x == 0 and self.y == 0: + self.dir = 3 + return + + while num > 0: + if self.dir == 0: + move = min(num, self.width - 1 - self.x) + self.x += move + num -= move + if num > 0: + self.dir = 1 + + elif self.dir == 1: + move = min(num, self.height - 1 - self.y) + self.y += move + num -= move + if num > 0: + self.dir = 2 + + elif self.dir == 2: + move = min(num, self.x) + self.x -= move + num -= move + if num > 0: + self.dir = 3 + + else: + move = min(num, self.y) + self.y -= move + num -= move + if num > 0: + self.dir = 0 + + def getPos(self) -> List[int]: + return [self.x, self.y] + + def getDir(self) -> str: + if self.dir == 0: + return "East" + elif self.dir == 1: + return "North" + elif self.dir == 2: + return "West" + else: + return "Soutn" + + +if __name__ == "__main__": + obj = Robot(6, 3) + obj.step(2) + obj.step(2) + print(obj.getPos()) + print(obj.getDir()) + obj.step(2) + obj.step(1) + obj.step(4) + print(obj.getPos()) + print(obj.getDir())