red 4 лет назад
Родитель
Сommit
918011f731
4 измененных файлов: 67 добавлений и 5 удалений
  1. +2
    -1
      CMakeLists.txt
  2. +47
    -0
      day7.c
  3. +17
    -4
      intcode.c
  4. +1
    -0
      intcode.h

+ 2
- 1
CMakeLists.txt Просмотреть файл

@@ -8,4 +8,5 @@ add_executable(day2 day2.c)
add_executable(day3 day3.c)
add_executable(day4 day4.c)
add_executable(day5 day5.c intcode.c)
add_executable(day6 day6.c)
add_executable(day6 day6.c)
add_executable(day7 day7.c intcode.c)

+ 47
- 0
day7.c Просмотреть файл

@@ -0,0 +1,47 @@
//
// Created by red on 07/12/19.
//

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "intcode.h"

bool in (int* arr, size_t len, int key) {
for (size_t i = 0; i < len; i++){
if (arr[i] == key) return true;
}
return false;
}

int main(int argc, char **argv) {
char* filename = argv[1];
intcode* v = read_file(filename, 2048);
int max = 0;
for(size_t i = 0; i < 3125; i++) {
int bits[5];
int num = (int) i;
for(size_t j = 0; j < 5; j++) {
bits[j] = num % 5; // read the jth bit
num = num / 5; // move to the next bit
}
bool isperm = true;
for(size_t j = 0; j < 5; j++) {
if (in(bits+j+1, 5-j-1, bits[j])) {
isperm = false;
}
}
if (isperm == false) continue;
int input[2] = {0, 0};
for(size_t j = 0; j < 5; j++) {
intcode* m = clone(v, 2048);
input[0] = bits[j];
m->input_tape = input;
ival out_val = run(m);
input[1] = out_val.num;
}
if (input[1] > max) max = input[1];
}
printf("max: %d\n", max);
return EXIT_SUCCESS;
}

+ 17
- 4
intcode.c Просмотреть файл

@@ -109,6 +109,7 @@ ival op_jnz (intcode* m) {
} else {
m->index += 3;
}
free((void*) operands);
return cont_val();
}

@@ -119,6 +120,7 @@ ival op_jez (intcode* m) {
} else {
m->index += 3;
}
free((void*) operands);
return cont_val();
}

@@ -130,6 +132,7 @@ ival op_lt (intcode* m) {
} else result = 0;
m->tape[m->tape[m->index+3]] = result;
m->index += 4;
free((void*) operands);
return cont_val();
}

@@ -141,6 +144,7 @@ ival op_eq (intcode* m) {
} else result = 0;
m->tape[m->tape[m->index+3]] = result;
m->index += 4;
free((void*) operands);
return cont_val();
}

@@ -168,15 +172,14 @@ ival run (intcode* m) {
ival v = step(m);
switch (v.type) {
case IVAL_ERR:
case IVAL_END: return v;
case IVAL_OUT: ival_print(v);
break;
case IVAL_END:
case IVAL_OUT: return v;
default: break;
}
}
};

intcode *read_file(char *filename, size_t buf_size) {
intcode* read_file(char *filename, size_t buf_size) {
int* tape = calloc(buf_size, sizeof(int));
memset((void*) tape, 0, sizeof(int)*buf_size);
FILE* file = fopen(filename, "r");
@@ -193,3 +196,13 @@ intcode *read_file(char *filename, size_t buf_size) {
v.tape_l = i;
return &v;
}

intcode *clone(intcode *m, size_t buf_size) {
static intcode v;
int* tape = calloc(buf_size, sizeof(int));
memcpy((void*) tape, (void*) m->tape, sizeof(int)*m->tape_l);
v.tape = tape;
v.index = 0;
v.tape_l = m->tape_l;
return &v;
}

+ 1
- 0
intcode.h Просмотреть файл

@@ -24,4 +24,5 @@ typedef struct {
ival step (intcode* m);
ival run (intcode* m);
intcode* read_file(char* filename, size_t buf_size);
intcode* clone(intcode* m, size_t buf_size);
#endif //AOC2019_INTCODE_H

Загрузка…
Отмена
Сохранить