Skip to content

Instantly share code, notes, and snippets.

@00xc

00xc/solve.py Secret

Last active February 24, 2021 14:49

Revisions

  1. 00xc revised this gist Feb 24, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions solve.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    # Takes a group of 3 bytes and parses it into an offset, length and character
    class Group:
    def __init__(self, b):
    self.off = ((b[1] & 0b11) << 8) | b[0]
    self.num = b[1] >> 2
    self.ch = chr(b[2])

    # Yields lst in chunks of size n
    def get_chunks(lst, n):
    for i in range(0, len(lst), n):
    yield lst[i:i + n]
  2. 00xc created this gist Feb 24, 2021.
    43 changes: 43 additions & 0 deletions solve.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    class Group:
    def __init__(self, b):
    self.off = ((b[1] & 0b11) << 8) | b[0]
    self.num = b[1] >> 2
    self.ch = chr(b[2])

    def get_chunks(lst, n):
    for i in range(0, len(lst), n):
    yield lst[i:i + n]

    def out_to_string(data):
    return "".join(o.ch for o in data)

    if __name__ == '__main__':

    with open("hacker_manifesto.txt", "rb") as f:
    data = f.read()

    # Parse the bytes into groups of 3
    data = [Group(chunk) for chunk in get_chunks(data, 3)]

    # Iterate through the groups
    out = []
    for i, group in enumerate(data):

    if group.off == 0 and group.num == 0:
    out.append(group)

    else:

    start = -group.off
    end = start + group.num

    # We need this check because out[-n:0] does not work as expected (out[-n:0] != out[-n:])
    if end == 0:
    out += out[start:]
    else:
    out += out[start:end]

    out.append(group)

    print(out_to_string(out))