aboutsummaryrefslogtreecommitdiff
path: root/2020/08
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
committerThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
commite7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch)
tree237261eef3afd0720be77dbcbb9599fa66a24b67 /2020/08
Initial commit
Diffstat (limited to '2020/08')
-rw-r--r--2020/08/.gitignore1
-rw-r--r--2020/08/Makefile7
-rw-r--r--2020/08/input653
-rw-r--r--2020/08/puzzles.c112
4 files changed, 773 insertions, 0 deletions
diff --git a/2020/08/.gitignore b/2020/08/.gitignore
new file mode 100644
index 0000000..60d075d
--- /dev/null
+++ b/2020/08/.gitignore
@@ -0,0 +1 @@
+puzzle-[12]
diff --git a/2020/08/Makefile b/2020/08/Makefile
new file mode 100644
index 0000000..c32dafa
--- /dev/null
+++ b/2020/08/Makefile
@@ -0,0 +1,7 @@
+all:
+ ${CC} ${CFLAGS} -o puzzle-1 puzzles.c
+ ${CC} ${CFLAGS} -DPART2 -o puzzle-2 puzzles.c
+
+.PHONY: clean
+clean:
+ rm -f puzzle-[12]
diff --git a/2020/08/input b/2020/08/input
new file mode 100644
index 0000000..49eb92d
--- /dev/null
+++ b/2020/08/input
@@ -0,0 +1,653 @@
+acc +9
+acc -2
+acc -12
+acc +33
+jmp +301
+nop +508
+jmp +216
+acc +27
+acc +35
+acc +43
+acc +31
+jmp +309
+acc +18
+acc -19
+acc +7
+jmp +44
+acc -13
+acc -17
+acc +31
+jmp +311
+nop +612
+jmp +143
+acc +22
+nop +85
+jmp +458
+acc -3
+jmp +13
+acc -19
+acc +27
+acc +12
+jmp +483
+acc +40
+acc +6
+jmp +128
+jmp +10
+acc +0
+acc -3
+acc -2
+jmp -11
+acc +43
+acc -12
+jmp +158
+acc +0
+jmp +240
+jmp +1
+acc +5
+acc +15
+jmp +187
+nop +563
+jmp +51
+acc -16
+jmp +158
+jmp +322
+acc +47
+nop -1
+jmp +299
+acc +26
+acc +25
+jmp +232
+jmp -9
+acc +15
+jmp +54
+jmp +558
+acc +7
+acc -7
+jmp +399
+nop +447
+jmp +71
+acc +26
+acc +46
+jmp +145
+acc +38
+acc +30
+acc +21
+jmp +263
+acc +10
+jmp +168
+acc +22
+nop +561
+jmp -26
+jmp +1
+acc -7
+jmp -5
+acc +28
+acc -6
+jmp +370
+jmp +94
+acc +50
+acc +42
+acc -9
+acc +30
+jmp +70
+acc +29
+jmp +166
+acc -5
+acc -18
+nop +84
+acc +2
+jmp +366
+jmp -40
+acc -4
+acc -15
+acc -1
+jmp +169
+jmp +1
+acc -4
+acc +0
+jmp -45
+nop -21
+nop +241
+acc -18
+acc +19
+jmp +26
+nop -51
+jmp +260
+acc +17
+jmp +428
+acc +6
+jmp +405
+acc +22
+acc +10
+nop +471
+jmp +352
+acc -6
+acc +48
+acc +7
+acc +3
+jmp +57
+acc -10
+acc +16
+acc +16
+acc +43
+jmp +432
+acc -5
+acc +0
+nop +339
+acc +49
+jmp +17
+acc +33
+nop +166
+acc -5
+jmp +392
+nop +246
+acc -7
+acc +21
+acc +30
+jmp +398
+acc +36
+acc +24
+acc -15
+acc -9
+jmp +114
+acc +19
+jmp +11
+acc +43
+nop +182
+jmp -129
+nop -29
+acc -6
+acc +2
+jmp +398
+jmp +78
+acc +36
+jmp +393
+acc +15
+nop -11
+acc -7
+acc -9
+jmp +76
+acc +0
+acc +27
+jmp +25
+acc +27
+nop -54
+jmp +458
+acc +3
+acc +29
+acc -4
+acc +43
+jmp +413
+acc +33
+acc +13
+jmp +382
+jmp -83
+acc +42
+acc +24
+jmp +64
+acc +23
+acc -13
+nop +110
+acc -5
+jmp +114
+jmp +113
+nop +112
+acc +26
+jmp -133
+jmp -12
+jmp +1
+jmp +330
+acc +25
+acc -1
+acc +30
+acc +42
+jmp -187
+jmp +1
+acc +20
+acc +35
+acc +36
+jmp -125
+jmp +165
+acc +28
+acc -17
+acc -12
+jmp +1
+jmp -120
+nop +1
+acc +2
+acc +26
+jmp +398
+acc +20
+acc -1
+jmp -127
+acc +36
+acc +14
+jmp +1
+jmp +331
+acc +50
+acc +1
+acc -10
+nop +159
+jmp -83
+jmp +374
+acc +17
+jmp +372
+acc +44
+nop -39
+jmp +228
+acc +17
+jmp +74
+acc +16
+acc +33
+acc -2
+jmp +152
+jmp +29
+acc +8
+acc +27
+nop +59
+jmp -32
+acc +28
+jmp -227
+nop -35
+jmp -168
+acc +13
+nop +390
+jmp -204
+acc +16
+acc +44
+jmp -230
+jmp +25
+acc +30
+jmp +383
+acc -11
+acc +38
+acc +11
+jmp +341
+acc +35
+acc +46
+acc -1
+jmp +94
+acc -4
+acc +12
+jmp +111
+jmp +133
+nop +283
+acc +13
+acc +37
+jmp +74
+nop -218
+jmp -178
+acc +46
+acc +25
+acc -5
+jmp -174
+acc +28
+acc +39
+acc +36
+acc +22
+jmp -172
+acc +19
+jmp -250
+nop +62
+acc +44
+nop +347
+acc +40
+jmp +345
+acc -3
+acc -13
+acc -11
+jmp +56
+jmp -180
+acc +17
+acc -4
+acc +46
+nop -165
+jmp +321
+acc -4
+jmp +1
+acc +9
+acc -12
+jmp -155
+acc +5
+jmp -96
+acc +0
+acc -2
+acc +38
+jmp +67
+acc -4
+nop -283
+acc +28
+jmp +324
+acc -9
+acc +43
+acc -1
+acc +9
+jmp -290
+acc +3
+acc +22
+nop +84
+acc -17
+jmp -210
+acc +7
+jmp -260
+nop -232
+nop +87
+acc +43
+acc +36
+jmp +96
+jmp +238
+acc +13
+acc -14
+acc +32
+acc +11
+jmp -146
+acc +13
+acc +37
+acc -10
+jmp +187
+acc +49
+acc +15
+jmp -234
+jmp -328
+jmp -136
+jmp +143
+jmp +1
+acc +27
+acc +22
+jmp +1
+jmp -5
+acc +30
+nop -7
+acc -6
+jmp -71
+acc -17
+acc +15
+jmp -52
+jmp -126
+acc -4
+jmp +151
+jmp +52
+nop -86
+acc +25
+jmp +187
+nop -22
+jmp -219
+acc +33
+nop -120
+acc +0
+jmp +215
+acc +46
+acc +38
+jmp +1
+jmp -262
+jmp +157
+acc -15
+acc +48
+acc +39
+acc +10
+jmp -137
+acc +47
+acc +50
+jmp -324
+nop +214
+acc +39
+jmp -178
+acc +49
+acc -10
+jmp -268
+jmp +50
+acc -14
+nop -100
+jmp +20
+acc +45
+acc -12
+acc -4
+jmp -208
+acc -19
+jmp -340
+acc +36
+nop -358
+acc +5
+jmp -348
+acc +47
+nop -18
+acc -12
+jmp -131
+acc +19
+acc +10
+acc +19
+acc +31
+jmp -164
+nop +162
+nop -260
+jmp +146
+acc +32
+acc -1
+nop -14
+jmp -192
+acc +3
+acc +31
+nop -185
+jmp -208
+jmp -69
+acc +43
+acc +43
+jmp -68
+acc -16
+acc +5
+acc -9
+jmp +126
+acc +33
+acc +2
+acc +34
+acc -9
+jmp -16
+acc +34
+acc -19
+jmp -266
+nop +135
+nop -389
+acc +33
+jmp -195
+acc +48
+jmp +1
+acc -12
+jmp +143
+nop -317
+acc -14
+nop -127
+acc +32
+jmp -372
+acc +24
+nop -41
+nop -42
+jmp -344
+acc +23
+nop +117
+nop +92
+acc +42
+jmp +143
+acc +48
+acc -6
+nop -272
+acc -13
+jmp -379
+acc -2
+acc +44
+acc +9
+jmp -369
+acc +6
+acc +25
+acc +34
+jmp -301
+nop -227
+acc +43
+jmp -141
+acc +12
+acc +41
+acc +17
+acc -11
+jmp +29
+jmp -121
+acc +6
+acc +7
+acc +7
+jmp +131
+nop +144
+nop -142
+acc -13
+acc -18
+jmp +149
+acc +14
+acc +49
+acc +25
+acc -17
+jmp -9
+acc +26
+acc -4
+jmp -230
+acc -18
+acc +36
+acc +27
+nop -142
+jmp +21
+acc +34
+nop +54
+jmp -476
+acc +10
+jmp -174
+nop -354
+acc +1
+jmp -324
+acc +40
+jmp +94
+acc -12
+jmp -136
+nop -454
+acc -14
+jmp +116
+acc +12
+acc -1
+nop -453
+jmp -241
+jmp -479
+acc -19
+jmp -87
+acc +27
+acc +48
+acc +0
+jmp -476
+acc +16
+acc +46
+jmp -534
+acc +0
+jmp -344
+acc +0
+acc +28
+jmp +10
+jmp -248
+nop -186
+jmp +1
+acc +26
+jmp -153
+acc +14
+acc -8
+nop -416
+jmp -91
+jmp -409
+jmp -326
+acc +2
+acc +8
+acc -18
+acc +33
+jmp -468
+jmp -175
+acc -7
+acc +45
+jmp -18
+jmp -375
+acc -8
+jmp +28
+acc -16
+nop -38
+acc +37
+acc +48
+jmp -343
+acc +10
+acc +26
+acc -9
+acc -16
+jmp -348
+acc +37
+jmp -453
+acc -2
+acc +27
+acc +17
+acc +28
+jmp -406
+acc +25
+acc +24
+acc +44
+acc +44
+jmp -532
+acc +10
+jmp -531
+acc +39
+acc +40
+jmp -284
+acc +19
+acc +3
+nop -533
+acc -3
+jmp -162
+nop -438
+acc -5
+jmp -114
+acc +45
+acc +1
+acc +28
+acc +9
+jmp -550
+jmp -222
+jmp -106
+acc -7
+nop -263
+nop -375
+jmp -381
+acc -4
+nop -223
+jmp -171
+jmp -465
+acc -2
+nop -562
+jmp -190
+acc +40
+jmp -4
+acc +30
+acc +21
+jmp -435
+acc +1
+acc +10
+jmp +1
+jmp -157
+acc -7
+acc +18
+acc -3
+acc +24
+jmp -113
+acc +21
+jmp -339
+acc +34
+jmp -563
+acc +27
+jmp -589
+jmp -61
+acc +35
+acc +50
+acc +8
+jmp -553
+acc +48
+acc -15
+acc +29
+acc +24
+jmp +1 \ No newline at end of file
diff --git a/2020/08/puzzles.c b/2020/08/puzzles.c
new file mode 100644
index 0000000..cfa9389
--- /dev/null
+++ b/2020/08/puzzles.c
@@ -0,0 +1,112 @@
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LINECOUNT 653
+
+struct Inst {
+ char opp;
+ int val;
+};
+
+/* Check if the given instruction has been executed already */
+static bool
+repeati(int const * const lines, int const rip)
+{
+ for (int i = 0; i < LINECOUNT; i++) {
+ if (lines[i] == rip)
+ return true;
+ }
+ return false;
+}
+
+static int
+run_circuit(struct Inst *circuit)
+{
+ int i, acc, rip;
+ int lines[LINECOUNT] = {0};
+
+ i = acc = rip = 0;
+
+ /* Execute the circuit */
+ do {
+ lines[i++] = rip;
+ switch (circuit[rip].opp) {
+ case 'j':
+ rip += circuit[rip].val;
+ if (rip >= LINECOUNT)
+ return acc;
+ break;
+ case 'a':
+ acc += circuit[rip].val;
+ /* FALLTHROUGH */
+ case 'n':
+ rip++;
+ break;
+ }
+ } while (!repeati(lines, rip));
+
+#ifdef PART2
+ return -1;
+#else
+ return acc;
+#endif
+}
+
+int
+main(void)
+{
+ int i = 0;
+ char cl[10];
+ FILE *fp;
+ struct Inst circuit[LINECOUNT];
+
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+
+ /* Load the entire circuit */
+ while (fgets(cl, sizeof(cl), fp)) {
+ char *val;
+ /* struct Inst operation; */
+
+ val = strtok(cl, " ");
+ circuit[i++] = (struct Inst) {
+ .opp = cl[0],
+ .val = atoi(strtok(NULL, " "))
+ };
+ }
+ fclose(fp);
+
+ int result = -1;
+#ifdef PART2
+ int count, prev_count;
+ count = prev_count = 1;
+
+ /* Run circuit until it completes successfully */
+ while (result == -1 && count < LINECOUNT) {
+ for (i = 0; i < LINECOUNT; i++) {
+ if (circuit[i].opp == 'j' || circuit[i].opp == 'n')
+ count--;
+
+ /* Swap jmp and nop */
+ if (!count) {
+ circuit[i].opp = (circuit[i].opp == 'j') ? 'n' : 'j';
+ break;
+ }
+ }
+
+ count = ++prev_count;
+ result = run_circuit(circuit);
+
+ /* Return to original array */
+ circuit[i].opp = (circuit[i].opp == 'j') ? 'n' : 'j';
+ }
+#else
+ result = run_circuit(circuit);
+#endif
+
+ printf("%d\n", result);
+ return EXIT_SUCCESS;
+}