"""ALE-Bench ahc019 (AHC 019 "Silhouette Block Puzzle Creation"): Python port of the official tools/src/lib.rs (parse_output, compute_score, is_same with the tool's own rotation enumeration). Answer = the contestant's stdout text. Lower is better.""" import re from run import Invalid _UINT = re.compile(r"\+?[0-9]+\Z") # Rust usize::from_str: optional '+', ASCII digits _WS = re.compile(r"[\t\n\x0b\x0c\r ]+") MAX_LEN = 1_000_000 D3 = [(0, 0, -1), (0, 0, 1), (0, -1, 0), (0, 1, 0), (-1, 0, 0), (1, 0, 0)] def read(tokens, pos, lb, ub): if pos >= len(tokens): raise Invalid("Unexpected EOF") t = tokens[pos] if not _UINT.match(t) or len(t) > 25 or int(t) >= 2 ** 64: raise Invalid(f"Parse error: {t[:20]}") v = int(t) if v < lb or ub < v: raise Invalid(f"Out of range: {v}") return v def parse_input(text): tok = text.split() D = int(tok[0]) k = 1 f, r = [], [] for _ in range(2): f.append([[int(c) for c in tok[k + z]] for z in range(D)]) k += D r.append([[int(c) for c in tok[k + z]] for z in range(D)]) k += D return D, f, r def normalize(b): mx = min(p[0] for p in b) my = min(p[1] for p in b) mz = min(p[2] for p in b) return [(x - mx, y - my, z - mz) for (x, y, z) in b] def is_same(b1, b2): if len(b1) != len(b2): return False b1 = sorted(normalize(b1)) b2 = normalize(b2) max_x = max(p[0] for p in b2) max_y = max(p[1] for p in b2) max_z = max(p[2] for p in b2) for i in range(6): for _ in range(4): b2.sort() if b1 == b2: return True b2 = [(max_y - y, x, z) for (x, y, z) in b2] max_x, max_y = max_y, max_x if i & 1: b2 = [(x, max_z - z, y) for (x, y, z) in b2] max_y, max_z = max_z, max_y else: b2 = [(z, y, max_x - x) for (x, y, z) in b2] max_x, max_z = max_z, max_x return False def check(inst, ans): out = ans.get("output") if not isinstance(out, str): raise Invalid("output must be a string") if len(out) > MAX_LEN: raise Invalid(f"output longer than {MAX_LEN} characters") if not out.isascii(): raise Invalid("output must be ASCII") D, F, R = parse_input(inst["input"]) tokens = [t for t in _WS.split(out) if t] n = read(tokens, 0, 0, 1000000) b = [] p = 1 for i in range(2): bi = [[[0] * D for _ in range(D)] for _ in range(D)] for x in range(D): for y in range(D): for z in range(D): bi[x][y][z] = read(tokens, p, 0, n) p += 1 b.append(bi) if len(tokens) > p: raise Invalid("Too many outputs") pos = [dict(), dict()] for i in range(2): bi = b[i] f = [[0] * D for _ in range(D)] r = [[0] * D for _ in range(D)] visited = [[[False] * D for _ in range(D)] for _ in range(D)] for x in range(D): for y in range(D): for z in range(D): bid = bi[x][y][z] if bid != 0: f[z][x] = 1 r[z][y] = 1 lst = pos[i].setdefault(bid, []) lst.append((x, y, z)) if len(lst) == 1: visited[x][y][z] = True stack = [(x, y, z)] while stack: cx, cy, cz = stack.pop() for dx, dy, dz in D3: x2, y2, z2 = cx + dx, cy + dy, cz + dz if 0 <= x2 < D and 0 <= y2 < D and 0 <= z2 < D and bi[x2][y2][z2] == bid \ and not visited[x2][y2][z2]: visited[x2][y2][z2] = True stack.append((x2, y2, z2)) elif not visited[x][y][z]: raise Invalid(f"block {bid} is not connected in the object {i + 1}") if f != F[i]: raise Invalid(f"The front silhouette for object {i + 1} does not match.") if r != R[i]: raise Invalid(f"The right silhouette for object {i + 1} does not match.") used = set(pos[0]) | set(pos[1]) if len(used) != n: raise Invalid("some block is not used") s = 0.0 for bid in range(1, n + 1): p0, p1 = pos[0].get(bid, []), pos[1].get(bid, []) if not p0 or not p1: s += float(len(p0) + len(p1)) elif is_same(p0, p1): s += 1.0 / len(p0) else: raise Invalid(f"The shape of block {bid} differs between objects 1 and 2.") return float(round_half_away(1e9 * s)), {"n": n} def round_half_away(x): import math r = math.floor(x) return r + 1 if x - r >= 0.5 else r