|
1 | 1 | import networkx as nx
|
2 |
| -from collections import * |
3 | 2 |
|
4 |
| - |
5 |
| -coords = {x+1j*y: c for y, r in enumerate(open(0)) for x, c in enumerate(r.strip().replace(".", " "))} |
6 |
| - |
7 |
| -d4 = [1, 1j, -1, -1j] |
| 3 | +coords = {x+1j*y: c for y, r in enumerate(open(0)) for x, c in enumerate(r.strip())} |
8 | 4 |
|
9 | 5 | G = nx.Graph()
|
10 | 6 | for c in coords:
|
11 | 7 | for d in [1, 1j, -1, -1j]:
|
12 | 8 | if coords[c] != '#' != coords[c+d]:
|
13 | 9 | G.add_edge(c, c+d)
|
14 |
| - # G.add_edge((c, 1), (c+d, 1)) |
15 | 10 |
|
16 | 11 | S = [c for c in coords if coords[c] in 'S'][0]
|
17 |
| -E = [c for c in coords if coords[c] in 'E'][0] |
18 |
| -# EL = nx.shortest_path_length(G, target=E) |
19 |
| -SL = nx.shortest_path_length(G, source=S) |
20 |
| -print(SL) |
21 |
| -# print(EL) |
22 |
| -C= Counter() |
23 |
| - |
24 |
| -sl = list(SL.items()) |
25 |
| -seen = set() |
26 |
| -s2 = 0 |
27 |
| -for i, (c1, dist1) in enumerate(sl): |
28 |
| - for c2, dist2 in sl[i+1:]: |
29 |
| - diff = c2-c1 |
30 |
| - diff = int(abs(diff.real) + abs(diff.imag)) |
31 |
| - if diff <= 20: |
32 |
| - D = dist2 - dist1 - diff |
33 |
| - if D > 0: |
34 |
| - C[D] += 1 |
35 |
| - if D >= 100: |
36 |
| - s2 += 1 |
37 |
| - |
38 |
| -for asd in sorted(C.items()): |
39 |
| - print(asd) |
40 |
| - |
41 |
| -print(s2) |
42 |
| -exit() |
43 |
| -# L = {l: max(EL[l], SL[l]) for l in EL} |
44 |
| - |
45 |
| -for c in coords: |
46 |
| - for d in [1, 1j, -1, -1j]: |
47 |
| - if coords[c] != '#' != coords[c+d]: |
48 |
| - G.add_edge(c, c+d) |
49 |
| - G.add_edge((c, 1), (c+d, 1)) |
| 12 | +coord_to_dist = nx.shortest_path_length(G, source=S).items() |
50 | 13 |
|
51 |
| -# L = [el+sl for ] |
| 14 | +s1 = s2 = 0 |
| 15 | +for c1, dist1 in coord_to_dist: |
| 16 | + for c2, dist2 in coord_to_dist: |
| 17 | + diff = int(abs((c2-c1).real) + abs((c2-c1).imag)) |
| 18 | + if dist2 - dist1 - diff >= 100: |
| 19 | + s1 += diff <= 2 |
| 20 | + s2 += diff <= 20 |
52 | 21 |
|
53 |
| -# print(L) |
54 |
| -# print(L[S]) |
55 |
| -# exit(0) |
56 |
| - |
57 |
| -C = Counter() |
58 |
| - |
59 |
| -s1 = 0 |
60 |
| -seen= set() |
61 |
| -for c in coords: |
62 |
| - for d in [1, 1j, -1, -1j]: |
63 |
| - for d2 in [d]: |
64 |
| - diff = 0 |
65 |
| - for d3 in [0]: |
66 |
| - c2 = c+d+d2+d3 |
67 |
| - if c2 in coords and coords[c] != '#' != coords[c2] and c != c2: |
68 |
| - delta = d+d2+d3 |
69 |
| - new = SL[c2] - SL[c] - int(abs(delta.imag) + abs(delta.real)) |
70 |
| - print("\t", d3, new) |
71 |
| - diff = max(diff, new) |
72 |
| - if (c, c+d+d2) not in seen and diff > 0: |
73 |
| - seen.add((c, c+d+d2)) |
74 |
| - seen.add((c+d+d2, c)) |
75 |
| - if diff > 0: |
76 |
| - # print(c, c2) |
77 |
| - C[diff] += 1 |
78 |
| - if diff >= 30: |
79 |
| - print(diff) |
80 |
| - for y in range(15): |
81 |
| - for x in range(15): |
82 |
| - X=x+y*1j |
83 |
| - print(end=coords[X] if X not in [c, c+d+d2] else "!") |
84 |
| - print(end=f"{SL.get(X, '##'):2}") |
85 |
| - print() |
86 |
| - print() |
87 |
| - if diff >= 100: |
88 |
| - s1 += 1 |
89 |
| - |
90 |
| - |
91 |
| -for asd in sorted(C.items()): |
92 |
| - print(asd) |
93 |
| - |
94 |
| -# 536 |
95 |
| -# 677 |
96 |
| -# 1923 |
97 |
| - |
98 |
| - |
99 |
| -# for path in nx.all_simple_paths(G, S, (E, 1), cutoff=L): |
100 |
| -# if L - len(path) >= 100: |
101 |
| -# s1 += 1 |
102 | 22 | print(s1)
|
103 |
| -# paths = list(nx.all_shortest_paths(G, S, (E, 0), "weight")) |
104 |
| - |
105 |
| -# print(sum(G.edges[e]["weight"] for e in zip(paths[0], paths[0][1:]))) |
106 |
| -# print(len({p[0] for p in sum(paths, [])})) |
| 23 | +print(s2) |
0 commit comments