Skip to content

Commit 30ab122

Browse files
committed
Add Day 8 solution
1 parent 5945af7 commit 30ab122

File tree

4 files changed

+1322
-15
lines changed

4 files changed

+1322
-15
lines changed

Day-08/c/JohnnyJayJay/Day08.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "../../../shared/JohnnyJayJay/aoc.h"
5+
6+
typedef struct {
7+
char* opcode;
8+
int arg;
9+
} instruction;
10+
11+
typedef struct {
12+
int* possible_corruption;
13+
int possible_corruption_len;
14+
int acc;
15+
int infinite_loop;
16+
} execution_result;
17+
18+
execution_result* run_program(int length, instruction* insts) {
19+
int* visits = calloc(sizeof(int), length);
20+
int acc = 0;
21+
int* possible_corruption = malloc(sizeof(int) * length);
22+
int possible_corruption_len = 0;
23+
int infinite_loop = 0;
24+
for (int i = 0; i < length; i++) {
25+
if (visits[i]) {
26+
infinite_loop = 1;
27+
break;
28+
}
29+
visits[i] = 1;
30+
instruction* inst = &insts[i];
31+
char* opcode = inst->opcode;
32+
int arg = inst->arg;
33+
if (strcmp(opcode, "acc") == 0) {
34+
acc += arg;
35+
} else {
36+
possible_corruption[possible_corruption_len] = i;
37+
possible_corruption_len++;
38+
if (strcmp(opcode, "jmp") == 0) {
39+
i += arg - 1;
40+
}
41+
}
42+
43+
}
44+
free(visits);
45+
execution_result* res = malloc(sizeof(execution_result));
46+
res->possible_corruption = possible_corruption;
47+
res->possible_corruption_len = possible_corruption_len;
48+
res->acc = acc;
49+
res->infinite_loop = infinite_loop;
50+
return res;
51+
}
52+
53+
void free_execution_result(execution_result* result) {
54+
free(result->possible_corruption);
55+
free(result);
56+
}
57+
58+
void fix_program(int length, instruction* insts, int* possible_corruption, int possible_corruption_len) {
59+
for (int i = 0; i < possible_corruption_len; i++) {
60+
int pos = possible_corruption[i];
61+
instruction old = insts[pos];
62+
instruction new = {strcmp(old.opcode, "jmp") == 0 ? "nop" : "jmp", old.arg};
63+
insts[pos] = new;
64+
execution_result* result = run_program(length, insts);
65+
int infinite_loop = result->infinite_loop;
66+
free_execution_result(result);
67+
if (infinite_loop) {
68+
insts[pos] = old;
69+
} else {
70+
return;
71+
}
72+
}
73+
}
74+
75+
76+
int main(int argc, char** argv) {
77+
FILE* file = fopen(argv[1], "r");
78+
int lines = count_lines(file);
79+
instruction* insts = malloc(sizeof(instruction) * lines);
80+
for (int i = 0; i < lines; i++) {
81+
char* opcode = malloc(4);
82+
int arg;
83+
fscanf(file, "%3s %d", opcode, &arg);
84+
instruction inst = {opcode, arg};
85+
insts[i] = inst;
86+
}
87+
88+
execution_result* result = run_program(lines, insts);
89+
printf("The accumulator is %d\n", result->acc);
90+
fix_program(lines, insts, result->possible_corruption, result->possible_corruption_len);
91+
result = run_program(lines, insts);
92+
printf("The fixed accumulator is %d\n", result->acc);
93+
}

0 commit comments

Comments
 (0)