from pathlib import Path import hashlib import struct
BOARD_SIZE = 100 MEMORY_SIZE = 0x2000 DECODE_MULTIPLIER = 0x3D DECODE_OFFSET = 0x59 HASH_SUFFIX_ADDR = 300 HASH_SUFFIX_LEN = 16 FLAG_PREFIX = "0xGame"
OP_STORE_IMM8 = 0x22 OP_XOR_IMM8 = 0x23 OP_MOV_IMM8 = 0x10 OP_CMP = 0x40 OP_JE = 0x42 OP_JMP = 0x41 OP_LEN = { 0x00: 0, 0x10: 2, 0x11: 2, 0x12: 3, 0x20: 2, 0x21: 2, 0x22: 3, 0x23: 3, 0x24: 3, 0x25: 2, 0x30: 3, 0x31: 3, 0x32: 3, 0x33: 3, 0x34: 1, 0x40: 2, 0x41: 2, 0x42: 2, 0x43: 2, 0x44: 2, 0x45: 2, 0x50: 1, 0x60: 3, 0x61: 4, 0x62: 2, }
def decode_bytecode(path: Path) -> bytes: raw = path.read_bytes() return bytes( byte ^ ((offset * DECODE_MULTIPLIER - DECODE_OFFSET) & 0xFF) for offset, byte in enumerate(raw) )
def parse_instructions(code: bytes): pc = 0 instructions = {} while pc < len(code): start = pc opcode = code[pc] pc += 1 arg_len = OP_LEN[opcode] args = code[pc:pc + arg_len] pc += arg_len instructions[start] = (opcode, bytes(args), pc) return instructions
def u16(args: bytes) -> int: return args[0] | (args[1] << 8)
def jump_target(pc_after: int, args: bytes) -> int: return pc_after + struct.unpack("<h", args)[0]
def extract_initial_memory(instructions) -> list[int]: memory = [0] * MEMORY_SIZE pc = 0 while True: opcode, args, next_pc = instructions[pc] if opcode != OP_STORE_IMM8: break memory[u16(args)] = args[2] pc = next_pc return memory
def extract_case_targets(instructions) -> dict[int, int]: targets = {} for pc, (opcode, args, next_pc) in instructions.items(): if opcode != OP_MOV_IMM8 or (args[0] & 0x0F) != 1: continue value = args[1] if value >= BOARD_SIZE: continue cmp_inst = instructions.get(next_pc) je_inst = instructions.get(cmp_inst[2]) if cmp_inst else None if not cmp_inst or not je_inst: continue if cmp_inst[0] == OP_CMP and je_inst[0] == OP_JE: targets[value] = jump_target(je_inst[2], je_inst[1]) if len(targets) != BOARD_SIZE: raise ValueError(f"expected {BOARD_SIZE} input cases, got {len(targets)}") return targets
def extract_toggles(instructions, case_targets: dict[int, int]) -> dict[int, list[int]]: toggles = {} for value, pc in case_targets.items(): changed = [] while True: opcode, args, next_pc = instructions[pc] if opcode == OP_XOR_IMM8: if args[2] != 1: raise ValueError(f"unexpected xor immediate at 0x{pc:x}") changed.append(u16(args)) pc = next_pc continue if opcode == OP_JMP: break raise ValueError(f"unexpected opcode 0x{opcode:02x} at 0x{pc:x}") toggles[value] = changed return toggles
def solve_gf2(initial: list[int], toggles: dict[int, list[int]]) -> list[int]: rhs = [1 ^ bit for bit in initial] rows = [] for cell in range(BOARD_SIZE): row = 0 for node, changed in toggles.items(): if cell in changed: row ^= 1 << node if rhs[cell]: row ^= 1 << BOARD_SIZE rows.append(row)
pivot_columns = [] pivot_row = 0 for column in range(BOARD_SIZE): pivot = next((i for i in range(pivot_row, BOARD_SIZE) if (rows[i] >> column) & 1), None) if pivot is None: continue rows[pivot_row], rows[pivot] = rows[pivot], rows[pivot_row] for i in range(BOARD_SIZE): if i != pivot_row and ((rows[i] >> column) & 1): rows[i] ^= rows[pivot_row] pivot_columns.append(column) pivot_row += 1
if pivot_row != BOARD_SIZE: raise ValueError(f"matrix rank is {pivot_row}, expected a unique full-rank solve")
solution = [] for row_index, column in enumerate(pivot_columns): if (rows[row_index] >> BOARD_SIZE) & 1: solution.append(column) return solution
def verify_solution(initial: list[int], toggles: dict[int, list[int]], solution: list[int]) -> None: state = initial[:] for node in solution: for cell in toggles[node]: state[cell] ^= 1 if state != [1] * BOARD_SIZE: raise ValueError("solution does not turn every light on")
def calc_node_value(solution: list[int], suffix: bytes) -> str: input_history = "".join(map(str, solution)).encode() digest = hashlib.md5(input_history + suffix).digest() return bytes(byte ^ suffix[i] for i, byte in enumerate(digest)).hex()
def main() -> None: code = decode_bytecode(Path(__file__).with_name("Light.bin")) instructions = parse_instructions(code) memory = extract_initial_memory(instructions) initial = memory[:BOARD_SIZE] suffix = bytes(memory[HASH_SUFFIX_ADDR:HASH_SUFFIX_ADDR + HASH_SUFFIX_LEN]) toggles = extract_toggles(instructions, extract_case_targets(instructions)) solution = solve_gf2(initial, toggles) verify_solution(initial, toggles, solution)
node_value = calc_node_value(solution, suffix) inner = node_value print(f"operation_count = {len(solution)}") print("nodes =", " ".join(map(str, solution))) print(f"node_value = {node_value}") print(f"flag_inner = {inner}") print(f"flag = {FLAG_PREFIX}{{{inner}}}")
if __name__ == "__main__": main()
|