#include "group.hpp" #include namespace group { int validate_config(const Config &config) { if (config.width == 0 || config.width > max_width) { return range; } if (config.capacity < config.width || config.capacity > max_capacity) { return range; } return 0; } int validate_items(Word ticket, const std::vector &items) { if (items.empty() || items.size() > max_width) { return invalid; } if (ticket == 0 || ticket > max_id) { return range; } for (const Item &item : items) { if (item.id == 0 || item.id > max_id || item.value > max_value) { return range; } } std::set seen; for (const Item &item : items) { if (!seen.insert(item.id).second) { return exists; } } return 0; } Word take_count(Word queued, Word width, bool closing) { if (width == 0 || width > max_width || queued > max_capacity) { return 0; } const Word available = closing ? queued : (queued / width) * width; return available < width ? available : width; } Word digest_items(const std::vector &items) { Word hash = 14695981039346656037ULL; for (const Item &item : items) { const Word fields[2] = {item.id, item.value}; for (Word field : fields) { for (unsigned shift = 0; shift < 64; shift += 8) { const Word byte = (field >> shift) & 255; hash ^= byte; hash *= 1099511628211ULL; } } } return hash; } Receipt summarize(Word ticket, const std::vector &items) { Receipt result; result.ticket = ticket; result.count = items.size(); for (const Item &item : items) { result.total += item.value; } result.digest = digest_items(items); return result; } bool same_items(const std::vector &left, const std::vector &right) { if (left.size() != right.size()) { return false; } for (std::size_t index = 0; index < left.size(); ++index) { if (left[index].id != right[index].id) { return false; } if (left[index].value != right[index].value) { return false; } } return true; } }