diff options
author | jack <[email protected]> | 2024-04-01 20:07:06 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-04-01 20:07:06 -0600 |
commit | 907a432ce4b3bac3874d104c7981a8a5d90f399f (patch) | |
tree | 579024a4e3c5bbfd43904068e3888c919481d881 | |
parent | f63cbd900b2e67a2d7fa82c2983d71921e5d1e30 (diff) | |
download | synacor_challenge-907a432ce4b3bac3874d104c7981a8a5d90f399f.tar.gz synacor_challenge-907a432ce4b3bac3874d104c7981a8a5d90f399f.zip |
add python tools
-rw-r--r-- | readme.md | 3 | ||||
-rwxr-xr-x | tools/ackermann.py | 34 | ||||
-rwxr-xr-x | tools/coin.py | 14 | ||||
-rwxr-xr-x | tools/maze.py | 63 | ||||
-rwxr-xr-x | tools/validate.py | 28 | ||||
-rwxr-xr-x | validate.sh | 26 |
6 files changed, 140 insertions, 28 deletions
@@ -1,9 +1,8 @@ -see [here](https://lzycat.xyz/synacor) for more information. binary file, codes, and checksums come +see [here](https://lzycat.xyz/projects) for more information. binary file, codes, and checksums come from [Aneurysm9/vm_challenge](https://github.com/Aneurysm9/vm_challenge), as the original [site](https://challenge.synacor.com) is no longer up. usage: ```shell make ./vm challenge.bin -./validate.sh codes.txt ``` diff --git a/tools/ackermann.py b/tools/ackermann.py new file mode 100755 index 0000000..01c90fa --- /dev/null +++ b/tools/ackermann.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + + +def ackermann(a, b, c): + stack = [(a, b)] + res = 0 + while stack: + a, b = stack.pop() + if a == 0: + res = b + 1 + elif a == 1: + res = b + c + 1 + elif a == 2: + res = (b + 2) * c + (b + 1) + elif b == 0: + stack.append((a - 1, c)) + else: + stack.append((a, b - 1)) + stack.append((a - 1, b)) + return res + + +def check_ack(): + for i in range(32768): + print("checking:", i) + res = ackermann(4, 1, i) % 32768 + if res == 6: + return i + return -1 + + +if __name__ == "__main__": + res = check_ack() + print("magic number:", res) diff --git a/tools/coin.py b/tools/coin.py new file mode 100755 index 0000000..fdf7e5c --- /dev/null +++ b/tools/coin.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +from itertools import permutations + + +def check_resution(arr): + a, b, c, d, e = arr + return a + b * c**2 + d**3 - e == 399 + + +if __name__ == "__main__": + coins = {2: "red", 3: "corroded", 5: "shiny", 7: "concave", 9: "blue"} + res = next(i for i in permutations(coins.keys(), len(coins)) if check_resution(i)) + print("order: " + ", ".join([coins[i] for i in res])) + print("({} + {}*{}^2 + {}^3 - {} = 399)".format(*res)) diff --git a/tools/maze.py b/tools/maze.py new file mode 100755 index 0000000..fa2ab2f --- /dev/null +++ b/tools/maze.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +import operator +from collections import deque + +maze = [ + ["*", "8", "-", "1"], + ["4", "*", "11", "*"], + ["+", "4", "-", "18"], + ["22", "-", "9", "*"], +] + + +def result(path): + res, op = int(path[0]), None + path = path[1:] + operators = {"+": operator.add, "*": operator.mul, "-": operator.sub} + for i in path: + if i in operators: + op = operators[i] + else: + res = op(res, int(i)) + return res + + +def solved(state): + x, y, p, _, _ = state + return x == 3 and y == 0 and result(p) == 30 + + +def hash(state): + x, y, p, _, _ = state + return ",".join(map(str, (x, y, result(p)))) + + +if __name__ == "__main__": + state = (0, 3, ["22"], 0, "") + seen = {} + dfs_s = deque([state]) + sols = [] + while dfs_s: + curr = dfs_s.popleft() + x, y, p, s, d = curr + print("%s steps \t%s " % (s, " ".join(p))) + if solved(curr): + sols.append(curr) + break + if hash(curr) in seen and seen[hash(curr)] < s: + continue + elif x == 3 and y == 0: + continue + seen[hash(curr)] = s + if x == 0 and y == 3 and s != 0: + continue + if x > 0: + dfs_s.append((x - 1, y, p + [maze[y][x - 1]], s + 1, d + "W")) + if x < 3: + dfs_s.append((x + 1, y, p + [maze[y][x + 1]], s + 1, d + "E")) + if y > 0: + dfs_s.append((x, y - 1, p + [maze[y - 1][x]], s + 1, d + "N")) + if y < 3: + dfs_s.append((x, y + 1, p + [maze[y + 1][x]], s + 1, d + "S")) + print("solutions") + print(sols) diff --git a/tools/validate.py b/tools/validate.py new file mode 100755 index 0000000..e31b627 --- /dev/null +++ b/tools/validate.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +import hashlib +import sys + +if len(sys.argv) < 2: + print("usage:", sys.argv[0], "<codes.txt>") + sys.exit(1) + +n = 0 +sums = [ + "76ec2408e8fe3f1753c25db51efd8eb3", + "0e6aa7be1f68d930926d72b3741a145c", + "7997a3b2941eab92c1c0345d5747b420", + "186f842951c0dcfe8838af1e7222b7d4", + "2bf84e54b95ce97aefd9fc920451fc45", + "e09640936b3ef532b7b8e83ce8f125f4", + "4873cf6b76f62ac7d5a53605b2535a0c", + "d0c54d4ed7f943280ce3e19532dbb1a6", +] + +with open(sys.argv[1], "r") as file: + for i in file: + i = i.strip() + curr = hashlib.md5(i.encode()).hexdigest() + print("code:", i) + print("produce:\t", curr) + print("expect:\t\t", sums[n]) + n += 1 diff --git a/validate.sh b/validate.sh deleted file mode 100755 index b1247ea..0000000 --- a/validate.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -if [ "$#" -eq 0 ]; then - printf "usage: %s <codes.txt>\n" $0 - exit 1 -fi - -n=0 -master="6fcd818224b42f563e10b91b4f2a5ae8" -sums=( - "76ec2408e8fe3f1753c25db51efd8eb3" - "0e6aa7be1f68d930926d72b3741a145c" - "7997a3b2941eab92c1c0345d5747b420" - "186f842951c0dcfe8838af1e7222b7d4" - "2bf84e54b95ce97aefd9fc920451fc45" - "e09640936b3ef532b7b8e83ce8f125f4" - "4873cf6b76f62ac7d5a53605b2535a0c" - "d0c54d4ed7f943280ce3e19532dbb1a6" -) - -while read i; do - curr=$(echo -n "$i" | md5sum $master - 2>/dev/null) - curr="${curr%?}" - printf "code %s:\nproduce:\t%s\nexpect:\t\t%s\n" $i $curr ${sums[$n]} - ((n++)) -done < "$1" |