From: Skullheadx Date: Mon, 13 Feb 2023 22:55:02 +0000 (-0500) Subject: 2016 S4 attempt X-Git-Url: http://git.skullheadx.com/nixos/static/now.html?a=commitdiff_plain;h=HEAD;p=CCC.git 2016 S4 attempt --- diff --git a/Main/Python/2016/S4.py b/Main/Python/2016/S4.py new file mode 100644 index 0000000..1fdc065 --- /dev/null +++ b/Main/Python/2016/S4.py @@ -0,0 +1,64 @@ +from queue import Queue + +N = int(input()) +riceballs = list(map(int, input().split())) + + +def find_operations(r, start=0, end=None): + if end is None: + end = len(r) - 1 + output = [] + for i in range(start, end): + if i < len(r) - 1: + if r[i] == r[i + 1]: + output.append((i, i + 1)) + if i < len(r) - 2: + if r[i] == r[i + 2]: + output.append((i, i + 2)) + return output + + +def clean(ops, start=0, end=None): + if end is None: + end = len(ops) + output = [] + for op in ops: + a, b = op + if not (start < a < end or start < b < end): + if a > end: + a -= 1 + if b > end: + b -= 1 + output.append((a, b)) + return output + + +q = Queue() +q.put((riceballs, find_operations(riceballs))) +visited = set() +output = 0 +while not q.empty(): + r, ops = q.get() + if tuple(r) in visited: + continue + visited.add(tuple(r)) + + output = max(output, max(r)) + + for op in ops: + temp = r[:] + a, b = op + if a > len(temp)-1 or b > len(temp)-1 or temp[a] != temp[b]: + continue + + if b - a == 1: + temp = temp[:op[0]] + [temp[op[0]] * 2] + temp[op[1] + 1:] + start, end = max(0, a - 2), min(a + 2, len(temp)) + temp2 = find_operations(temp, start, end) + ops + q.put((temp, temp2)) + else: + temp = temp[:op[0]] + [sum(temp[a:b + 1])] + temp[op[1] + 1:] + start, end = max(0, a - 2), min(a + 2, len(temp)) + temp2 = find_operations(temp, start, end) + ops + q.put((temp, temp2)) +print(output)