Created
December 19, 2019 07:10
-
-
Save erikaderstedt/c6ca4ac890a3c76880a1acad7c3d490b to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// main.c | |
// p17 | |
// | |
// Created by Erik Aderstedt on 2019-12-16. | |
// Copyright © 2019 Aderstedt Software AB-> All rights reserved. | |
// | |
#include <stdio.h> | |
#include "intcode.h" | |
int64_t *source; | |
size_t length; | |
#define MAX_NUM_OUTPUTS (10) | |
int64_t beam_at(int64_t x, int64_t y) { | |
int64_t output[MAX_NUM_OUTPUTS]; | |
int64_t i[2]; | |
struct program *p = init_program_instance(source, length, output); | |
i[0] = x; | |
i[1] = y; | |
p->input = i; | |
p->remaining_inputs = 2; | |
run_program_instance_until_halted(p); | |
free_program_instance(p); | |
return output[0]; | |
} | |
int main(int argc, const char * argv[]) { | |
source = load_source_code_from_file(argv[1], &length, 1000); | |
size_t t = 0; | |
for (int64_t x = 0; x < 50; x++) for (int64_t y = 0; y < 50; y++) if (beam_at(x,y)) t++; | |
printf("Pt 1: %zu\n", t); | |
#define SQUARE_SIZE (100) | |
int64_t x = 0, y = SQUARE_SIZE; | |
while (1) { | |
while (!beam_at(x, y)) x++; | |
while (beam_at(x,y)) x++; | |
x--; | |
if (beam_at(x - (SQUARE_SIZE - 1), y + SQUARE_SIZE - 1)) { | |
printf("Pt 2: %lld\n", (x - (SQUARE_SIZE - 1))*10000 + y); | |
break; | |
} | |
y++; | |
} | |
free(source); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment