-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog15.c
221 lines (201 loc) · 4.5 KB
/
prog15.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <alloca.h>
#include <termios.h>
#include "intcode.h"
#include "printu.h"
int64_t program[] = {
#include "inputs/input15"
};
#define UNKNOWN 0xFFFF
#define GOAL 0xFFFE
#define WALL 0xFFFD
#define OXYGEN 0xFFFC
#define OXYGEN_STEP 0xFFFB
#define LAST_TYPE 0xFFFA
int part1_count = 0;
int max_y = 0;
int bot_x = 50;
int bot_y = 50;
int area[100][100];
int goal_dist = 0;
void draw() {
printU("\e[1;1H");
for (int i = bot_y - 20; i < bot_y + 20; i++) {
for (int j = 0; j < 100; j++) {
switch (area[i][j]) {
case UNKNOWN: printf(" "); break;
case WALL: printf("#"); break;
case GOAL: printf("@"); break;
case OXYGEN: printf("O"); break;
default: printf("%d", area[i][j] % 10); break;
}
}
printf("\n");
}
int d = area[bot_y][bot_x];
if (d == GOAL) d = goal_dist;
printf("%d, %d %d\n", bot_x, bot_y, d);
printfU("\e[%d;%dH", 21, bot_x + 1);
}
struct termios stored_settings;
void setup_term()
{
tcgetattr(0, &stored_settings);
struct termios new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_lflag &= (~ECHO); // don't echo the character
//new_settings.c_cc[VTIME] = 1; // timeout (tenths of a second)
//new_settings.c_cc[VMIN] = 0; // minimum number of characters
tcsetattr(0, TCSANOW, &new_settings);
}
void update_square(int64_t out, int dx, int dy)
{
int * old_pos = &area[bot_y][bot_x];
if (out == 0) {
area[bot_y + dy][bot_x + dx] = WALL;
}
if (out == 1) {
bot_x += dx;
bot_y += dy;
if (area[bot_y][bot_x] == UNKNOWN) {
area[bot_y][bot_x] = *old_pos + 1;
}
}
if (out == 2) {
if (area[bot_y][bot_x] == UNKNOWN) {
goal_dist = *old_pos + 1;
}
bot_x += dx;
bot_y += dy;
area[bot_y][bot_x] = GOAL;
}
}
void interactive_mode(struct intcode_machine * m)
{
setup_term();
while (1) {
char c;
draw();
read(STDIN_FILENO, &c, 1);
int64_t move = 0;
int dx = 0, dy = 0;
if (c == 'k') { move = 1; dx = 0; dy = -1; }
if (c == 'j') { move = 2; dx = 0; dy = 1; }
if (c == 'h') { move = 3; dx = -1; dy = 0; }
if (c == 'l') { move = 4; dx = 1; dy = 0; }
if (c == 'q') { break; }
PUT(m->inputs, move);
run_machine(m);
int64_t out = GET(m->outputs);
update_square(out, dx, dy);
}
}
int update_oxygen(int x, int y)
{
int updates = 0;
if (area[y - 1][x] < LAST_TYPE) {
area[y - 1][x] = OXYGEN_STEP;
updates++;
}
if (area[y + 1][x] < LAST_TYPE) {
area[y + 1][x] = OXYGEN_STEP;
updates++;
}
if (area[y][x - 1] < LAST_TYPE) {
area[y][x - 1] = OXYGEN_STEP;
updates++;
}
if (area[y][x + 1] < LAST_TYPE) {
area[y][x + 1] = OXYGEN_STEP;
updates++;
}
printf("\e[40;1HUPDATES %d\n", updates);
return updates;
}
int main(int argc, char * argv[])
{
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
area[i][j] = UNKNOWN;
}
}
area[bot_y][bot_x] = 0;
struct intcode_machine * m = NEW_INTCODE_MACHINE(program, _I());
int move = 1;
int64_t out;
int dx = 0;
int dy = 0;
int * first_wall = NULL;
int * first_pos = &area[bot_y][bot_x];
int * goal_pos = NULL;
while (1) {
switch (move) {
case 1: dx = 0; dy = -1; break;
case 2: dx = 0; dy = 1; break;
case 3: dx = -1; dy = 0; break;
case 4: dx = 1; dy = 0; break;
}
PUT(m->inputs, move);
run_machine(m);
out = GET(m->outputs);
update_square(out, dx, dy);
draw();
if (out == 2) {
goal_pos = &area[bot_y][bot_x];
}
if (out != 0) {
switch (move) {
case 1: move = 4; break;
case 2: move = 3; break;
case 3: move = 1; break;
case 4: move = 2; break;
}
} else {
int * this_wall = &area[bot_y + dy][bot_x + dx];
int * this_pos = &area[bot_y][bot_x];
if (first_wall == NULL) {
first_wall = this_wall;
first_pos = &area[bot_y][bot_x];
} else {
if (this_wall == first_wall && this_pos == first_pos) {
break;
}
}
switch (move) {
case 1: move = 3; break;
case 2: move = 4; break;
case 3: move = 2; break;
case 4: move = 1; break;
}
}
// if we moved
usleep(1 * 1000);
}
*goal_pos = OXYGEN;
int steps = 0;
int updates = 1;
while (updates != 0) {
updates = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (area[i][j] == OXYGEN) {
updates += update_oxygen(j, i);
}
}
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (area[i][j] == OXYGEN_STEP) {
area[i][j] = OXYGEN;
}
}
}
steps++;
draw();
usleep(50 * 1000);
}
printf("\n\nSTEPS!!! %d\n\n", steps - 1); // subtract 1 for the last step
return 0;
}