diff --git a/examples/aoc2024/day23/part1.jou b/examples/aoc2024/day23/part1.jou new file mode 100644 index 00000000..f80e7cc9 --- /dev/null +++ b/examples/aoc2024/day23/part1.jou @@ -0,0 +1,99 @@ +import "stdlib/str.jou" +import "stdlib/mem.jou" +import "stdlib/io.jou" + + +class Computer: + name: byte[3] + group_id: int # computers connected together have same group id + connections: void*[100] # TODO: should be Computer*[100], can't be due to compiler bug + + def connect(self, other: Computer*) -> None: + assert other != NULL + + for i = 0; i < sizeof(self->connections)/sizeof(self->connections[0]); i++: + if self->connections[i] == NULL: + self->connections[i] = other + return + + # array is full + assert False + + def is_connected(self, other: Computer*) -> bool: + for i = 0; i < sizeof(self->connections)/sizeof(self->connections[0]); i++: + if self->connections[i] == other: + return True + return False + + +def find_or_create_computer(computers: Computer*, ncomputers: int*, name: byte*) -> Computer*: + for i = 0; i < *ncomputers; i++: + if strcmp(computers[i].name, name) == 0: + return &computers[i] + + c = &computers[*ncomputers] + assert strlen(name) == 2 + strcpy(c->name, name) + c->group_id = *ncomputers # unique initially + memset(c->connections, 0, sizeof(c->connections)) + + ++*ncomputers + return c + + +def merge_groups(computers: Computer*, ncomputers: int, id1: int, id2: int) -> None: + printf("Merge %d,%d\n", id1, id2) + if id1 == id2: + return + + for i = 0; i < ncomputers; i++: + if computers[i].group_id == id1: + computers[i].group_id = id2 + + +def main() -> int: + computers: Computer* = malloc(sizeof(computers[0]) * 5000) + assert computers != NULL + ncomputers = 0 + + f = fopen("sampleinput.txt", "r") + assert f != NULL + name1: byte[3] + name2: byte[3] + + while fscanf(f, "%2s-%2s\n", name1, name2) == 2: + assert ncomputers < 5000 + c1 = find_or_create_computer(computers, &ncomputers, name1) + assert ncomputers < 5000 + c2 = find_or_create_computer(computers, &ncomputers, name2) + c1->connect(c2) + c2->connect(c1) + + fclose(f) + printf("Got %d computers\n", ncomputers) # Output: Got 16 computers + + n = 0 + for i = 0; i < ncomputers; i++: + for j = i+1; j < ncomputers; j++: + if i == j or not computers[i].is_connected(&computers[j]): + continue + + for k = j+1; k < ncomputers; k++: + if ( + k == i + or k == j + or not computers[k].is_connected(&computers[i]) + or not computers[k].is_connected(&computers[j]) + ): + continue + + if ( + computers[i].name[0] == 't' + or computers[j].name[0] == 't' + or computers[k].name[0] == 't' + ): + # Found 3 connected + n++ + + printf("%d\n", n) # Output: 7 + return 0 diff --git a/examples/aoc2024/day23/sampleinput.txt b/examples/aoc2024/day23/sampleinput.txt new file mode 100644 index 00000000..3d497667 --- /dev/null +++ b/examples/aoc2024/day23/sampleinput.txt @@ -0,0 +1,32 @@ +kh-tc +qp-kh +de-cg +ka-co +yn-aq +qp-ub +cg-tb +vc-aq +tb-ka +wh-tc +yn-cg +kh-ub +ta-co +de-co +tc-td +tb-wq +wh-td +ta-ka +td-qp +aq-cg +wq-ub +ub-vc +de-ta +wq-aq +wq-vc +wh-yn +ka-de +kh-ta +co-tc +wh-qp +tb-vc +td-yn