commit cedb45a57fb1989768dcd09cf7f142628860e911
parent 25c5cdae5e6851c0473fc24f59ba668686d52e53
Author: bsandro <email@bsandro.tech>
Date: Wed, 10 Dec 2025 08:53:26 +0200
day10 parsing
Diffstat:
| A | day10.c | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 71 insertions(+), 0 deletions(-)
diff --git a/day10.c b/day10.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+#define BUF_SIZE 256
+
+typedef struct {
+ int toggles_l;
+ int toggles[16];
+} Button;
+
+typedef struct {
+ int lights_l;
+ bool lights[16];
+ int buttons_l;
+ Button buttons[16];
+} Machine;
+
+void m_add_lights(Machine *m, int buf_l, char buf[buf_l]) {
+ for (int i=0;i<buf_l;++i) {
+ m->lights[m->lights_l++] = buf[i]=='#';
+ }
+}
+
+void m_add_button(Machine *m, int buf_l, char buf[buf_l]) {
+ Button *btn = &m->buttons[m->buttons_l++];
+ for (int i=0;i<buf_l;++i) {
+ btn->toggles[btn->toggles_l++] = buf[i]-'0';
+ }
+}
+
+void m_print(Machine *m, bool btns) {
+ puts("---- Machine ----");
+ printf("Lights: [");
+ for (int i=0;i<m->lights_l;++i) printf("%c", m->lights[i]?'#':'.');
+ printf("]\n");
+ if (!btns) return;
+ printf("Buttons: ");
+ for (int i=0;i<m->buttons_l;++i) {
+ printf("(");
+ Button *btn = &m->buttons[i];
+ for (int j=0;j<btn->toggles_l;++j) printf("%d", btn->toggles[j]);
+ printf(") ");
+ }
+ printf("\n");
+}
+
+int main(void) {
+ Machine machines[180] = {0};
+ int machines_l = 0;
+ char buf[BUF_SIZE] = {0};
+ int buf_l = 0;
+ for (int c=getchar();c!=EOF;c=getchar()) {
+ if (c==']') {
+ m_add_lights(&machines[machines_l], buf_l, buf);
+ buf_l = 0;
+ }
+ else if (c==')') {
+ m_add_button(&machines[machines_l], buf_l, buf);
+ buf_l = 0;
+ }
+ else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[buf_l++] = c;
+ else if (c=='\n') {
+ machines_l++;
+ buf_l = 0;
+ }
+ }
+ for (int i=0;i<machines_l;++i) {
+ m_print(&machines[i], false);
+ }
+ return 0;
+}