%d\n", str_pad(decbin($i), NUM_CONNECTIONS, '0', STR_PAD_LEFT), $truth_table[$i]); } } function print_connections(array $connections): void { print("Connections:\n"); for ($i = 0; $i < NUM_NODES; $i++) { printf("[%3d] => [%s]\n", $i, implode(', ', $connections[$i])); } } function format_nodes(array $nodes): string { return implode(' ', array_map(fn($i): string => $i == 0 ? CHAR_ON_ZERO : CHAR_ON_ONE, $nodes)); } function propagate(array $nodes, array &$connections, array &$truth_table): array { $next_nodes = []; for ($i = 0; $i < NUM_NODES; $i++) { $tt_index = 0; for ($j = 0; $j < NUM_CONNECTIONS; $j++) { $tt_index += $nodes[$connections[$i][$j]] << (NUM_CONNECTIONS - 1 - $j); } $next_nodes[$i] = $truth_table[$tt_index]; } return $next_nodes; } print_truth_table($truth_table); print("\n"); print_connections($connections); print("\n"); print_generation(0, $nodes); for ($i = 0; $i < NUM_GENERATIONS; $i++) { $nodes = propagate($nodes, $connections, $truth_table); print_generation($i, $nodes); }