advent2025

Advent of Code 2025 Solutions
git clone git://bsandro.tech/advent2025
Log | Files | Refs | LICENSE

commit 3637ea9d48f67da86ae03baa0f31aa9ae6e54781
parent 514810dad8832a71be788e671fa8f6311374e5a0
Author: bsandro <email@bsandro.tech>
Date:   Wed, 10 Dec 2025 23:28:47 +0200

day10 p1 somehow works, I misread the task. Plenty of room to optimize too - gotta redo it with bit masks

Diffstat:
Mday10.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 82 insertions(+), 17 deletions(-)

diff --git a/day10.c b/day10.c @@ -1,5 +1,8 @@ #include <stdio.h> #include <stdbool.h> +#include <limits.h> +#include <inttypes.h> +#include "cputime.h" #define BUF_SIZE 256 @@ -10,38 +13,87 @@ typedef struct { typedef struct { int lights_l; - bool lights[16]; + bool lights[16]; //@todo bit field? desired value + bool lights1[16];//overall value that we can change int buttons_l; Button buttons[16]; + int presses; } Machine; -void m_add_lights(Machine *m, int buf_l, char buf[buf_l]) { +void m_lights_add(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]) { +void m_button_add(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]?'#':'.'); +bool m_lights_on(Machine *m) { + for (int i=0;i<m->lights_l;++i) { + if (m->lights[i]!=m->lights1[i]) return false; + } + return true; +} + +void m_light_toggle(Machine *m, int light) { + m->lights1[light] = !m->lights1[light]; +} + +void m_print(Machine *m) { + printf("(%zu) Lights: [", sizeof(Machine)); + for (int i=0;i<m->lights_l;++i) printf("%c", m->lights1[i]?'#':'.'); printf("]\n"); - if (!btns) return; - printf("Buttons: "); +} + +void m_btn_press(Machine *m, int b) { + Button *btn = &m->buttons[b]; + for (int i=0;i<btn->toggles_l;++i) { + m_light_toggle(m, btn->toggles[i]); + } + m->presses++; +} + +void m_btn_press_many(Machine *m, int comb_l, int comb[comb_l]) { + for (int i=0;i<comb_l;++i) { + m_btn_press(m, comb[i]); + } +} + +bool try_combination(Machine *m, int comb_l, int comb[comb_l], int p) { + if (p==comb_l) { + /*printf("combination: "); + for (int i=0;i<comb_l;++i) printf("%d", comb[i]); + printf("\n");*/ + Machine m1 = *m; + //m_print(&m1); + m_btn_press_many(&m1, comb_l, comb); + //puts("after:"); + //m_print(&m1); + if (m_lights_on(&m1)) { + puts("ok"); + return true; + } + //return m_lights_on(&m1); + return false; + } 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(") "); + if (p>0&&comb[p-1]==i) continue; // do not press same button twice in a row + comb[p] = i; + if (try_combination(m, comb_l, comb, p+1)) { + return true; + } } - printf("\n"); + return false; +} + +bool m_try_start(Machine m, int max_presses) { + int comb[max_presses]; + return try_combination(&m, max_presses, comb, 0); } int main(void) { @@ -51,11 +103,11 @@ int main(void) { int buf_l = 0; for (int c=getchar();c!=EOF;c=getchar()) { if (c==']') { - m_add_lights(&machines[machines_l], buf_l, buf); + m_lights_add(&machines[machines_l], buf_l, buf); buf_l = 0; } else if (c==')') { - m_add_button(&machines[machines_l], buf_l, buf); + m_button_add(&machines[machines_l], buf_l, buf); buf_l = 0; } else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[buf_l++] = c; @@ -64,8 +116,21 @@ int main(void) { buf_l = 0; } } + int64_t part1 = 0; + int64_t part2 = 0; for (int i=0;i<machines_l;++i) { - m_print(&machines[i], false); + printf("---- Machine %p ----\n", &machines[i]); + printf("%d buttons\n", machines[i].buttons_l); + m_print(&machines[i]); + for (int j=1;j<=20;++j) { + printf("try start %i with %d button presses\n", i, j); + if (m_try_start(machines[i], j)) { + printf("%d presses start ok\n", j); + part1 += j; + break; + } + } } + printf("p1: %"PRIi64"\np2: %"PRIi64"\n", part1, part2); return 0; }