-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08a.py
executable file
·135 lines (104 loc) · 2.99 KB
/
08a.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
135
#!/usr/bin/env python3
# 0: 1: 2: 3: 4:
# aaaa .... aaaa aaaa ....
# b c . c . c . c b c
# b c . c . c . c b c
# .... .... dddd dddd dddd
# e f . f e . . f . f
# e f . f e . . f . f
# gggg .... gggg gggg ....
# 5: 6: 7: 8: 9:
# aaaa aaaa aaaa aaaa aaaa
# b . b . . c b c b c
# b . b . . c b c b c
# dddd dddd .... dddd dddd
# . f e f . f e f . f
# . f e f . f e f . f
# gggg gggg .... gggg gggg
segam = {
2: [1],
3: [7],
4: [4],
5: [2, 3, 5],
6: [0, 6, 9],
7: [8],
}
import numpy as np
def minus(a, b):
uniq = a
for bb in b:
uniq = uniq.replace(bb, '')
return uniq
def equal(a,b):
return sorted(a) == sorted(b)
def find_nums(segs):
nums = {}
for seg in segs:
if len(seg) == 2:
nums[1] = seg
if len(seg) == 4:
nums[4] = seg
if len(seg) == 3:
nums[7] = seg
if len(seg) == 7:
nums[8] = seg
ltormid = minus(nums[4], nums[1])
top = minus(nums[7], nums[1])
bot = None
for seg in segs:
if len(seg) != 5:
continue
res = minus(minus(seg, nums[4]), top)
if len(res) == 1:
bot = res
for seg in segs:
if equal(seg, nums[4] + top + bot):
nums[9] = seg
bl = minus(nums[8], nums[9])
for seg in segs:
if len(seg) != 5:
continue
res = minus(seg, nums[1] + bot + top)
if len(res) == 1:
nums[3] = seg
zero_or_six = []
for seg in segs:
if len(seg) != 6 or seg == nums[9]:
continue
zero_or_six.append(seg)
rem1 = minus(zero_or_six[0], zero_or_six[1])
assert(len(rem1) == 1)
rem2 = minus(zero_or_six[1], zero_or_six[0])
assert(len(rem2) == 1)
if rem1 in nums[1]:
nums[0] = zero_or_six[0]
elif rem2 in nums[1]:
nums[0] = zero_or_six[1]
mid = minus(nums[8], nums[0])
lt = minus(ltormid, mid)
for seg in segs:
if len(seg) == 6 and seg != nums[0] and seg != nums[9]:
nums[6] = seg
for seg in segs:
if len(seg) != 5 or seg == nums[3]:
continue
if lt in seg:
nums[5] = seg
else:
nums[2] = seg
return nums
with open('08.input', 'r') as f:
res = 0
for line in f.readlines():
segs, code = line.split(' | ')
segs = segs.split()
nums = find_nums(segs)
codes = np.array(code.split())
for code in codes:
for num in [nums[1], nums[4], nums[7], nums[8]]:
if equal(code, num):
res += 1
# print(np.isin(code, [nums[1], nums[4], nums[7], nums[8]]))
# print(nums)
# print(len(nums))
print(res)