advent2025

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

commit 186d00385ac90e6275a6d1df0b033c42000fe2fa
parent 6a2844416f21d4c56259a9a6550d05bd883bc4b8
Author: bsandro <email@bsandro.tech>
Date:   Sat, 13 Dec 2025 23:34:10 +0200

day10 cleanup

Diffstat:
Mday10.c | 78+++++++++++++++++++++++-------------------------------------------------------
1 file changed, 23 insertions(+), 55 deletions(-)

diff --git a/day10.c b/day10.c @@ -7,6 +7,7 @@ #include "cputime.h" #define BUF_SIZE 256 +#define MAX_TRIES 20 static int btoi(int bufl, char buf[bufl]) { int num = 0; @@ -15,70 +16,37 @@ static int btoi(int bufl, char buf[bufl]) { } typedef struct { - int toggles_l; - int toggles[16]; -} Button; - -typedef struct { - int lights_l; - bool lights[16]; uint16_t indicators; int buttons_l; - Button buttons[16]; - int buttons1_l; - uint16_t buttons1[16]; + uint16_t buttons[16]; int joltages_l; - int joltages[16]; // same number as lights + int joltages[16]; } Machine; -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]=='#'; - } -} - 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; - } + 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'; - } -} - -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_buttons_add(Machine *m, int bufl, char buf[bufl]) { + int b = m->buttons_l++; + for (int i=0;i<bufl;++i) + m->buttons[b] |= 1<<(buf[i]-'0'); } static void m_joltage_add(Machine *m, int jolt) { m->joltages[m->joltages_l++] = jolt; } -// 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; - } - } - return false; -} - static bool try_combination(Machine *m, int comb_l, uint16_t comb[comb_l], int p) { if (p==comb_l) { 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->buttons1_l;++i) { - uint16_t btn = m->buttons1[i]; + for (int i=0;i<m->buttons_l;++i) { + uint16_t btn = m->buttons[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; @@ -89,11 +57,11 @@ static bool try_combination(Machine *m, int comb_l, uint16_t comb[comb_l], int p static int64_t solve_glpk(Machine *m) { int btnl = m->buttons_l; int cntl = m->joltages_l; - // matrix of buttons/counters - int matbc[btnl][cntl]; + int matbc[btnl][cntl]; // matrix of buttons/counters for (int b=0;b<btnl;++b) { for (int c=0;c<cntl;++c) { - matbc[b][c] = btn_lights(&m->buttons[b], c) ? 1 : 0; + uint16_t i = 1<<c; + matbc[b][c] = (m->buttons[b]&i)==0 ? 0 : 1; } } @@ -141,22 +109,22 @@ int main(void) { bool parse_joltages = false; 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); + m_buttons_add(&machines[machines_l], bufl, buf); bufl = 0; } - else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[bufl++] = c; - else if (c=='\n') { + else if ((c>='0'&&c<='9')||c=='.'||c=='#') { + buf[bufl++] = c; + } else if (c=='\n') { machines_l++; bufl = 0; parse_joltages = false; - } else if (c=='{') parse_joltages = true; - else if (parse_joltages&&(c==','||c=='}')) { + } else if (c=='{') { + parse_joltages = true; + } else if (parse_joltages&&(c==','||c=='}')) { int jolt = btoi(bufl, buf); m_joltage_add(&machines[machines_l], jolt); bufl = 0; @@ -165,7 +133,7 @@ int main(void) { int64_t part1 = 0; int64_t part2 = 0; for (int i=0;i<machines_l;++i) { - for (int j=1;j<=20;++j) { + for (int j=1;j<=MAX_TRIES;++j) { uint16_t comb[j]; if (try_combination(&machines[i], j, comb, 0)) { part1 += j;