Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open('input8') as f:
- lines = f.read().splitlines()
- def convert(segments):
- temp=0
- if 'a' in segments:
- temp+=2**0
- if 'b' in segments:
- temp+=2**1
- if 'c' in segments:
- temp+=2**2
- if 'd' in segments:
- temp+=2**3
- if 'e' in segments:
- temp+=2**4
- if 'f' in segments:
- temp+=2**5
- if 'g' in segments:
- temp+=2**6
- return temp
- answer=0
- for line in lines:
- line = line.split()
- conv=[0,0,0,0,0,0,0,0,127,0]
- L=[]
- displays = list(map(lambda x: convert(x), line[11:15]))
- digits = list(map(lambda x: convert(x), line[0:10]))
- for d in digits:
- segcnt=bin(d).count("1")
- if segcnt==2:
- conv[1]=d
- elif segcnt==3:
- conv[7]=d
- elif segcnt==4:
- conv[4]=d
- elif segcnt!=7:
- L.append(d)
- for l in L:
- if (l&conv[7]==conv[7]) and (bin(l).count("1")==5):
- conv[3]=l
- L.remove(l)
- break
- for l in L:
- if (l&conv[3]==conv[3]) and (bin(l).count("1")==6):
- conv[9]=l
- L.remove(l)
- break
- for l in L:
- if (l&conv[7]==conv[7]) and (l&(~conv[9])!=0):
- conv[0]=l
- L.remove(l)
- break
- for l in L:
- if (l&(~conv[9])==0):
- conv[5]=l
- L.remove(l)
- break
- for l in L:
- if (bin(l).count("1")==6):
- conv[6]=l
- L.remove(l)
- break
- conv[2]=L[0]
- answer += 1000*conv.index(displays[0]) + 100*conv.index(displays[1]) + 10*conv.index(displays[2]) + conv.index(displays[3])
- print(answer)
Advertisement
Add Comment
Please, Sign In to add comment