summaryrefslogtreecommitdiff
path: root/2022/day09/main2.c
diff options
context:
space:
mode:
Diffstat (limited to '2022/day09/main2.c')
-rw-r--r--2022/day09/main2.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/2022/day09/main2.c b/2022/day09/main2.c
new file mode 100644
index 0000000..da2382c
--- /dev/null
+++ b/2022/day09/main2.c
@@ -0,0 +1,50 @@
+#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[10] = {0};
+ while (scanf("%c %d\n", &c, &d) != EOF)
+ {
+ printf("%c %d\n", c, d);
+ for (int i = 0; i < d; i++)
+ {
+ h[0].x += (c == 'R') - (c == 'L');
+ h[0].y += (c == 'U') - (c == 'D');
+ printf("H %d %d \n", h->x, h->y);
+ for (int i = 0; i < 9; i++)
+ {
+ updateTail(&h[i], &h[i+1]);
+ }
+ printf("T %d %d \n", h[9].x, h[9].y);
+ }
+ }
+
+}
+
+// ./main < input2.txt | grep T | sort | uniq | wc -l
+
+