aoc

advent of code
git clone git://source.orangerot.dev:/aoc.git
Log | Files | Refs

main.c (939B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct Cord_ Cord;
      5 
      6 typedef struct Cord_ {
      7     int x,y;
      8     Cord *next, *prev;
      9 } Cord;
     10 
     11 Cord* updateTail(Cord *head, Cord *tail)
     12 {
     13     int dy, dx, d;
     14     dx = head->x - tail->x;
     15     dy = head->y - tail->y;
     16     d = dx * dx + dy * dy;
     17     // distance <= 1
     18     if ( d <= 2) return tail;
     19     tail->x += (dx + dx/abs(dx))/2;
     20     tail->y += (dy + dy/abs(dy))/2;
     21 
     22     return tail;
     23 }
     24 
     25 int main()
     26 {
     27     char c;
     28     int d;
     29     Cord *h, *t;
     30     t = &(Cord){0};
     31     h = &(Cord){0};
     32     while (scanf("%c %d\n", &c, &d) != EOF)
     33     {
     34         printf("%c %d\n", c, d);
     35         for (int i = 0; i < d; i++)
     36         {
     37             h->x += (c == 'R') - (c == 'L');
     38             h->y += (c == 'U') - (c == 'D');
     39             printf("H %d %d \n", h->x, h->y);
     40             t = updateTail(h, t);
     41             printf("T %d %d \n", t->x, t->y);
     42         }
     43     }
     44 
     45 }
     46 
     47 // ./main < input2.txt | grep T | sort | uniq | wc -l
     48 
     49