#include <bits/stdc++.h>
using namespace std;
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include "communication.h"
void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...);
void encode(int N, int X);
pair<int, int> decode(int N);
namespace {
enum { ENCODE, DECODE } current_phase;
int N, X;
vector<int> signals;
size_t cursor = 0;
bool flipped = false;
}
int send(int s) {
if (current_phase == DECODE || (s != 0 && s != 1))
result("Invalid send.");
printf("send(%d) -> ", s); fflush(stdout);
int answer;
if (scanf("%d", &answer) != 1 || (answer != 0 && answer != 1))
result("Invalid reply to send.");
bool flipped_now = (s != answer);
if (flipped && flipped_now)
result("Invalid reply to send");
flipped = flipped_now;
signals.push_back(answer);
if (signals.size() > (size_t)250)
result("Looks (and smells) fishy.");
return signals.back();
}
int receive() {
if (current_phase == ENCODE) result("Invalid receive.");
if (cursor >= signals.size()) result("Assistant waiting for Godot.");
int r = signals[cursor++];
printf("receive() -> %d\n", r);
return r;
}
int main() {
if (scanf("%d %d", &N, &X) != 2 || X < 1 || X > N)
result("Invalid input.");
current_phase = ENCODE;
encode(N, X);
current_phase = DECODE;
auto r = decode(N);
if (r.first < 1 || r.first > N || r.second < 1 || r.second > N)
result("Invalid answer.");
if (r.first == X || r.second == X)
result("Correct: %d signals sent.", (int)signals.size());
else
result("Wrong answer.");
}
void encode(int N, int X) {
if (X == 1) {
send(0);
send(1);
} else if (X == 2) {
send(1);
send(0);
} else if (X == 3) {
send(1);
send(1);
}
}
pair<int, int> decode(int N) {
int first_bit = receive();
int second_bit = receive();
if (first_bit == 0 && second_bit == 1) {
return {1, 1};
} else if (first_bit == 1 && second_bit == 0) {
return {2, 2};
} else if (first_bit == 1 && second_bit == 1) {
return {3, 3};
}
return {1, 2};
}
Compilation message
/usr/bin/ld: /tmp/ccBuCgF8.o: in function `send(int)':
interface.cpp:(.text+0x70): multiple definition of `send(int)'; /tmp/ccnlY5e8.o:communication.cpp:(.text+0xf0): first defined here
/usr/bin/ld: /tmp/ccBuCgF8.o: in function `receive()':
interface.cpp:(.text+0x100): multiple definition of `receive()'; /tmp/ccnlY5e8.o:communication.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccBuCgF8.o: in function `main':
interface.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccnlY5e8.o:communication.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccnlY5e8.o: in function `receive()':
communication.cpp:(.text+0x66): undefined reference to `result(char const*, ...)'
/usr/bin/ld: communication.cpp:(.text+0x74): undefined reference to `result(char const*, ...)'
/usr/bin/ld: /tmp/ccnlY5e8.o: in function `send(int)':
communication.cpp:(.text+0x1fa): undefined reference to `result(char const*, ...)'
/usr/bin/ld: communication.cpp:(.text+0x208): undefined reference to `result(char const*, ...)'
/usr/bin/ld: communication.cpp:(.text+0x216): undefined reference to `result(char const*, ...)'
/usr/bin/ld: /tmp/ccnlY5e8.o:communication.cpp:(.text+0x224): more undefined references to `result(char const*, ...)' follow
collect2: error: ld returned 1 exit status