mirror of
https://git.soft.fish/val/MicroCorruption.git
synced 2024-11-24 17:55:58 +00:00
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
|
import string
|
||
|
from string import printable
|
||
|
from math import ceil, floor
|
||
|
|
||
|
valid = 0
|
||
|
valid_chars = (string.digits + string.ascii_uppercase + string.ascii_lowercase).encode()
|
||
|
|
||
|
def u16(i:int):
|
||
|
return i & 0xffff
|
||
|
|
||
|
def half_pair(i: int) -> list:
|
||
|
i = u16(i)
|
||
|
return [floor(i/2), ceil(i/2)]
|
||
|
|
||
|
def neg_half_pair(i: int) -> list:
|
||
|
# 2's c negate i
|
||
|
i = 0x10000 - u16(i)
|
||
|
return half_pair(i)
|
||
|
|
||
|
def not_in_set(set, start: int = 0x0, end: int = 0xff):
|
||
|
for char in range(start, end+1):
|
||
|
if char not in set:
|
||
|
print(F"{char:02x}[{chr(char)}]", end=" ")
|
||
|
|
||
|
|
||
|
def isprintable(char: int):
|
||
|
return f'{chr(char)}' if char > 0x30 and chr(char) in printable else "."
|
||
|
|
||
|
# for solution in sorted(solutions):
|
||
|
# print(f"{solution:02x} '{isprintable(solution)}'= {solutions[solution]}")
|
||
|
|
||
|
def bfs(start, end, characters = valid_chars): #function for BFS
|
||
|
solutions = {}
|
||
|
queue = []
|
||
|
# Populate solutions with trivial solutions, and queue with the same
|
||
|
for character in characters:
|
||
|
solutions[character] = f'{character:x}';
|
||
|
queue.append(character)
|
||
|
# loop until the queue is empty, and every reachable number has been found
|
||
|
while queue:
|
||
|
current = queue.pop(0)
|
||
|
for neighbor in characters:
|
||
|
for operator, operation in [['+', int.__add__], ['-', int.__sub__]]:
|
||
|
new = operation(current, neighbor)
|
||
|
if new in solutions: continue
|
||
|
if new in range(start, end+1):
|
||
|
solutions[new] = f"{solutions[current]} {operator} {neighbor:x}"
|
||
|
queue.append(new)
|
||
|
return solutions
|
||
|
|
||
|
super_valid_chars:bytes = []
|
||
|
for one in valid_chars:
|
||
|
for two in valid_chars:
|
||
|
super_valid_chars.append (int.from_bytes(one.to_bytes(1, 'big') + two.to_bytes(1, 'big'), 'big'))
|
||
|
|
||
|
|
||
|
solutions = bfs(0, 0xffff, super_valid_chars)
|
||
|
for solution in sorted(solutions):
|
||
|
bas = solution.to_bytes(2, 'big')
|
||
|
print(f"{solution:04x} '{isprintable(bas[0])}{isprintable(bas[1])}'= {solutions[solution]}")
|
||
|
|
||
|
exit()
|