Advertisement
Guest User

Untitled

a guest
Dec 24th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. # Input: grids of . # separated by blank lines
  2. #        lock: top row filled, bottom row empty
  3. #        key: top row empty, bottom row filled
  4. # Output: how many key/lock pairs can be overlaid with no overlap in #'s?
  5.  
  6. all_locks = []
  7. all_keys = []
  8.  
  9. def process(lines):
  10.   values = [-1] * 6 # -1 to compensate for base row, [5] = total height
  11.   for line in lines:
  12.     for index in range(5):
  13.       if line[index] == "#":
  14.         values[index] += 1
  15.   values[5] = len(lines) - 2 # compensate for base row and blank row
  16.   if lines[0] == "#####":
  17.     all_locks.append(values)
  18.   else:
  19.     all_keys.append(values)
  20.  
  21. def fit(l, k):
  22.   for index in range(5):
  23.     if l[index] + k[index] > l[5]:
  24.       return False
  25.   return True
  26.  
  27. file = open("25_input.txt", "r")
  28. current_lines = []
  29. for line in file:
  30.   line = line.replace("\n", "")
  31.   if line != "":
  32.     current_lines.append(line)
  33.     continue
  34.   process(current_lines)
  35.   current_lines = []
  36. if len(current_lines) > 0:
  37.   process(current_lines)
  38.  
  39. total = 0
  40. for l in all_locks:
  41.   for k in all_keys:
  42.     if fit(l, k):
  43.       total += 1
  44. print (total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement