day09
This commit is contained in:
parent
4a894a7b95
commit
ba40688c4a
BIN
2022/day01/main
BIN
2022/day01/main
Binary file not shown.
BIN
2022/day03/main
BIN
2022/day03/main
Binary file not shown.
BIN
2022/day04/main
BIN
2022/day04/main
Binary file not shown.
BIN
2022/day05/main
BIN
2022/day05/main
Binary file not shown.
BIN
2022/day06/main
BIN
2022/day06/main
Binary file not shown.
BIN
2022/day07/main
BIN
2022/day07/main
Binary file not shown.
BIN
2022/day08/main
BIN
2022/day08/main
Binary file not shown.
8
2022/day09/input1.txt
Normal file
8
2022/day09/input1.txt
Normal 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
2000
2022/day09/input2.txt
Normal file
File diff suppressed because it is too large
Load diff
8
2022/day09/input3.txt
Normal file
8
2022/day09/input3.txt
Normal 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
49
2022/day09/main.c
Normal 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
50
2022/day09/main2.c
Normal 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue