-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp08b.py
104 lines (90 loc) · 2.44 KB
/
p08b.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
# https://adventofcode.com/2021/day/8 part 2
# Created by: Menaka S. 8 Dec 2021
import sys
from collections import defaultdict
total = 0
digits = []
def split(word):
return [char for char in word]
def present_in(word1, word2):
w1 = split(word1)
found = 1
for i in range(0,len(w1)):
if not w1[i] in word2:
found = 0
return found
for line in sys.stdin:
mapping = dict()
line = line.strip()
(lhs,rhs) = line.split(' | ')
#lhs
digits = lhs.split()
for i in range(len(digits)):
digits[i] = "".join(sorted([char for char in digits[i]]))
digits.sort(key=len)
#rhs
rdigits = rhs.split()
for i in range(len(rdigits)):
rdigits[i] = "".join(sorted([char for char in rdigits[i]]))
mapping[1] = digits[0] #length 2
mapping[7] = digits[1] #length 3
mapping[4] = digits[2] #length 4
mapping[8] = digits[9] #length 7
# 0 6 9
if not present_in(mapping[7],digits[6]):
mapping[6] = digits[6]
if present_in(mapping[4],digits[7]):
mapping[9] = digits[7]
mapping[0] = digits[8]
if present_in(mapping[4],digits[8]):
mapping[9] = digits[8]
mapping[0] = digits[7]
if not present_in(mapping[7],digits[7]):
mapping[6] = digits[7]
if present_in(mapping[4],digits[6]):
mapping[9] = digits[6]
mapping[0] = digits[8]
if present_in(mapping[4],digits[8]):
mapping[9] = digits[8]
mapping[0] = digits[6]
if not present_in(mapping[7],digits[8]):
mapping[6] = digits[8]
if present_in(mapping[4],digits[7]):
mapping[9] = digits[7]
mapping[0] = digits[6]
if present_in(mapping[4],digits[6]):
mapping[9] = digits[6]
mapping[0] = digits[7]
# 2 3 5
if present_in(mapping[1],digits[3]):
mapping[3] = digits[3]
if present_in(digits[4],mapping[9]):
mapping[5] = digits[4]
mapping[2] = digits[5]
if present_in(digits[5],mapping[9]):
mapping[5] = digits[5]
mapping[2] = digits[4]
if present_in(mapping[1],digits[4]):
mapping[3] = digits[4]
if present_in(digits[3],mapping[9]):
mapping[5] = digits[3]
mapping[2] = digits[5]
if present_in(digits[5],mapping[9]):
mapping[5] = digits[5]
mapping[2] = digits[3]
if present_in(mapping[1],digits[5]):
mapping[3] = digits[5]
if present_in(digits[4],mapping[9]):
mapping[5] = digits[4]
mapping[2] = digits[3]
if present_in(digits[3],mapping[9]):
mapping[5] = digits[3]
mapping[2] = digits[4]
inv_map = {v: k for k, v in mapping.items()}
num = 0
ct = 3
for d in rdigits:
num += int(inv_map[d]) * (10** ct)
ct -=1
total += num
print(total)