Skip to content

Commit

Permalink
Fix line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Jan 9, 2024
1 parent 2a38948 commit 0603141
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.txt text eol=lf
*.csv linguist-detectable
*.csv text eol=lf linguist-detectable
*.sh text eol=lf
112 changes: 62 additions & 50 deletions lib/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,24 @@ enum Opcode

typedef enum Opcode Opcode;

static Word emulator_on_input(Emulator instance)
static bool emulator_on_input(Emulator instance, Word* result)
{
if (instance->firstInput == -1)
{
return 0;
}

Word result = instance->inputs[instance->firstInput];

if (instance->firstInput == instance->lastInput)
{
instance->firstInput = -1;
instance->lastInput = -1;
}
else if (instance->firstInput == EMULATOR_INPUTS_CAPACITY - 1)
{
instance->firstInput = 0;
}
else
{
instance->firstInput++;
}

return result;
return queue_try_dequeue(&instance->inputs, result);
}

static void emulator_on_output(Emulator instance, Word word)
{
instance->outputs[instance->outputCount] = word;
instance->outputCount++;
queue_enqueue(&instance->outputs, word);
}

void emulator(Emulator instance, Word memory[])
{
instance->memory = memory;
instance->input = emulator_on_input;
instance->output = emulator_on_output;
instance->firstInput = -1;
instance->firstInput = -1;
instance->outputCount = 0;

queue(&instance->inputs, NULL, 0);
queue(&instance->outputs, NULL, 0);
}

static void emulator_move(
Expand Down Expand Up @@ -90,26 +68,6 @@ static Word emulator_load(Emulator instance, Word* instruction, int slot)
return value;
}

void emulator_input(Emulator instance, Word value)
{
if (instance->firstInput == -1)
{
instance->firstInput = 0;
instance->lastInput = 0;
}
else if (instance->firstInput &&
instance->lastInput == EMULATOR_INPUTS_CAPACITY - 1)
{
instance->lastInput = 0;
}
else
{
instance->lastInput++;
}

instance->inputs[instance->lastInput] = value;
}

void emulator_execute(Emulator instance)
{
Word* instruction = instance->memory;
Expand Down Expand Up @@ -144,8 +102,9 @@ void emulator_execute(Emulator instance)

case OPCODE_INPUT:
{
Word input = instance->input(instance);

Word input;

instance->input(instance, &input);
emulator_move(instance, instruction, input, 1);

instruction += 2;
Expand Down Expand Up @@ -213,3 +172,56 @@ void emulator_execute(Emulator instance)
}
}
}

void queue(Queue instance, Word buffer[], int capacity)
{
instance->items = buffer;
instance->first = -1;
instance->last = -1;
instance->capacity = capacity;
}

void queue_enqueue(Queue instance, Word item)
{
if (instance->first == -1)
{
instance->first = 0;
instance->last = 0;
}
else if (instance->first && instance->last == instance->capacity - 1)
{
instance->last = 0;
}
else
{
instance->last++;
}

instance->items[instance->last] = item;
}

bool queue_try_dequeue(Queue instance, Word* result)
{
if (instance->first == -1)
{
return false;
}

*result = instance->items[instance->first];

if (instance->first == instance->last)
{
instance->first = -1;
instance->last = -1;
}
else if (instance->first == instance->capacity - 1)
{
instance->first = 0;
}
else
{
instance->first++;
}

return true;
}
25 changes: 16 additions & 9 deletions lib/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@

#include <stdbool.h>
#include "word.h"
#define EMULATOR_INPUTS_CAPACITY 2
#define EMULATOR_OUTPUTS_CAPACITY 1

struct Queue
{
long* items;
int first;
int last;
int capacity;
};

struct Emulator
{
long* memory;

long (*input)(struct Emulator* instance);
bool (*input)(struct Emulator* instance, long* result);
void (*output)(struct Emulator* instance, long value);

long inputs[EMULATOR_INPUTS_CAPACITY];
long outputs[EMULATOR_OUTPUTS_CAPACITY];
int firstInput;
int lastInput;
int outputCount;
struct Queue inputs;
struct Queue outputs;
};

typedef struct Emulator* Emulator;
typedef struct Queue* Queue;

void emulator(Emulator instance, Word memory[]);
void emulator_input(Emulator instance, Word value);
void emulator_execute(Emulator instance);

void queue(Queue instance, Word buffer[], int capacity);
void queue_enqueue(Queue instance, Word item);
bool queue_try_dequeue(Queue instance, Word* result);
12 changes: 9 additions & 3 deletions src/day05a.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
#include <time.h>
#include "../lib/emulator.h"
#include "../lib/parser.h"
#define MEMORY 100000
#define INPUTS 1
#define MEMORY 4096
#define OUTPUTS 8

int main()
{
Word inputs[INPUTS];
Word memory[MEMORY];
Word outputs[OUTPUTS];
struct Emulator processor;
clock_t start = clock();

parser_parse(stdin, memory);
emulator(&processor, memory);
emulator_input(&processor, 1);
queue(&processor.inputs, inputs, INPUTS);
queue(&processor.outputs, outputs, OUTPUTS);
queue_enqueue(&processor.inputs, 1);
emulator_execute(&processor);
printf(
"05a " WORD_FORMAT " %lf\n",
processor.outputs[processor.outputCount - 1],
processor.outputs.items[processor.outputs.last],
(double)(clock() - start) / CLOCKS_PER_SEC);

return 0;
Expand Down
10 changes: 8 additions & 2 deletions src/day05b.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
#include <time.h>
#include "../lib/emulator.h"
#include "../lib/parser.h"
#define INPUTS 1
#define MEMORY 4096
#define OUTPUTS 8

int main()
{
Word inputs[INPUTS];
Word memory[MEMORY];
Word outputs[OUTPUTS];
struct Emulator processor;
clock_t start = clock();

parser_parse(stdin, memory);
emulator(&processor, memory);
emulator_input(&processor, 5);
queue(&processor.inputs, inputs, INPUTS);
queue(&processor.outputs, outputs, OUTPUTS);
queue_enqueue(&processor.inputs, 5);
emulator_execute(&processor);
printf(
"05b " WORD_FORMAT " %lf\n",
processor.outputs[processor.outputCount - 1],
processor.outputs.items[processor.outputs.last],
(double)(clock() - start) / CLOCKS_PER_SEC);

return 0;
Expand Down
14 changes: 10 additions & 4 deletions src/day07a.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <time.h>
#include "../lib/emulator.h"
#include "../lib/parser.h"
#define INPUTS 2
#define MEMORY 4096
#define OUTPUTS 100

struct PermutationIterator
{
Expand Down Expand Up @@ -74,6 +76,8 @@ int main()
Word max = 0;
Word programMemory[MEMORY];
Word sharedMemory[5][MEMORY];
Word inputs[5][INPUTS];
Word outputs[5][OUTPUTS];
struct PermutationIterator iter;
struct Emulator amplifiers[5];
int set[] = { 0, 1, 2, 3, 4 };
Expand All @@ -86,17 +90,19 @@ int main()
{
memcpy(sharedMemory[i], programMemory, length * sizeof(Word));
emulator(amplifiers + i, sharedMemory[i]);
queue(&amplifiers[i].inputs, inputs[i], INPUTS);
queue(&amplifiers[i].outputs, outputs[i], OUTPUTS);
}

Word output = 0;

for (int i = 0; i < 5; i++)
{
emulator_input(amplifiers + i, set[i]);
emulator_input(amplifiers + i, output);
queue_enqueue(&amplifiers[i].inputs, set[i]);
queue_enqueue(&amplifiers[i].inputs, output);
emulator_execute(amplifiers + i);

output = amplifiers[i].outputs[0];
output = amplifiers[i].outputs.items[amplifiers[i].outputs.first];
}

if (output > max)
Expand All @@ -106,7 +112,7 @@ int main()
}

printf(
"05b " WORD_FORMAT " %lf\n",
"07a " WORD_FORMAT " %lf\n",
max,
(double)(clock() - start) / CLOCKS_PER_SEC);

Expand Down
1 change: 1 addition & 0 deletions tools/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cat ../alt02.csv | .././day02a.o
cat ../alt02.csv | .././day02b.o
cat ../alt05.csv | .././day05a.o
cat ../alt05.csv | .././day05b.o
cat ../alt07.csv | .././day07a.o

0 comments on commit 0603141

Please sign in to comment.