Guest User

Untitled

a guest
Dec 8th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. with open('input8') as f:
  2.     lines = f.read().splitlines()
  3.  
  4. def convert(segments):
  5.     temp=0
  6.     if 'a' in segments:
  7.         temp+=2**0
  8.     if 'b' in segments:
  9.         temp+=2**1
  10.     if 'c' in segments:
  11.         temp+=2**2
  12.     if 'd' in segments:
  13.         temp+=2**3
  14.     if 'e' in segments:
  15.         temp+=2**4
  16.     if 'f' in segments:
  17.         temp+=2**5
  18.     if 'g' in segments:
  19.         temp+=2**6
  20.     return temp
  21.  
  22. answer=0
  23. for line in lines:
  24.     line = line.split()
  25.     conv=[0,0,0,0,0,0,0,0,127,0]
  26.     L=[]
  27.     displays = list(map(lambda x: convert(x), line[11:15]))
  28.     digits = list(map(lambda x: convert(x), line[0:10]))
  29.  
  30.     for d in digits:
  31.         segcnt=bin(d).count("1")
  32.         if segcnt==2:
  33.             conv[1]=d
  34.         elif segcnt==3:
  35.             conv[7]=d
  36.         elif segcnt==4:
  37.             conv[4]=d
  38.         elif segcnt!=7:
  39.             L.append(d)
  40.    
  41.     for l in L:
  42.         if (l&conv[7]==conv[7]) and (bin(l).count("1")==5):
  43.             conv[3]=l
  44.             L.remove(l)
  45.             break
  46.      
  47.     for l in L:
  48.         if (l&conv[3]==conv[3]) and (bin(l).count("1")==6):
  49.             conv[9]=l
  50.             L.remove(l)
  51.             break
  52.  
  53.     for l in L:
  54.         if (l&conv[7]==conv[7]) and (l&(~conv[9])!=0):
  55.             conv[0]=l
  56.             L.remove(l)
  57.             break
  58.  
  59.     for l in L:
  60.         if (l&(~conv[9])==0):
  61.             conv[5]=l
  62.             L.remove(l)
  63.             break
  64.  
  65.     for l in L:
  66.         if (bin(l).count("1")==6):
  67.             conv[6]=l
  68.             L.remove(l)
  69.             break
  70.  
  71.     conv[2]=L[0]
  72.  
  73.     answer += 1000*conv.index(displays[0]) + 100*conv.index(displays[1]) + 10*conv.index(displays[2]) + conv.index(displays[3])
  74.  
  75. print(answer)
Advertisement
Add Comment
Please, Sign In to add comment