-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path08.c
151 lines (128 loc) · 2.99 KB
/
08.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputs/08.h"
static void err(int status, char *message) {
fputs(message, stderr);
exit(status);
}
enum instruction_types { NOOP, ACC, JUMP };
struct Instruction {
int value;
enum instruction_types type;
char sign;
};
struct Instructions {
struct Instruction* values;
size_t cap;
size_t size;
};
static const unsigned char *
parse_instruction_line(const unsigned char* line, struct Instruction *ins) {
char buf[32];
int i = 0;
while (*line != ' ') {
buf[i++] = *line++;
};
buf[i] = '\0';
if (strcmp(buf, "nop") == 0) {
ins->type = NOOP;
} else if (strcmp(buf, "acc") == 0) {
ins->type = ACC;
} else if (strcmp(buf, "jmp") == 0) {
ins->type = JUMP;
} else {
err(EXIT_FAILURE, "Invalid op type");
}
// skip spaces
while (*line == ' ')
line++;
// read sign
ins->sign = *line == '+' ? 1 : -1;
line++;
// skip spaces
while (*line == ' ')
line++;
// read value
int v = 0;
while (*line != '\n' && *line != '\0') {
v = v * 10 + (*line - '0');
line++;
}
ins->value = v * ins->sign;
return line;
}
int day8(void) {
const unsigned char *s = input;
struct Instructions ins = {
.cap = 64,
.size = 0,
.values = malloc(64 * sizeof(struct Instruction)),
};
if (!ins.values) {
err(EXIT_FAILURE, "Error allocating memory for instruction values");
}
// Parse input file
while (*s != '\0') {
struct Instruction* i = &ins.values[ins.size++];
s = parse_instruction_line(s, i);
if (ins.size == ins.cap) {
ins.cap *= 2;
ins.values = realloc(ins.values, ins.cap * sizeof(struct Instruction));
if (!ins.values) {
err(EXIT_FAILURE, "Error allocating memory for instruction values");
}
}
if (*s == '\n') s++;
}
int* seen = malloc(ins.size * sizeof(int));
if (!seen) {
err(EXIT_FAILURE, "error allocating memory for seen array");
}
struct Instruction* i;
for (size_t c = 0; c < ins.size; c++) {
// skip ACC instructions
if (ins.values[c].type == ACC) {
continue;
}
int acc = 0;
size_t ip;
memset(seen, 0, ins.size * sizeof(0));
for (ip = 0; ip < ins.size; ip++) {
// break if we've seen this instruction already
if (seen[ip] == 1) {
break;
}
seen[ip] = 1;
i = &ins.values[ip];
enum instruction_types type = i->type;
if (c == ip) {
if (type == NOOP && i->value != 0) {
type = JUMP;
} else if (type == JUMP) {
type = NOOP;
}
}
switch (type) {
case NOOP:
// Do nothing special
break;
case ACC:
acc += i->value;
break;
case JUMP:
ip += i->value - 1;
break;
}
}
// Print result if we made it to end of program
if (ip == ins.size) {
printf("%d\n", acc);
assert(acc == 2092);
}
}
free(ins.values);
free(seen);
return 0;
}