summaryrefslogtreecommitdiff
path: root/2022/day09/main.c
blob: 7f19df5e97209ae7cfa14351655b45a44051aaa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>

typedef struct Cord_ Cord;

typedef struct Cord_ {
    int x,y;
    Cord *next, *prev;
} Cord;

Cord* updateTail(Cord *head, Cord *tail)
{
    int dy, dx, d;
    dx = head->x - tail->x;
    dy = head->y - tail->y;
    d = dx * dx + dy * dy;
    // distance <= 1
    if ( d <= 2) return tail;
    tail->x += (dx + dx/abs(dx))/2;
    tail->y += (dy + dy/abs(dy))/2;

    return tail;
}

int main()
{
    char c;
    int d;
    Cord *h, *t;
    t = &(Cord){0};
    h = &(Cord){0};
    while (scanf("%c %d\n", &c, &d) != EOF)
    {
        printf("%c %d\n", c, d);
        for (int i = 0; i < d; i++)
        {
            h->x += (c == 'R') - (c == 'L');
            h->y += (c == 'U') - (c == 'D');
            printf("H %d %d \n", h->x, h->y);
            t = updateTail(h, t);
            printf("T %d %d \n", t->x, t->y);
        }
    }

}

// ./main < input2.txt | grep T | sort | uniq | wc -l