-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.c
189 lines (170 loc) · 4.41 KB
/
day8.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include "htab.h"
#include "lines.h"
#include <stdlib.h>
#include <sys/types.h>
typedef enum {
NOP = 0,
ACC = 1,
JMP = 2
} INS;
typedef struct {
INS i;
int op;
// list_t ls;
} instr_t;
typedef struct {
u_int64_t accum;
instr_t * instructions; /* a flexible buffer */
size_t ins_len;
size_t ins_cap;
} machine_t;
void machine_init(machine_t * m) {
m->ins_cap = 8; // start with at least 8.
m->instructions = calloc(sizeof(instr_t), m->ins_cap);
m->ins_len = 0;
}
void machine_free(machine_t *m) {
if (m->instructions)
free(m->instructions);
m->ins_len = 0;
m->ins_cap = 0;
}
void machine_sbrk(machine_t * m, size_t cap) {
if (cap <= m->ins_cap) return;
m->instructions = realloc(m->instructions, sizeof(instr_t) * cap);
assert(m->instructions);
m->ins_cap = cap;
}
void machine_ins_fromline(char * line, machine_t * m) {
if (!line) return;
if (m->ins_cap == 0) machine_init(m);
if (m->ins_cap == m->ins_len) {
// allocate more
machine_sbrk(m, m->ins_cap * 2);
}
instr_t * ins = &m->instructions[m->ins_len];
char * input = strdup(line);
char * start = strsep(&line, " ");
// grab the instruction that we want to add to
// program_t * prg = calloc(sizeof(program_t), 1);
ins->op = atoi(line);
if (strncmp(start, "nop",3) == 0)
ins->i = NOP;
else if (strncmp(start, "acc", 3) == 0) ins->i = ACC;
else if (strncmp(start, "jmp", 3) == 0) ins->i = JMP;
else assert(1 == 0); // fail
m->ins_len++;
free(input);
}
void machine_print_ins(machine_t * m) {
if (!m) return;
printf("Machine - cap:len [cap:len] [%ld:%ld]\n", m->ins_cap, m->ins_len);
for(int i = 0 ; i < m->ins_len; i++) {
printf("instr - %d, op - %d\n", m->instructions[i].i,
m->instructions[i].op);
}
}
void machine_ins_readall(lines_t * lines, machine_t * m) {
for (lines_t * l = lines ; l != NULL; l = lines_next(l)) {
machine_ins_fromline(l->line, m);
}
}
void detect_cycle(machine_t *m) {
int pc = 0;
m->accum = 0; // set the accumulator to zero.
bool seen[m->ins_len];
bzero(seen, sizeof(bool) * m->ins_len);
for(int i = 0 ; i < m->ins_len;i++) {
if (pc >= m->ins_len || pc < 0) break;
if (seen[pc]) break;
seen[pc] = true; // set this program counter as seen.
instr_t *itr = &m->instructions[pc];
switch (itr->i) {
case NOP:
pc++;
break;
case ACC:
m->accum += itr->op;
pc++;
break;
case JMP:
pc += itr->op;
break;
}
}
printf("detect-cycle: %d %lu\n", pc, m->accum);
m->accum = 0;
}
bool check_corrupt(machine_t *m) {
int pc = 0;
m->accum = 0; // set the accumulator to zero.
bool seen[m->ins_len];
bzero(seen, sizeof(bool) * m->ins_len);
for(int i = 0 ; i < m->ins_len;i++) {
if (pc >= m->ins_len || pc < 0) break;
if (seen[pc]) break;
seen[pc] = true; // set this program counter as seen.
instr_t *itr = &m->instructions[pc];
switch (itr->i) {
case NOP:
pc++;
break;
case ACC:
m->accum += itr->op;
pc++;
break;
case JMP:
pc += itr->op;
break;
}
}
if (pc == m->ins_len) {
printf("Valid Program! [pc:%d] [accum:%lu]\n", pc, m->accum);
m->accum = 0;
return false;
}
m->accum = 0;
return true;
}
/* toggles JMP and NOP, does nothing for ACC */
INS toggle(INS i) {
switch (i) {
case JMP: return NOP; break;
case NOP: return JMP; break;
default:
return i;
}
}
void fix_program(machine_t * m) {
// check for corruption.
// iterate through the instructions
// toggle every instruction
// if the program is valid, you are done.
// if the program is not valid, then toggle that instruction again and go to the next one.
if(!check_corrupt(m)) return; // what if the program is valid, return it.
for(int i = 0 ; i < m->ins_len; i++){
m->instructions[i].i = toggle(m->instructions[i].i); // toggle the instruction
if (check_corrupt(m)) {
m->instructions[i].i = toggle(m->instructions[i].i); // still corrupt toggle it back
} else {
return;
}
}
printf("!!no correct program found!!\n");
}
int main() {
char * filename = "day8_input.txt";
lines_t * lines = read_lines(filename);
print_lines(lines);
machine_t m = {0};
machine_init(&m);
machine_ins_readall(lines, &m);
machine_print_ins(&m);
detect_cycle(&m);
printf("%d\n", check_corrupt(&m));
fix_program(&m);
machine_free(&m);
free_lines(lines);
return 0;
}