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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#define XLEN 139 //10
#define YLEN 137 //9
#define LEN (XLEN * YLEN)
typedef struct Sea_cuc_move {
int from_pos;
int to_pos;
} Sea_cuc_move;
char map[LEN];
char map_bak[LEN];
void ParseInput(char *filepath)
{
char ch;
FILE *fp;
fp = fopen(filepath, "r");
if(fp == NULL)
{
fprintf(stderr, "ERROR: something with file idk what fuck you");
exit(EXIT_FAILURE);
}
int i = 0;
while((ch = fgetc(fp)) != EOF)
{
if(ch == '\n') continue;
map[i] = ch;
i++;
}
fclose(fp);
}
void Move(char type, Sea_cuc_move pos)
{
map[pos.from_pos] = '.';
map[pos.to_pos] = type;
}
void MoveEast()
{
Sea_cuc_move positions_east[5000];
int cucs_east = 0;
for(int i=0; i<LEN; i++)
{
if(map[i] == '>')
{
int x = i % XLEN;
if(x == (XLEN-1))
{
if(map[i - x] != '.')
continue;
positions_east[cucs_east] = (Sea_cuc_move){i, i - x};
cucs_east += 1;
}
else if(map[i+1] == '.')
{
positions_east[cucs_east] = (Sea_cuc_move){i, i + 1};
cucs_east += 1;
}
}
}
assert(cucs_east < 5000);
if(cucs_east != 0)
for(int i=0; i<cucs_east; i++)
Move('>', positions_east[i]);
}
void MoveSouth()
{
Sea_cuc_move positions_south[5000];
int cucs_south = 0;
for(int i=0; i<LEN; i++)
{
if(map[i] == 'v')
{
int y = floor(i / XLEN);
if(y == (YLEN-1))
{
if(map[i%XLEN] != '.')
continue;
positions_south[cucs_south] = (Sea_cuc_move){i, i%XLEN};
cucs_south += 1;
}
else if (map[i+XLEN] == '.')
{
positions_south[cucs_south] = (Sea_cuc_move){i, i+XLEN};
cucs_south +=1;
}
}
}
assert(cucs_south < 5000);
if(cucs_south != 0)
for(int i=0; i<cucs_south; i++)
Move('v', positions_south[i]);
}
void NextStep()
{
MoveEast();
MoveSouth();
}
void PrintMap()
{
for(int i=0; i<LEN; i++)
{
if((i % XLEN) == 0)
printf("\n");
printf("%c", map[i]);
}
printf("\n\n");
}
int main()
{
ParseInput("input.txt");
for(u_int64_t i=0; i < 10000; i++)
{
memcpy(map_bak, map, sizeof(char) * LEN);
NextStep();
if(strcmp(map, map_bak) == 0)
{
printf("EQUAL after: %llu\n", i + 1);
exit(0);
}
}
PrintMap();
return 0;
}
|