Advertisement
idoveolexvweplpwsd

Untitled

Dec 16th, 2021
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import numpy as np
  2. import Inputs
  3.  
  4. def the_common(arr, col, least = False):
  5.     '''Returns the most common number (1 or 0) at the given
  6.    column. If least is true, returns the least common.'''
  7.     rows, _ = arr.shape
  8.     col_sum = arr.sum(axis=0)
  9.  
  10.     # More 0s than 1s
  11.     if col_sum[col] < rows/2:
  12.         if least: return 1
  13.         return 0
  14.  
  15.     # More 1s than 0s
  16.     elif col_sum[col] > rows/2:
  17.         if least: return 0
  18.         return 1
  19.        
  20.     # Equal ammount
  21.     else:
  22.         return -1
  23.  
  24. o2_code = Inputs.Day03()
  25. co2_code = Inputs.Day03()
  26.  
  27. rows, cols = o2_code.shape
  28.  
  29.  
  30. for col in range(cols):
  31.     most_com = the_common(o2_code, col)
  32.    
  33.     if most_com == -1:
  34.         o2_code = o2_code[np.where(o2_code[:,col] == 1)]
  35.    
  36.     else:
  37.         # This 'where' keeps the rows that, at the specified column
  38.         # have a the most common number
  39.         o2_code = o2_code[np.where(o2_code[:,col] == most_com)]
  40.        
  41.     # If only one row left, thats our answer
  42.     rows, cols = o2_code.shape
  43.     if rows == 1:
  44.         o2_code = o2_code.flatten()
  45.         break
  46.    
  47.  
  48. # Need to have two for loops because they may break at different moments
  49. for col in range(cols):
  50.     most_com = the_common(co2_code, col, True)
  51.     # False return
  52.     if most_com == -1:
  53.         co2_code = co2_code[np.where(co2_code[:,col] == 0)]
  54.    
  55.     else:
  56.         co2_code = co2_code[np.where(co2_code[:,col] == most_com)]
  57.        
  58.     rows, cols = co2_code.shape
  59.     if rows == 1:
  60.         co2_code = co2_code.flatten()
  61.         break
  62.    
  63. # Remove brackets, spaces and commas
  64. # and finally convert to base 10
  65. o2_code = np.array2string(o2_code, separator="")[1:-1]
  66. co2_code = np.array2string(co2_code, separator="")[1:-1]
  67.  
  68. print("o2_code in binary: {}".format(o2_code))
  69. print("co2_code in binary: {}".format(co2_code))
  70. print()
  71. print("o2_code in int: {}".format(int(o2_code, 2)))
  72. print("co2_code in int: {}".format(int(co2_code, 2)))
  73. print()
  74. print("Result: {}".format(int(o2_code, 2)*int(co2_code, 2)))
  75.  
  76. assert int(o2_code, 2)*int(co2_code, 2) == 5736383
  77.  
  78. '''
  79. 110111111111
  80. 011001000001
  81. o2_code in binary: 110111111111
  82. co2_code in binary: 011001000001
  83.  
  84. o2_code in int: 3583
  85. co2_code in int: 1601
  86.  
  87. Result: 5736383
  88. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement