amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

241 lines
7.8 KiB
C++

#include "group.hpp"
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
namespace {
using group::Word;
std::vector<std::string> words(const std::string &line)
{
std::vector<std::string> result;
std::string word;
for (char c : line) {
if (c == ' ' || c == '\t') {
if (!word.empty()) {
result.push_back(word);
word.clear();
}
} else {
word.push_back(c);
}
}
if (!word.empty()) result.push_back(word);
return result;
}
bool number(const std::string &text, Word &out)
{
if (text.empty()) return false;
for (char c : text) if (c < '0' || c > '9') return false;
Word result = 0;
const Word maximum = std::numeric_limits<Word>::max();
for (char c : text) {
const Word digit = static_cast<Word>(c - '0');
if (result > (maximum - digit) / 10) return false;
result = result * 10 + digit;
}
out = result;
return true;
}
int nibble(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
bool unhex(const std::string &text, std::string &out)
{
out.clear();
if (text == "-") return true;
if (text.size() % 2 != 0) return false;
for (std::size_t i = 0; i < text.size(); i += 2) {
const int high = nibble(text[i]);
const int low = nibble(text[i + 1]);
if (high < 0 || low < 0) return false;
out.push_back(static_cast<char>(high * 16 + low));
}
return true;
}
Word raw_hash(const std::string &text)
{
Word hash = 14695981039346656037ULL;
for (unsigned char c : text) {
hash ^= c;
hash *= 1099511628211ULL;
}
return hash;
}
Word raw_items(const std::vector<group::Item> &items)
{
std::string bytes;
for (const auto &item : items) {
for (Word value : {item.id, item.value}) {
for (unsigned shift = 0; shift < 64; shift += 8) {
bytes.push_back(static_cast<char>((value >> shift) & 255));
}
}
}
return raw_hash(bytes);
}
void unchanged(Word before, Word after)
{
std::cout << '[' << before << ',' << after << "]\n";
}
void receipt(int rc, const group::Receipt &r)
{
std::cout << '[' << rc << ',' << r.ticket << ',' << r.count
<< ',' << r.total << ',' << r.digest << "]\n";
}
void info(const group::JournalInfo &s)
{
std::cout << s.batches << ',' << s.items << ',' << s.total
<< ',' << s.attempts << ',' << s.failures;
}
bool execute(group::Engine &e, group::Journal &j, const std::vector<std::string> &w)
{
const std::string &op = w[0];
std::vector<Word> a;
std::string text;
const bool text_op = op == "N" || op == "T";
for (std::size_t i = 1; i < w.size(); ++i) {
if (text_op && i == 1) {
if (!unhex(w[i], text)) return false;
} else {
Word value = 0;
if (!number(w[i], value)) return false;
a.push_back(value);
}
}
int rc = 0;
if (op == "C" && a.size() == 2) {
rc = e.configure({a[0], a[1]});
} else if (op == "A" && a.size() == 2) {
rc = e.enqueue({a[0], a[1]});
} else if (op == "X" && a.empty()) {
rc = e.close();
} else if (op == "F" && a.size() == 1) {
rc = e.fail_next(a[0]);
} else if (op == "Z" && a.empty()) {
e.reset();
} else if ((op == "P" && a.size() == 1) || (op == "R" && a.size() == 2)) {
const Word n = a.back();
group::Receipt r{n, n, n, n};
rc = op == "P" ? e.pump(r) : e.read(a[0], r);
receipt(rc, r);
return true;
} else if (op == "Q" && a.size() == 2) {
group::Item item{a[1], a[1]};
rc = e.peek(a[0], item);
std::cout << '[' << rc << ',' << item.id << ',' << item.value << "]\n";
return true;
} else if (op == "S" && a.empty()) {
const group::Status s = e.status();
std::cout << '[' << s.active << ',' << s.width << ',' << s.capacity
<< ',' << s.closing << ',' << s.queued << ',' << s.accepted
<< ',' << s.generation << ',' << s.ready << ',' << s.next_ticket << ',';
info(s.journal);
std::cout << "]\n";
return true;
} else if (op == "N" && a.size() == 2) {
const Word before = raw_hash(text);
Word value = a[1];
rc = group::parse_decimal(text, a[0], value);
std::cout << '[' << rc << ',' << value << "]\n";
unchanged(before, raw_hash(text));
return true;
} else if (op == "T" && a.size() == 1) {
const Word before = raw_hash(text);
group::Config config{a[0], a[0]};
rc = group::decode_config(text, config);
std::cout << '[' << rc << ',' << config.width << ',' << config.capacity << "]\n";
unchanged(before, raw_hash(text));
return true;
} else if (op == "V" && a.size() == 2) {
rc = group::validate_config({a[0], a[1]});
} else if (op == "K" && a.size() == 3 && a[2] <= 1) {
std::cout << '[' << group::take_count(a[0], a[1], a[2] != 0) << "]\n";
return true;
} else if (op == "JF" && a.size() == 1) {
rc = j.fail_next(a[0]);
} else if (op == "JZ" && a.empty()) {
j.reset();
} else if (op == "JS" && a.empty()) {
std::cout << '[';
info(j.info());
std::cout << "]\n";
return true;
} else if (op == "JR" && a.size() == 2) {
group::Receipt r{a[1], a[1], a[1], a[1]};
rc = j.read(a[0], r);
receipt(rc, r);
return true;
} else if ((op == "JC" || op == "B" || op == "D" || op == "H") && a.size() >= 3
&& a[2] <= 64 && a.size() == 3 + 2 * a[2]) {
std::vector<group::Item> items;
for (std::size_t k = 3; k < a.size(); k += 2) items.push_back({a[k], a[k + 1]});
const Word before = raw_items(items);
if (op == "B") {
rc = group::validate_items(a[0], items);
std::cout << '[' << rc << "]\n";
} else if (op == "H") {
std::cout << '[' << group::digest_items(items) << "]\n";
} else if (op == "D") {
const auto r = group::summarize(a[0], items);
receipt(0, r);
} else {
group::Receipt r{a[1], a[1], a[1], a[1]};
rc = j.commit(a[0], items, r);
receipt(rc, r);
}
unchanged(before, raw_items(items));
return true;
} else if (op == "E" && a.size() >= 2 && a[0] <= 64 && a[1] <= 64
&& a.size() == 2 + 2 * (a[0] + a[1])) {
std::vector<group::Item> left;
std::vector<group::Item> right;
for (std::size_t k = 2; k < 2 + 2 * a[0]; k += 2) left.push_back({a[k], a[k + 1]});
for (std::size_t k = 2 + 2 * a[0]; k < a.size(); k += 2) right.push_back({a[k], a[k + 1]});
const Word before_left = raw_items(left);
const Word before_right = raw_items(right);
std::cout << '[' << group::same_items(left, right) << "]\n";
unchanged(before_left, raw_items(left));
unchanged(before_right, raw_items(right));
return true;
} else {
return false;
}
std::cout << '[' << rc << "]\n";
return true;
}
}
int main()
{
group::Engine engines[2];
group::Journal journals[2];
Word selected = 0;
std::string line;
while (std::getline(std::cin, line)) {
const auto w = words(line);
if (w.empty() || w[0][0] == '#') continue;
if (w[0] == "U") {
if (w.size() != 2 || !number(w[1], selected) || selected > 1) return 2;
std::cout << '[' << selected << "]\n";
} else if (!execute(engines[selected], journals[selected], w)) {
return 2;
}
}
return std::cin.bad() ? 2 : 0;
}