Skip to content

Commit 5b6055f

Browse files
committed
Add day5a
1 parent ed0b675 commit 5b6055f

File tree

8 files changed

+129
-39
lines changed

8 files changed

+129
-39
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
a.out
2-
a.csv
2+
[0-9].csv
33
*.exe
44
*.o
55
.vscode/

Makefile

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ TWOS_COMPLEMENT = -fno-strict-overflow -fwrapv
66

77
all: \
88
day02a day02b \
9-
day03a day03b \
10-
day04a day04b \
119
day05a day05b \
1210
day06a day06b \
1311
day07a day07b \
@@ -44,20 +42,8 @@ day02a: src/day02a.c emulator parser
4442
day02b: src/day02b.c emulator parser
4543
$(CC) $(CFLAGS) $< -o $@.o emulator.o parser.o
4644

47-
day03a: src/day03a.c
48-
$(CC) $(CFLAGS) $< -o $@.o
49-
50-
day03b: src/day03b.c
51-
$(CC) $(CFLAGS) $< -o $@.o
52-
53-
day04a: src/day04a.c
54-
$(CC) $(CFLAGS) $< -o $@.o
55-
56-
day04b: src/day04b.c
57-
$(CC) $(CFLAGS) $< -o $@.o
58-
59-
day05a: src/day05a.c
60-
$(CC) $(CFLAGS) $< -o $@.o
45+
day05a: src/day05a.c emulator parser
46+
$(CC) $(CFLAGS) $< -o $@.o emulator.o parser.o
6147

6248
day05b: src/day05b.c
6349
$(CC) $(CFLAGS) $(TWOS_COMPLEMENT) $< -o $@.o

lib/emulator.c

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,91 @@
1+
#include <stdio.h>
12
#include "emulator.h"
2-
#include "opcode.h"
33

4-
void emulator_execute(Word memory[])
4+
enum Opcode
55
{
6-
Word* instruction = memory;
6+
OPCODE_ADD = 1,
7+
OPCODE_MULTIPLY = 2,
8+
OPCODE_INPUT = 3,
9+
OPCODE_OUTPUT = 4,
10+
OPCODE_TERMINATE = 99
11+
};
12+
13+
typedef enum Opcode Opcode;
14+
15+
Word emulator_load(Emulator instance, Word* instruction, int slot)
16+
{
17+
int denominator = 100;
18+
19+
for (int i = 0; i < slot - 1; i++)
20+
{
21+
denominator *= 10;
22+
}
23+
24+
Word value = instruction[slot];
25+
26+
if ((*instruction / denominator) % 10 == 0)
27+
{
28+
return instance->memory[value];
29+
}
30+
31+
return value;
32+
}
33+
34+
void emulator_execute(Emulator instance)
35+
{
36+
Word* instruction = instance->memory;
737

838
for (;;)
939
{
10-
switch (*instruction)
40+
Opcode opcode = *instruction % 100;
41+
42+
switch (opcode)
1143
{
1244
case OPCODE_ADD:
1345
{
14-
Word* a = memory + instruction[1];
15-
Word* b = memory + instruction[2];
16-
Word* result = memory + instruction[3];
46+
Word a = emulator_load(instance, instruction, 1);
47+
Word b = emulator_load(instance, instruction, 2);
48+
Word* result = instance->memory + instruction[3];
1749

18-
*result = *a + *b;
50+
*result = a + b;
1951
instruction += 4;
2052
}
2153
break;
2254

2355
case OPCODE_MULTIPLY:
2456
{
25-
Word* a = memory + instruction[1];
26-
Word* b = memory + instruction[2];
27-
Word* result = memory + instruction[3];
57+
Word a = emulator_load(instance, instruction, 1);
58+
Word b = emulator_load(instance, instruction, 2);
59+
Word* result = instance->memory + instruction[3];
2860

29-
*result = *a * *b;
61+
*result = a * b;
3062
instruction += 4;
3163
}
3264
break;
3365

66+
case OPCODE_INPUT:
67+
{
68+
Word* result = instance->memory + instruction[1];
69+
70+
if (scanf(WORD_FORMAT, result) != 1)
71+
{
72+
return;
73+
}
74+
75+
instruction += 2;
76+
}
77+
break;
78+
79+
case OPCODE_OUTPUT:
80+
{
81+
Word value = emulator_load(instance, instruction, 1);
82+
83+
printf(WORD_FORMAT "\n", value);
84+
85+
instruction += 2;
86+
}
87+
break;
88+
3489
case OPCODE_TERMINATE: return;
3590
}
3691
}

lib/emulator.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
#include "word.h"
22

3-
void emulator_execute(Word memory[]);
3+
struct Emulator
4+
{
5+
FILE* input;
6+
FILE* output;
7+
Word* memory;
8+
};
9+
10+
typedef struct Emulator* Emulator;
11+
12+
void emulator_execute(Emulator instance);

lib/opcode.h

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/day02a.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
int main(void)
1212
{
1313
Word memory[MEMORY];
14+
struct Emulator emulator =
15+
{
16+
.memory = memory
17+
};
1418
clock_t start = clock();
1519

1620
parser_parse(stdin, memory);
1721

1822
memory[1] = 12;
1923
memory[2] = 2;
2024

21-
emulator_execute(memory);
25+
emulator_execute(&emulator);
2226
printf(
2327
"02a " WORD_FORMAT " %lf\n",
2428
memory[0],

src/day02b.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ static Word scan(Word clean[], int length)
3737
int main(void)
3838
{
3939
Word memory[MEMORY];
40+
struct Emulator emulator =
41+
{
42+
.memory = memory
43+
};
4044
clock_t start = clock();
4145
Word product = scan(memory, parser_parse(stdin, memory));
4246

src/day05a.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed under the MIT license.
2+
3+
// Sunny with a Chance of Asteroids Part 1
4+
5+
#include <stdio.h>
6+
#include <time.h>
7+
#include "../lib/emulator.h"
8+
#include "../lib/parser.h"
9+
#define MEMORY 100000
10+
11+
int main(int count, ZString args[])
12+
{
13+
if (count < 2)
14+
{
15+
printf("Usage: day05a <program>\n");
16+
17+
return 1;
18+
}
19+
20+
Word memory[MEMORY];
21+
struct Emulator emulator =
22+
{
23+
.input = stdin,
24+
.output = stdout,
25+
.memory = memory
26+
};
27+
clock_t start = clock();
28+
29+
if (!parser_parse_file(args[1], memory))
30+
{
31+
fprintf(stderr, "Error: I/O.\n");
32+
33+
return 1;
34+
}
35+
36+
emulator_execute(&emulator);
37+
printf("05a %lf", (double)(clock() - start) / CLOCKS_PER_SEC);
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)