Last active
December 11, 2019 12:36
-
-
Save erikaderstedt/f05d436a7edd0bad9a64c62ed52c6f93 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 120 | |
#define HEIGHT 120 | |
#define WHITE (1) | |
#define BLACK (0) | |
#define UNPAINTED_BLACK (-1) | |
int main(int argc, char **argv) { | |
int64_t output[10]; | |
size_t length; | |
int64_t *source = load_source_code_from_file(argv[1], &length, 10000); | |
int8_t *grid = (int8_t *)calloc(WIDTH*HEIGHT, 1); | |
bool pt2 = false; | |
bool done = false; | |
while (!done) { | |
struct program *p = init_program_instance(source, length, output); | |
int64_t x = WIDTH*0.5; | |
int64_t y = HEIGHT*0.5; | |
int64_t dir = 0; // UP | |
int64_t min_x = WIDTH; | |
int64_t max_x = 0; | |
int64_t min_y = HEIGHT; | |
int64_t max_y = 0; | |
memset(grid, UNPAINTED_BLACK, WIDTH*HEIGHT); | |
grid[y*WIDTH + x] = pt2 ? WHITE : UNPAINTED_BLACK; | |
while (true) { | |
int64_t input = (grid[y*WIDTH+x] == WHITE) ? WHITE : BLACK; | |
p->input = &input; | |
p->remaining_inputs = 1; | |
if (run_program_instance_until_halted(p)) { break; } | |
grid[x+y*WIDTH] = output[0]; | |
dir = (dir + ((output[1] == 0) ? 3 : 1)) % 4; | |
switch (dir) { | |
case 0: | |
y--; break; | |
case 1: | |
x++; break; | |
case 2: | |
y++; break; | |
case 3: | |
x--; break; | |
} | |
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) { | |
fprintf(stderr, "out of bounds %lld %lld\n", x, y); | |
exit(1); | |
} | |
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; | |
} | |
if (!pt2) { | |
int count = 0; | |
for (size_t i = 0; i < WIDTH*HEIGHT; i++) { if (grid[i] != UNPAINTED_BLACK) count++; } | |
printf("Pt 1: %d\n", count); | |
pt2 = true; | |
} else { | |
printf("Pt 2:\n"); | |
for (int64_t y = min_y; y <= max_y; y++) { | |
printf("| "); | |
for (int64_t x = min_x; x <= max_x; x++) { | |
printf("%s", (grid[x + y*WIDTH] == WHITE) ? "#" : " "); | |
} | |
printf(" |\n"); | |
} | |
done = true; | |
} | |
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