advent2025

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

commit 6a2844416f21d4c56259a9a6550d05bd883bc4b8
parent 6394f767b86d0a0b421e639f88544d672da5614e
Author: bsandro <email@bsandro.tech>
Date:   Sat, 13 Dec 2025 23:14:18 +0200

day10 p1 basic optimization (using xor)

Diffstat:
Mday10.c | 87++++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 37 insertions(+), 50 deletions(-)

diff --git a/day10.c b/day10.c @@ -8,7 +8,7 @@ #define BUF_SIZE 256 -int btoi(int bufl, char buf[bufl]) { +static int btoi(int bufl, char buf[bufl]) { int num = 0; for (int i=0;i<bufl;++i) num = num*10+(buf[i]-'0'); return num; @@ -21,32 +21,48 @@ typedef struct { typedef struct { int lights_l; - bool lights[16]; //@todo bit field? desired value - bool lights1[16];//overall value that we can change + bool lights[16]; + uint16_t indicators; int buttons_l; Button buttons[16]; + int buttons1_l; + uint16_t buttons1[16]; int joltages_l; int joltages[16]; // same number as lights } Machine; -void m_lights_add(Machine *m, int bufl, char buf[bufl]) { +static void m_lights_add(Machine *m, int bufl, char buf[bufl]) { for (int i=0;i<bufl;++i) { m->lights[m->lights_l++] = buf[i]=='#'; } } -void m_button_add(Machine *m, int bufl, char buf[bufl]) { +static void m_indicators_add(Machine *m, int bufl, char buf[bufl]) { + for (int i=0;i<bufl;++i) { + if (buf[i]=='#') m->indicators |= 1<<i; + } +} + +static void m_button_add(Machine *m, int bufl, char buf[bufl]) { Button *btn = &m->buttons[m->buttons_l++]; for (int i=0;i<bufl;++i) { btn->toggles[btn->toggles_l++] = buf[i]-'0'; } } -void m_joltage_add(Machine *m, int jolt) { +static void m_button1_add(Machine *m, int bufl, char buf[bufl]) { + int b = m->buttons1_l++; + for (int i=0;i<bufl;++i) { + m->buttons1[b] |= 1<<(buf[i]-'0'); + } +} + +static void m_joltage_add(Machine *m, int jolt) { m->joltages[m->joltages_l++] = jolt; } -bool btn_lights(Button *btn, int light) { +// used in p2 +static bool btn_lights(Button *btn, int light) { for (int i=0;i<btn->toggles_l;++i) { if (btn->toggles[i]==light) { return true; @@ -55,54 +71,22 @@ bool btn_lights(Button *btn, int light) { return false; } -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; -} - -//@todo use XOR -void m_light_toggle(Machine *m, int light) { - m->lights1[light] = !m->lights1[light]; -} - -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]); - } -} - -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) { +static bool try_combination(Machine *m, int comb_l, uint16_t comb[comb_l], int p) { if (p==comb_l) { - Machine m1 = *m; - m_btn_press_many(&m1, comb_l, comb); - if (m_lights_on(&m1)) return true; - return false; + uint16_t c = 0; + for (int i=0;i<comb_l;++i) c ^= comb[i]; + return m->indicators==c; } - for (int i=0;i<m->buttons_l;++i) { - 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; - } + for (int i=0;i<m->buttons1_l;++i) { + uint16_t btn = m->buttons1[i]; + if (p>0&&comb[p-1]==btn) continue; // do not press same button twice in a row + comb[p] = btn; + if (try_combination(m, comb_l, comb, p+1)) return true; } return false; } -bool m_try_start(Machine m, int max_presses) { - int comb[max_presses]; - return try_combination(&m, max_presses, comb, 0); -} - -int64_t solve_glpk(Machine *m) { +static int64_t solve_glpk(Machine *m) { int btnl = m->buttons_l; int cntl = m->joltages_l; // matrix of buttons/counters @@ -158,10 +142,12 @@ int main(void) { for (int c=getchar();c!=EOF;c=getchar()) { if (c==']') { m_lights_add(&machines[machines_l], bufl, buf); + m_indicators_add(&machines[machines_l], bufl, buf); bufl = 0; } else if (c==')') { m_button_add(&machines[machines_l], bufl, buf); + m_button1_add(&machines[machines_l], bufl, buf); bufl = 0; } else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[bufl++] = c; @@ -180,7 +166,8 @@ int main(void) { int64_t part2 = 0; for (int i=0;i<machines_l;++i) { for (int j=1;j<=20;++j) { - if (m_try_start(machines[i], j)) { + uint16_t comb[j]; + if (try_combination(&machines[i], j, comb, 0)) { part1 += j; break; }