amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:43:15 +08:00

223 lines
7.5 KiB
C++

#include "overlay.hpp"
#include <cstdint>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace {
using namespace overlay_relocation;
class Words {
public:
explicit Words(const std::string& line) : stream_(line) {}
std::string next() {
std::string word;
if (!(stream_ >> word)) throw std::runtime_error("missing token");
return word;
}
std::uint64_t number(std::uint64_t limit) {
const std::string word = next();
std::uint64_t result = 0;
for (unsigned char byte : word) {
if (byte < '0' || byte > '9') throw std::runtime_error("decimal token");
const unsigned digit = byte - '0';
if (digit > limit || result > (limit - digit) / 10) {
throw std::runtime_error("number wire limit");
}
result = result * 10 + digit;
}
return result;
}
void end() {
std::string word;
if (stream_ >> word) throw std::runtime_error("extra token");
}
private:
std::istringstream stream_;
};
unsigned nibble(unsigned char byte) {
if (byte >= '0' && byte <= '9') return byte - '0';
if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10;
if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10;
throw std::runtime_error("hex token");
}
std::string read_bytes(Words& words, std::size_t limit) {
const std::string word = words.next();
if (word == "-") return {};
if (word.size() % 2 != 0 || word.size() / 2 > limit) {
throw std::runtime_error("byte wire limit");
}
std::string result;
result.reserve(word.size() / 2);
for (std::size_t i = 0; i < word.size(); i += 2) {
result.push_back(static_cast<char>((nibble(word[i]) << 4) | nibble(word[i + 1])));
}
return result;
}
template<class Range>
void hex(std::ostream& out, const Range& bytes) {
static const char digits[] = "0123456789abcdef";
out << '"';
for (unsigned char byte : bytes) out << digits[byte >> 4] << digits[byte & 15];
out << '"';
}
void tree(std::ostream& out, const Tree& value) {
out << '[';
for (std::size_t i = 0; i < value.nodes.size(); ++i) {
if (i != 0) out << ',';
const Node& node = value.nodes[i];
out << '[';
hex(out, node.path);
out << ", [";
for (std::size_t j = 0; j < node.properties.size(); ++j) {
if (j != 0) out << ',';
const Property& property = node.properties[j];
out << '[';
hex(out, property.name);
out << ',';
hex(out, property.value);
out << ']';
}
out << "]]";
}
out << ']';
}
struct Bank {
Tree inputs[2];
Tree last;
bool has_last = false;
std::size_t names = 0;
std::size_t data = 0;
std::size_t properties = 0;
};
std::string input_value(const Bank& bank) {
std::ostringstream out;
tree(out, bank.inputs[0]);
out << ',';
tree(out, bank.inputs[1]);
return out.str();
}
Tree dirty() {
return Tree{{Node{"/old", {Property{"OLD", Bytes{0, 255}}}}}};
}
struct Observer {
Bank banks[2];
Tree held[4];
bool has_held[4] = {false, false, false, false};
unsigned active = 0;
unsigned calls = 0;
void command(const std::string& line) {
std::istringstream probe(line);
std::string first;
if (!(probe >> first) || first[0] == '#') return;
Words words(line);
const std::string op = words.next();
Bank& bank = banks[active];
if (op == "A") {
active = static_cast<unsigned>(words.number(1));
} else if (op == "N") {
bank = Bank{};
} else if (op == "F") {
Tree& value = bank.inputs[words.number(1)];
std::string path = read_bytes(words, 131073);
if (value.nodes.size() >= 513 || path.size() > 262144 - bank.names) {
throw std::runtime_error("node wire limit");
}
bank.names += path.size();
value.nodes.push_back({std::move(path), {}});
} else if (op == "P") {
Tree& value = bank.inputs[words.number(1)];
const auto index = words.number(512);
if (index >= value.nodes.size()) throw std::runtime_error("missing node");
std::string name = read_bytes(words, 131073);
const std::string bytes = read_bytes(words, 65537);
if (bank.properties >= 4097 || name.size() > 262144 - bank.names ||
bytes.size() > 2097152 - bank.data) {
throw std::runtime_error("property wire limit");
}
bank.names += name.size();
bank.data += bytes.size();
++bank.properties;
value.nodes[static_cast<std::size_t>(index)].properties.push_back(
{std::move(name), Bytes(bytes.begin(), bytes.end())});
} else if (op == "R" || op == "Y") {
const bool alias = op == "Y";
const unsigned which = alias ? static_cast<unsigned>(words.number(1)) : 0;
words.end();
if (++calls > 256) throw std::runtime_error("call wire limit");
const std::string before = input_value(bank);
Tree output = dirty();
const int status = apply_overlay(bank.inputs[which], bank.inputs[alias ? which : 1], output);
const bool unchanged = before == input_value(bank);
bank.last = output;
bank.has_last = true;
std::cout << "[\"" << op << "\"," << status << ',';
tree(std::cout, output);
std::cout << ',' << (unchanged ? "true" : "false") << "]\n";
return;
} else if (op == "H" || op == "J" || op == "M") {
const unsigned slot = static_cast<unsigned>(words.number(3));
words.end();
if (op == "H") {
if (!bank.has_last) throw std::runtime_error("no last output");
held[slot] = bank.last;
has_held[slot] = true;
} else {
if (!has_held[slot]) throw std::runtime_error("no held output");
std::cout << "[\"" << op << "\",";
tree(std::cout, held[slot]);
if (op == "M") {
Tree copy = held[slot];
if (!copy.nodes.empty()) {
copy.nodes[0].path = "/copy";
if (!copy.nodes[0].properties.empty()) {
copy.nodes[0].properties[0].value = Bytes{0, 255};
}
copy.nodes[0].properties.push_back({"COPY", Bytes{1, 2}});
}
copy.nodes.push_back({"/new", {Property{"NEW", Bytes{255, 0}}}});
std::cout << ',';
tree(std::cout, copy);
std::cout << ',';
tree(std::cout, held[slot]);
}
std::cout << "]\n";
}
return;
} else {
throw std::runtime_error("unknown command");
}
words.end();
}
};
} // namespace
int main() {
try {
Observer observer;
std::string line;
while (std::getline(std::cin, line)) {
if (line.size() > 8388608) throw std::runtime_error("line wire limit");
observer.command(line);
}
if (!std::cin.eof()) throw std::runtime_error("input read error");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 2;
}
}