|
| 1 | +from collections import * |
| 2 | +from itertools import * |
| 3 | +from functools import * |
| 4 | +# import numpy as np |
| 5 | +# import networkx as nx |
| 6 | +# import z3 |
| 7 | +import re |
| 8 | +import sys |
| 9 | +sys.setrecursionlimit(1000000) |
| 10 | + |
| 11 | +s1 = s2 = 0 |
| 12 | + |
| 13 | +# d4 = [1, 1j, -1, -1j] |
| 14 | +d4 = {'>': 1, 'v':1j, '<':-1, '^':-1j} |
| 15 | +# d8 = d4 + [1+1j, 1-1j, -1+1j, -1-1j] |
| 16 | +# d4half = [i/2 for i in d4] |
| 17 | +# d8half = [i/2 for i in d8] |
| 18 | +def adjacent(coord, dirs=d4): |
| 19 | + return [coord + d for d in dirs] |
| 20 | + |
| 21 | + |
| 22 | +field, moves = open(0).read().split("\n\n") |
| 23 | + |
| 24 | + |
| 25 | +if True: |
| 26 | + for r, r2 in [("#", "##"), ("O", "[]"), (".", ".."), ("@", "@.")]: |
| 27 | + field = field.replace(r, r2) |
| 28 | + |
| 29 | +coords = {x+1j*y: c for y, r in enumerate(field.strip().splitlines()) for x, c in enumerate(r.strip())} |
| 30 | + |
| 31 | +# field = [list(l) for l in field.strip().split("\n")] |
| 32 | +s = 0 |
| 33 | +for c, char in coords.items(): |
| 34 | + if char == "@": |
| 35 | + s = c |
| 36 | + coords[c] = "." |
| 37 | + |
| 38 | +M = max(map(lambda c: c.real, coords)) |
| 39 | +for c, char in coords.items(): |
| 40 | + print(end=char) |
| 41 | + if c.real == M: |
| 42 | + print() |
| 43 | + |
| 44 | + |
| 45 | +def collect(s, d): |
| 46 | + if coords[s+d] in "[]": |
| 47 | + dn = (1 if coords[s+d] == '[' else -1) + d |
| 48 | + base = {s+d: coords[s+d], s+dn: coords[s+dn]} | collect(s+d, d) |
| 49 | + if d not in [1, -1]: |
| 50 | + base |= collect(s+dn, d) |
| 51 | + return base |
| 52 | + return {} |
| 53 | + |
| 54 | +for m in moves.strip(): |
| 55 | + if m not in d4: |
| 56 | + continue |
| 57 | + # print(s) |
| 58 | + d = d4[m] |
| 59 | + n = s + d |
| 60 | + i = 1 |
| 61 | + |
| 62 | + |
| 63 | + ok = True |
| 64 | + if coords[s + d] in '[]': |
| 65 | + boxes = collect(s, d) |
| 66 | + # print(boxes) |
| 67 | + for x, char in boxes.items(): |
| 68 | + if coords[x+d] == '#': |
| 69 | + ok = False |
| 70 | + break |
| 71 | + if ok: |
| 72 | + for x, char in boxes.items(): |
| 73 | + coords[x] = '.' |
| 74 | + for x, char in boxes.items(): |
| 75 | + coords[x+d] = char |
| 76 | + s = n |
| 77 | + elif coords[s+d] == '.': |
| 78 | + s = n |
| 79 | + |
| 80 | + # while coords[s + d * i] in '[]': |
| 81 | + # i += 1 |
| 82 | + # if coords[s+d*i] == '.': |
| 83 | + # coords[s+d*i] = 'O' |
| 84 | + # coords[n] = '.' |
| 85 | + # s = n |
| 86 | + |
| 87 | + # print("\033[0;0H") |
| 88 | + # for c, char in coords.items(): |
| 89 | + # print(end=char if c != s else '@') |
| 90 | + # if c.real == M: |
| 91 | + # print() |
| 92 | + # import time |
| 93 | + # time.sleep(0.02) |
| 94 | + |
| 95 | +for c, char in coords.items(): |
| 96 | + if char == '[': |
| 97 | + s1 += c.imag * 100 + c.real |
| 98 | + |
| 99 | + |
| 100 | +# for line in open(0): |
| 101 | + # n = [int(a) for a in line.split()] |
| 102 | + # re.findall(r"\d+", line) |
| 103 | + |
| 104 | + |
| 105 | +print(int(s1), s2, sep="\n") |
| 106 | +# not 6186250.0 |
0 commit comments