Advertisement
Ihmemies

AOC 2021 8B

Nov 11th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.47 KB | Source Code | 0 0
  1. class Display:
  2.    
  3.     def __init__(self, filename:str):
  4.         """
  5.        param : TODO
  6.        return: none
  7.        """
  8.         self.data:list = []
  9.         self.read_file(filename)
  10.        
  11.        
  12.     def read_file(self, filename:str) -> None:
  13.         """
  14.        param : str, filename to read
  15.        return: None
  16.        """
  17.        
  18.         try:
  19.             with open (filename, "r") as read_file:
  20.                 self.data: list = read_file.read().splitlines()
  21.             read_file.close()
  22.         except OSError:
  23.             print("Bad file name!")
  24.             exit()
  25.            
  26.     def count_easy(self) -> int:
  27.         """
  28.        param : TODO
  29.        return: none
  30.        """
  31.         count:int = 0
  32.        
  33.         for row in self.data:
  34.             parts = [x.strip().split(" ") for x in row.split('|')]
  35.             #print(parts[1])
  36.             for str in parts[1]:
  37.                 if len(str) in (2,4,3,7):
  38.                     count += 1
  39.                    
  40.         return count
  41.    
  42.     def count_hard(self) -> int:
  43.         """      
  44.        
  45.        1 = be
  46.        8 = abcdefg
  47.        4 = bceg
  48.  
  49.        0/6/9 = bcdefg 9?
  50.        0/6/9 = acdefg 6?
  51.        0/6/9 = abdefg 0? (koska c puuttuu)
  52.  
  53.        2/3/5 = cdefg 5?
  54.        2/3/5 = bcdef 3?
  55.        2/3/5 = abcdf 2?
  56.  
  57.        7 = bde
  58.        ***************
  59.        8 = abcdefg
  60.        2/3/5 = bcdef
  61.        0/6/9 = bcdefg
  62.        4 = bceg
  63.  
  64.         dddd
  65.         g    b
  66.         g    b
  67.          cccc  
  68.         a    e
  69.         a    e
  70.         ffff  
  71.        return: none
  72.        """
  73.         count:int = 0
  74.                        
  75.         for row in self.data:
  76.             parts = [x.strip().split(" ") for x in row.split('|')]    
  77.             keys:dict = self.solve_keys(parts[0] + parts[1])
  78.            
  79.             num: str = ""              
  80.             for decode in parts[1]:
  81.                 decode = "".join([x for x in sorted(decode)])
  82.                 # this is so not optimal..
  83.                 # should have key:value in flipped order
  84.                 for key, val in keys.items():
  85.                     if decode == val:
  86.                         num += str(key)  
  87.            
  88.             count += int(num) # ..... really            
  89.        
  90.         return count
  91.    
  92.     def solve_keys(self, row:list) -> dict:
  93.         """
  94.        :param list row: _description_
  95.        :return dict: _description_
  96.        """
  97.         keys:dict = {}
  98.         set_069 = set()
  99.         set_235 = set()            
  100.            
  101.         # sort string letters
  102.         for str in row:
  103.             str = "".join([x for x in sorted(str)])
  104.            
  105.             if len(str) == 2:
  106.                 keys[1] = str
  107.             elif len(str) == 4:
  108.                 keys[4] = str
  109.             elif len(str) == 3:
  110.                 keys[7] = str
  111.             elif len(str) == 7:
  112.                 keys[8] = str
  113.             elif len(str) == 6:  
  114.                 set_069.add(str)
  115.             elif len(str) == 5:
  116.                 set_235.add(str)
  117.             else:
  118.                 print("!?!?!")
  119.        
  120.         #print("SETS:", set_069, set_235)
  121.  
  122.         one_ch = {}
  123.         for ch in keys[1]:
  124.             one_ch[ch] = 1
  125.            
  126.         five_ch = {}
  127.         for val in set_235:
  128.             for ch in val:
  129.                 if ch in five_ch:
  130.                     five_ch[ch] += 1
  131.                 else:
  132.                     five_ch.update({ch: 1})
  133.                    
  134.         six_ch = {}
  135.         for val in set_069:        
  136.             for ch in val:            
  137.                 if ch in six_ch:
  138.                     six_ch[ch] += 1
  139.                 else:
  140.                     six_ch.update({ch: 1})  
  141.        
  142.         # figure out value 5
  143.         for key, val in five_ch.items():              
  144.             if val == 1 and key in six_ch:            
  145.                 if six_ch[key] == 3:                
  146.                     for s in set_235:
  147.                         if s.find(key) >= 0:                      
  148.                             keys[5] = s # this is no 5 str                            
  149.         set_235.remove(keys[5])    
  150.                    
  151.         # figure out value 3
  152.         for val in set_235:
  153.             hits = 0
  154.             for ch in val:
  155.                 if ch in one_ch:
  156.                     hits += 1
  157.             if hits == 2:
  158.                 keys[3] = val # this is 3          
  159.        
  160.         # 2 is the remaining one
  161.         set_235.remove(keys[3])
  162.         keys[2] = set_235.pop() # pop() returns last item from set
  163.  
  164.         # find 6 first by looking which one is missing other piece from 1
  165.         for val in set_069:
  166.             hits = 0
  167.             for ch in val:
  168.                 if ch in one_ch:
  169.                     hits += 1
  170.             if hits == 1:
  171.                 keys[6] = val # this is 6  
  172.                
  173.         set_069.remove(keys[6])
  174.        
  175.         # find 9 by comparing it to 3
  176.         for val in set_069:
  177.             diff = set(val).difference(set(keys[3]))
  178.             if len(diff) == 1:
  179.                 keys[9] = val  
  180.        
  181.         # 0 is last        
  182.         set_069.remove(keys[9])      
  183.         keys[0] = set_069.pop()    
  184.  
  185.         return keys
  186.        
  187.  
  188. def main():
  189.     d = Display("task.input")
  190.     total = d.count_hard()
  191.     """
  192.    if total != 26:
  193.        print(f"wrong amount! ({total})")
  194.    else:
  195.    """
  196.     #print("nums:", total)
  197.  
  198. import timeit    
  199. time = timeit.timeit(main, number=1000)
  200. print(f"{time*1000:.5f}ms")
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement