This commit is contained in:
Orangerot 2022-12-17 03:59:18 +01:00
parent 4a894a7b95
commit ba40688c4a
12 changed files with 2115 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

8
2022/day09/input1.txt Normal file
View file

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2

2000
2022/day09/input2.txt Normal file

File diff suppressed because it is too large Load diff

8
2022/day09/input3.txt Normal file
View file

@ -0,0 +1,8 @@
R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20

49
2022/day09/main.c Normal file
View file

@ -0,0 +1,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

50
2022/day09/main2.c Normal file
View file

@ -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