Browse Source

day 8

master
red 4 years ago
parent
commit
bba37862ae
7 changed files with 97 additions and 80 deletions
  1. +2
    -1
      CMakeLists.txt
  2. +4
    -6
      d1-10/day8.c
  3. +3
    -2
      intcode/day5.c
  4. +3
    -3
      intcode/day7.c
  5. +17
    -0
      intcode/day9.c
  6. +63
    -65
      intcode/intcode.c
  7. +5
    -3
      intcode/intcode.h

+ 2
- 1
CMakeLists.txt View File

@@ -10,4 +10,5 @@ add_executable(day4 d1-10/day4.c)
add_executable(day5 intcode/day5.c intcode/intcode.c)
add_executable(day6 d1-10/day6.c)
add_executable(day7 intcode/day7.c intcode/intcode.c)
add_executable(day8 d1-10/day8.c)
add_executable(day8 d1-10/day8.c)
add_executable(day9 intcode/day9.c intcode/intcode.c)

+ 4
- 6
d1-10/day8.c View File

@@ -10,12 +10,11 @@ int main(int argv, char** argc) {
char* filename = argc[1];
FILE* f = fopen(filename, "r");
char buffer[15001];
fscanf(f, "%s", buffer);
char final[150];
memset((void*) final, '2', 150*sizeof(char));
fscanf(f, "%s", buffer);
int min_0s = 150;
int no_1s = 0;
int no_2s = 0;
int checksum = 0;
for (size_t layer = 0; layer < 100; layer++) {
int num_0s = 0;
int num_1s = 0;
@@ -32,11 +31,10 @@ int main(int argv, char** argc) {
}
if (num_0s < min_0s) {
min_0s = num_0s;
no_1s = num_1s;
no_2s = num_2s;
checksum = num_1s * num_2s;
}
}
printf("Smallest no. of 0s found: %d, checksum: %d\n", min_0s, no_1s*no_2s);
printf("Smallest no. of 0s found: %d, checksum: %d\n", min_0s, checksum);
for (size_t i = 0; i < 150; i++) {
printf("%c", final[i]);
if (i % 25 == 24) {

+ 3
- 2
intcode/day5.c View File

@@ -9,7 +9,8 @@
int main(int argc, char **argv) {
char* filename = argv[1];
intcode* v = read_file(filename, 2048);
int input[1] = { 5 };
long input[1] = { 5 };
v->input_tape = input;
run(v);
ival out_val = run(v);
ival_print(out_val);
}

+ 3
- 3
intcode/day7.c View File

@@ -17,7 +17,7 @@ bool in (int* arr, size_t len, int key) {
int main(int argc, char **argv) {
char* filename = argv[1];
intcode* v = read_file(filename, 2048);
int max = 0;
long max = 0;
for(size_t i = 0; i < 3125; i++) {
int bits[5];
int num = (int) i;
@@ -32,7 +32,7 @@ int main(int argc, char **argv) {
}
}
if (isperm == false) continue;
int input[2] = {0, 0};
long input[2] = {0, 0};
for(size_t j = 0; j < 5; j++) {
intcode* m = clone(v, 2048);
input[0] = bits[j];
@@ -42,6 +42,6 @@ int main(int argc, char **argv) {
}
if (input[1] > max) max = input[1];
}
printf("max: %d\n", max);
printf("max: %ld\n", max);
return EXIT_SUCCESS;
}

+ 17
- 0
intcode/day9.c View File

@@ -0,0 +1,17 @@
//
// Created by red on 10/12/19.
//

#include "intcode.h"

int main(int argc, char **argv) {
char *filename = argv[1];
intcode *v = read_file(filename, 2 << 24);
long input[1] = { 2 };
v->input_tape = input;
for (;;) {
ival out_val = run(v);
ival_print(out_val);
if (out_val.type == IVAL_END || out_val.type == IVAL_ERR) break;
}
}

+ 63
- 65
intcode/intcode.c View File

@@ -7,14 +7,14 @@
#include <stdlib.h>
#include "intcode.h"

ival out_val(int n) {
ival out_val(long n) {
ival v;
v.type = IVAL_OUT;
v.num = n;
return v;
}

ival err_val(int n) {
ival err_val(long n) {
ival v;
v.type = IVAL_ERR;
v.err = n;
@@ -44,7 +44,7 @@ void ival_print(ival v) {
}
}
case IVAL_OUT: {
printf("Output value: %d\n", v.num);
printf("Output value: %ld\n", v.num);
}
}
}
@@ -54,113 +54,109 @@ int power (int base, int pow) {
else return base*power(base,pow-1);
}

int* get_operands (intcode* m, size_t num_operands) {
int* operands = calloc(num_operands, sizeof(int));
int opcode = m->tape[m->index] / 100;
for (size_t i = 0; i < num_operands; i++) {
int addr_mode = opcode % 10;
void get_operands (intcode* m, long** operands) {
long opcode = m->tape[m->index] / 100;
for (size_t i = 0; i < 3; i++) {
long addr_mode = opcode % 10;
opcode = opcode / 10;
int v = 0;
if (addr_mode == 0) {
v = m->tape[m->tape[m->index+i+1]];
long* v = 0;
switch (addr_mode) {
case 0: v = &m->tape[m->tape[m->index + i + 1]]; break;
case 1: v = &m->tape[m->index + i + 1]; break;
case 2: {
long delta = m->tape[m->index + i + 1];
v = &m->tape[m->relative_base + delta];
}
}
else v = m->tape[m->index+i+1];
operands[i] = v;
}
return operands;
}

ival op_add (intcode* m) {
int* operands = get_operands(m, 2);
m->tape[m->tape[m->index+3]] = operands[0] + operands[1];
ival op_add (intcode* m, long** operands) {
*operands[2] = *operands[0] + *operands[1];
m->index += 4;
free((void*) operands);
return cont_val();
}

ival op_mul (intcode* m) {
int* operands = get_operands(m, 2);
m->tape[m->tape[m->index+3]] = operands[0] * operands[1];
ival op_mul (intcode* m, long** operands) {
*operands[2] = *operands[0] * *operands[1];
m->index += 4;
free((void*) operands);
return cont_val();
}

ival op_in (intcode* m) {
int i = m->tape[m->index+1];
ival op_in (intcode* m, long** operands) {
*operands[0] = m->input_tape[0];
m->index += 2;
m->tape[i] = m->input_tape[0];
m->input_tape = m->input_tape+1;
return cont_val();
}

ival op_out (intcode* m) {
int* operands = get_operands(m, 1);
ival op_out (intcode* m, long** operands) {
m->index += 2;
int v = operands[0];
free((void*) operands);
long v = *operands[0];
return out_val(v);
}

ival op_jnz (intcode* m) {
int* operands = get_operands(m, 2);
if (operands[0] != 0) {
m->index = (size_t) operands[1];
ival op_jnz (intcode* m, long** operands) {
if (*operands[0] != 0) {
m->index = (size_t) *operands[1];
} else {
m->index += 3;
}
free((void*) operands);
return cont_val();
}

ival op_jez (intcode* m) {
int* operands = get_operands(m, 2);
if (operands[0] == 0) {
m->index = (size_t) operands[1];
ival op_jez (intcode* m, long** operands) {
if (*operands[0] == 0) {
m->index = (size_t) *operands[1];
} else {
m->index += 3;
}
free((void*) operands);
return cont_val();
}

ival op_lt (intcode* m) {
int* operands = get_operands(m,2);
int result;
if (operands[0] < operands[1]) {
ival op_lt (intcode* m, long** operands) {
long result;
if (*operands[0] < *operands[1]) {
result = 1;
} else result = 0;
m->tape[m->tape[m->index+3]] = result;
*operands[2] = result;
m->index += 4;
free((void*) operands);
return cont_val();
}

ival op_eq (intcode* m) {
int* operands = get_operands(m,2);
int result;
if (operands[0] == operands[1]) {
ival op_eq (intcode* m, long** operands) {
long result;
if (*operands[0] == *operands[1]) {
result = 1;
} else result = 0;
m->tape[m->tape[m->index+3]] = result;
*operands[2] = result;
m->index += 4;
free((void*) operands);
return cont_val();
}

ival op_rbs (intcode* m, long** operands) {
m->relative_base += *operands[0];
m->index += 2;
return cont_val();
}

ival step (intcode* m) {
int opcode = m->tape[m->index];
long* operands[3];
get_operands(m, operands);
long opcode = m->tape[m->index];
opcode = opcode % 100;
switch (opcode) {
case 99: return end_val();
case 1: return op_add(m);
case 2: return op_mul(m);
case 3: return op_in(m);
case 4: return op_out(m);
case 5: return op_jnz(m);
case 6: return op_jez(m);
case 7: return op_lt(m);
case 8: return op_eq(m);
case 1: return op_add(m, operands);
case 2: return op_mul(m, operands);
case 3: return op_in(m, operands);
case 4: return op_out(m, operands);
case 5: return op_jnz(m, operands);
case 6: return op_jez(m, operands);
case 7: return op_lt(m, operands);
case 8: return op_eq(m, operands);
case 9: return op_rbs(m, operands);
default: return err_val(IERR_BAD_OP);
}
}
@@ -180,13 +176,13 @@ ival run (intcode* m) {
};

intcode* read_file(char *filename, size_t buf_size) {
int* tape = calloc(buf_size, sizeof(int));
memset((void*) tape, 0, sizeof(int)*buf_size);
long* tape = calloc(buf_size, sizeof(long));
memset((void*) tape, 0, sizeof(long)*buf_size);
FILE* file = fopen(filename, "r");
size_t i;
for (i = 0; i < buf_size; i++) {
int a;
int err = fscanf(file, "%d,", &a);
long a;
long err = fscanf(file, "%ld,", &a);
if (err == EOF) break;
tape[i] = a;
}
@@ -194,15 +190,17 @@ intcode* read_file(char *filename, size_t buf_size) {
v.tape = tape;
v.index = 0;
v.tape_l = i;
v.relative_base = 0;
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);
long* tape = calloc(buf_size, sizeof(long));
memcpy((void*) tape, (void*) m->tape, sizeof(long)*m->tape_l);
v.tape = tape;
v.index = 0;
v.tape_l = m->tape_l;
v.relative_base = m->relative_base;
return &v;
}

+ 5
- 3
intcode/intcode.h View File

@@ -12,15 +12,17 @@ enum {IERR_BAD_OP, IERR_OUT_OF_BOUNDS};
enum {IVAL_OUT, IVAL_ERR, IVAL_END, IVAL_CONT};
typedef struct {
int type;
int num;
long num;
int err;
} ival;
typedef struct {
int* tape;
long* tape;
size_t tape_l;
size_t index;
int* input_tape;
long* input_tape;
long relative_base;
} intcode;
void ival_print(ival v);
ival step (intcode* m);
ival run (intcode* m);
intcode* read_file(char* filename, size_t buf_size);

Loading…
Cancel
Save