Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- enum {max_size = 2000};
- struct cmd {
- char dir;
- int distance;
- };
- struct location {
- int hx, hy, tx, ty;
- };
- void updateLoc(struct location* l, struct cmd* c) {
- char dir = c->dir;
- if (dir == 'U') {
- --l->hy;
- } else if (dir == 'D') {
- ++l->hy;
- } else if (dir == 'L') {
- --l->hx;
- } else if (dir == 'R') {
- ++l->hx;
- } else {
- printf("Direction %c not recognised\n", dir);
- }
- printf("head now at %d,%d\n", l->hx, l->hy);
- // tail follows
- if (l->hx - l->tx > 1)
- ++l->tx;
- if (l->tx - l->hx > 1)
- --l->tx;
- if (l->hy - l->ty > 1)
- ++l->ty;
- if (l->ty - l->hy > 1)
- --l->ty;
- }
- int part1(struct cmd cmds[], int size) {
- printf("in p1\n");
- int grid[max_size][max_size] = {0};
- struct location l;
- l.hx = 1000;
- l.hy = 1000;
- l.tx = 1000;
- l.ty = 1000;
- for (int i = 0; i < size; ++i) {
- printf("command %c %d\n", cmds[i].dir, cmds[i].distance);
- for (int j = 0; j < cmds[i].distance; ++j)
- updateLoc(&l, &cmds[i]);
- printf("location updated\n");
- grid[l.ty][l.tx] = 1;
- }
- int count = 0;
- for (int x = 0; x < max_size; ++x) {
- for (int y = 0; y < max_size; ++y) {
- if (grid[y][x] != 0)
- ++count;
- }
- }
- return count;
- }
- int main() {
- int i = 0;
- struct cmd list[max_size];
- int ret = 2;
- while (ret == 2) {
- ret = scanf("%c %d\n", &list[i].dir, &list[i].distance);
- if (ret < 2) break;
- ++i;
- }
- printf("read %d intructions\n", i);
- int value = part1(list, i);
- printf("%d\n", value);
- //printf("%d\n", part2(list, i));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement