aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-02 14:34:48 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-02 14:34:48 +0100
commitfeca43e796fbe8bde441f017f4c94642392bc40f (patch)
tree3642a3ceec6375a8275c29de8db22e9f5c88fa44
parent3ac99f409ae6545e2f4fece4d9397d28d35b279b (diff)
Add day 23 solutions
-rw-r--r--2015/23/Makefile9
-rw-r--r--2015/23/input48
-rw-r--r--2015/23/puzzles.l103
3 files changed, 160 insertions, 0 deletions
diff --git a/2015/23/Makefile b/2015/23/Makefile
new file mode 100644
index 0000000..8779ec0
--- /dev/null
+++ b/2015/23/Makefile
@@ -0,0 +1,9 @@
+all:
+ flex -f -DYY_NO_INPUT --nounput puzzles.l
+ ${CC} ${CFLAGS} -lfl -DPROGLEN=`wc -l <input` -o puzzle-1 lex.yy.c
+ ${CC} ${CFLAGS} -lfl -DPROGLEN=`wc -l <input` -DPART2 -o puzzle-2 lex.yy.c
+ rm lex.yy.c
+
+.PHONY: clean
+clean:
+ rm -f puzzle-[12]
diff --git a/2015/23/input b/2015/23/input
new file mode 100644
index 0000000..cad5dc2
--- /dev/null
+++ b/2015/23/input
@@ -0,0 +1,48 @@
+jio a, +22
+inc a
+tpl a
+tpl a
+tpl a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+jmp +19
+tpl a
+tpl a
+tpl a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+tpl a
+tpl a
+jio a, +8
+inc b
+jie a, +4
+tpl a
+inc a
+jmp +2
+hlf a
+jmp -7
diff --git a/2015/23/puzzles.l b/2015/23/puzzles.l
new file mode 100644
index 0000000..7d654e1
--- /dev/null
+++ b/2015/23/puzzles.l
@@ -0,0 +1,103 @@
+%{
+#define _POSIX_C_SOURCE
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Shut up the compiler */
+int fileno(FILE *);
+
+struct Instr {
+ enum {
+ HLF,
+ INC,
+ JIE,
+ JIO,
+ JMP,
+ TPL
+ } type;
+ enum {
+ A,
+ B
+ } reg;
+ int imm;
+};
+
+int i = 0;
+struct Instr *program;
+
+#ifdef PART2
+unsigned long long registers[2] = {1, 0};
+#else
+unsigned long long registers[2];
+#endif
+%}
+
+%x ARGS
+
+%%
+
+hlf { program[i].type = HLF; BEGIN(ARGS); }
+inc { program[i].type = INC; BEGIN(ARGS); }
+jie { program[i].type = JIE; BEGIN(ARGS); }
+jio { program[i].type = JIO; BEGIN(ARGS); }
+jmp { program[i].type = JMP; BEGIN(ARGS); }
+tpl { program[i].type = TPL; BEGIN(ARGS); }
+
+<ARGS>[ab] { program[i].reg = *yytext - 'a'; }
+<ARGS>[+\-][0-9]+ { program[i].imm = atoi(yytext); }
+<ARGS>[ ,]+ { ; }
+<ARGS>\n { i++; BEGIN(INITIAL); }
+
+%%
+
+static unsigned long long
+execute(void)
+{
+ for (i = 0; i < PROGLEN; i++) {
+ switch (program[i].type) {
+ case HLF:
+ registers[program[i].reg] /= 2;
+ break;
+ case INC:
+ registers[program[i].reg]++;
+ break;
+ case JIE:
+ if (!(registers[program[i].reg] & 1))
+ i += program[i].imm - 1;
+ break;
+ case JIO:
+ if (registers[program[i].reg] == 1)
+ i += program[i].imm - 1;
+ break;
+ case JMP:
+ i += program[i].imm - 1;
+ break;
+ case TPL:
+ registers[program[i].reg] *= 3;
+ break;
+ }
+ }
+
+ return registers[B];
+}
+
+int
+main(void)
+{
+ FILE *fp;
+
+ if (!(program = calloc(PROGLEN, sizeof(struct Instr))))
+ err(EXIT_FAILURE, "calloc");
+
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+
+ yyrestart(fp);
+ yylex();
+ fclose(fp);
+
+ printf("%llu\n", execute());
+ return EXIT_SUCCESS;
+}