-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
executable file
·134 lines (91 loc) · 2.95 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from PIL import Image, ImageDraw, ImageOps
from collections import OrderedDict
from itertools import islice
from functools import partial
import operator as op
import random
XS, Y0 = 800, 800
def read_boards():
mcu, mem = {}, {}
for rline in iter(raw_input, '====='):
line = rline.split(';')
pin, t, xy = (int(line[0]), int(line[1]),
tuple([180 + float(a.replace(',', '.')) * 30 for a in line[2:]]))
if t == 1:
mcu[pin] = xy
else:
mem[pin] = xy
return mcu, mem
def read_conns(mcu, mem):
conns = {}
for i, rline in enumerate(iter(raw_input, '#####')):
line = rline.split(';')
n1, n2 = map(int, line)
n1x, n1y = mcu[n1]
n2x, n2y = mem[n2]
c1 = n1x - n2x
c2 = n1y - n2y
conns[c1 * c2] = (n1x, n1y, n2x, n2y)
if i > 100:
break
return conns
def overlap(conn1, conn2):
deltas = [op.sub(*p) for p in zip(conn1, conn2)]
xs, ys = deltas[::2], deltas[1::2]
return not (all(t >= 0 for t in xs) and all(t <= 0 for t in ys)
or all(t <= 0 for t in xs) and all(t >= 0 for t in ys))
def group_conns(conns):
groups = [[]]
for xconn in conns.values():
added = False
o = partial(overlap, xconn)
for group in groups:
if any(map(o, group)):
continue
else:
group.append(xconn)
added = True
break
if not added:
groups.append([xconn])
return groups
def draw_boards(drw, mcu, mem):
for xy in mcu.values():
x, y = xy
drw.ellipse(((x - 2, y - 2), (x + 2, y + 2)), fill='red', outline='red')
for xy in mem.values():
x, y = xy
drw.ellipse(((x - 2, y - 2), (x + 2, y + 2)), fill='blue', outline='blue')
def draw_conn(drw, conn):
n1x, n1y, n2x, n2y = conn
color = '#%06x' % random.randint(0, 0xFFFFFF)
drw.line(((n1x, n1y), (n1x, n2y)), fill=color, width=1)
drw.line(((n1x, n2y), (n2x, n2y)), fill=color, width=1)
drw.line(((n1x, n1y), (n2x, n2y)), fill='white', width=1)
def draw_groups(groups, mcu, mem):
for n, group in enumerate(groups):
img = Image.new('RGBA', (XS, Y0), (0, 0, 0, 255))
drw = ImageDraw.Draw(img)
draw_boards(drw, mcu, mem)
for conn in group:
draw_conn(drw, conn)
img = ImageOps.flip(img)
img.save('%s.png' % n)
del drw
def main():
mcu, mem = read_boards()
conns = read_conns(mcu, mem)
groups = group_conns(conns)
draw_groups(groups, mcu, mem)
sconns = OrderedDict(sorted(conns.items()))
img = Image.new('RGBA', (XS, Y0), (0, 0, 0, 255))
drw = ImageDraw.Draw(img)
draw_boards(drw, mcu, mem)
for f in islice(sconns, 100):
draw_conn(drw, conns[f])
img = ImageOps.flip(img)
img.show()
img.save('test.png')
del drw
if __name__ == '__main__':
main()