commit 04e621e3fde91ff77b402949353d6e62f8b2d9c7
parent 997db06eef33167856f71b0385f103c10e1ad15d
Author: bsandro <email@bsandro.tech>
Date: Fri, 12 Dec 2025 10:54:23 +0200
day10 parse joltages
Diffstat:
| M | day10.c | | | 45 | +++++++++++++++++++++++++++++---------------- |
1 file changed, 29 insertions(+), 16 deletions(-)
diff --git a/day10.c b/day10.c
@@ -6,9 +6,9 @@
#define BUF_SIZE 256
-int btoi(int bufl, int buf[bufl]) {
+int btoi(int bufl, char buf[bufl]) {
int num = 0;
- for (int i=0;i<bufl;++i) num = num*10+buf[i];
+ for (int i=0;i<bufl;++i) num = num*10+(buf[i]-'0');
return num;
}
@@ -23,23 +23,27 @@ typedef struct {
bool lights1[16];//overall value that we can change
int buttons_l;
Button buttons[16];
- int joltajes_l;
+ int joltages_l;
int joltages[16]; // same number as lights
} Machine;
-void m_lights_add(Machine *m, int buf_l, char buf[buf_l]) {
- for (int i=0;i<buf_l;++i) {
+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 buf_l, char buf[buf_l]) {
+void m_button_add(Machine *m, int bufl, char buf[bufl]) {
Button *btn = &m->buttons[m->buttons_l++];
- for (int i=0;i<buf_l;++i) {
+ for (int i=0;i<bufl;++i) {
btn->toggles[btn->toggles_l++] = buf[i]-'0';
}
}
+void m_joltage_add(Machine *m, int jolt) {
+ m->joltages[m->joltages_l++] = jolt;
+}
+
bool m_lights_on(Machine *m) {
for (int i=0;i<m->lights_l;++i) {
if (m->lights[i]!=m->lights1[i]) return false;
@@ -54,8 +58,10 @@ void m_light_toggle(Machine *m, int light) {
void m_print(Machine *m) {
printf("(%zu) Lights: [", sizeof(Machine));
- for (int i=0;i<m->lights_l;++i) printf("%c", m->lights[i]?'#':'.');
- printf("]\n");
+ for (int i=0;i<m->lights_l;++i) putchar(m->lights[i]?'#':'.');
+ printf("] Joltages:");
+ for (int i=0;i<m->joltages_l;++i) printf(" %d", m->joltages[i]);
+ putchar('\n');
}
void m_btn_press(Machine *m, int b) {
@@ -107,20 +113,27 @@ int main(void) {
Machine machines[180] = {0};
int machines_l = 0;
char buf[BUF_SIZE] = {0};
- int buf_l = 0;
+ int bufl = 0;
+ bool parse_joltages = false;
for (int c=getchar();c!=EOF;c=getchar()) {
if (c==']') {
- m_lights_add(&machines[machines_l], buf_l, buf);
- buf_l = 0;
+ m_lights_add(&machines[machines_l], bufl, buf);
+ bufl = 0;
}
else if (c==')') {
- m_button_add(&machines[machines_l], buf_l, buf);
- buf_l = 0;
+ m_button_add(&machines[machines_l], bufl, buf);
+ bufl = 0;
}
- else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[buf_l++] = c;
+ else if ((c>='0'&&c<='9')||c=='.'||c=='#') buf[bufl++] = c;
else if (c=='\n') {
machines_l++;
- buf_l = 0;
+ bufl = 0;
+ parse_joltages = false;
+ } 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;
}
}
int64_t part1 = 0;