Created
December 13, 2019 07:14
-
-
Save erikaderstedt/a1a94b6ec0a22fd975f838ff548c76f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#include "intcode.h" | |
#define WIDTH 50 | |
#define HEIGHT 30 | |
#define MAX_NUM_OUTPUTS 5000 | |
size_t blocks_remaining(int8_t *grid) { | |
size_t b = 0; | |
for (size_t x = 0; x < WIDTH*HEIGHT; x++) if (grid[x] == 2) b++; | |
return b; | |
} | |
int main(int argc, char **argv) { | |
int64_t output[MAX_NUM_OUTPUTS]; | |
size_t length; | |
int64_t *source = load_source_code_from_file(argv[1], &length, 10000); | |
int8_t *grid = (int8_t *)calloc(WIDTH*HEIGHT, 1); | |
// int64_t min_x = INT64_MAX; | |
// int64_t max_x = INT64_MIN; | |
// int64_t min_y = INT64_MAX; | |
// int64_t max_y = INT64_MIN; | |
bool done = false; | |
struct program *p = init_program_instance(source, length, output); | |
p->input = NULL; | |
p->remaining_inputs = 0; | |
while (!done) { | |
done = run_program_instance_until_halted(p); | |
for (int64_t i = 0; i < p->num_outputs; i += 3) { | |
int64_t x = output[i]; | |
int64_t y = output[i+1]; | |
grid[x+y*WIDTH] = output[i+2]; | |
// if (max_x < x) max_x = x; | |
// if (min_y > y) min_y = y; | |
// if (min_x > x) min_x = x; | |
// if (max_y < y) max_y = y; | |
} | |
} | |
printf("Pt 1: %zu\n", blocks_remaining(grid)); | |
free_program_instance(p); | |
// printf("x: (%lld to %lld)\nY: (%lld to %lld)\n", min_x, max_x, min_y, max_y); | |
// for (int64_t y = min_y; y < max_y; y++) { | |
// for (int64_t x = min_x; x < max_x; x++) { | |
// int8_t g = grid[y*WIDTH + x]; | |
// switch(g) { | |
// case 0: printf(" "); break; | |
// case 1: printf("#"); break; | |
// case 2: printf("@"); break; | |
// case 3: printf("-"); break; | |
// case 4: printf("o"); break; | |
// } | |
// } | |
// printf("\n"); | |
// } | |
p = init_program_instance(source, length, output); | |
p->code[0] = 2; // Free play!!!! | |
int64_t input = 0; | |
p->input = &input; | |
p->remaining_inputs = 1; | |
done = false; | |
#define UNKNOWN (-100111011) | |
int64_t score = 0; | |
int64_t ball_x = UNKNOWN; | |
int64_t ball_y = UNKNOWN; | |
int64_t paddle_x = 0; | |
int64_t paddle_y = 0; | |
while (!done) { | |
done = run_program_instance_until_halted(p); | |
for (int64_t i = 0; i < p->num_outputs; i += 3) { | |
int64_t x = output[i]; | |
int64_t y = output[i+1]; | |
if (x == -1 && y == 0) { | |
score = output[i+2]; | |
} else { | |
grid[x+y*WIDTH] = output[i+2]; | |
} | |
if (output[i+2] == 3) { | |
paddle_x = x; | |
paddle_y = y; | |
} else if (output[i+2] == 4) { | |
ball_y = y; | |
ball_x = x; | |
} | |
} | |
if (paddle_x > ball_x) { | |
input = -1; | |
} else if (paddle_x < ball_x) { | |
input = 1; | |
} else { | |
input = 0; | |
} | |
p->input = &input; | |
p->remaining_inputs = 1; | |
if (blocks_remaining(grid) == 0) { | |
done = true; | |
printf("Pt 2: %lld\n", score); | |
} | |
} | |
free_program_instance(p); | |
free(grid); | |
free(source); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment