Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: grids of . # separated by blank lines
- # lock: top row filled, bottom row empty
- # key: top row empty, bottom row filled
- # Output: how many key/lock pairs can be overlaid with no overlap in #'s?
- all_locks = []
- all_keys = []
- def process(lines):
- values = [-1] * 6 # -1 to compensate for base row, [5] = total height
- for line in lines:
- for index in range(5):
- if line[index] == "#":
- values[index] += 1
- values[5] = len(lines) - 2 # compensate for base row and blank row
- if lines[0] == "#####":
- all_locks.append(values)
- else:
- all_keys.append(values)
- def fit(l, k):
- for index in range(5):
- if l[index] + k[index] > l[5]:
- return False
- return True
- file = open("25_input.txt", "r")
- current_lines = []
- for line in file:
- line = line.replace("\n", "")
- if line != "":
- current_lines.append(line)
- continue
- process(current_lines)
- current_lines = []
- if len(current_lines) > 0:
- process(current_lines)
- total = 0
- for l in all_locks:
- for k in all_keys:
- if fit(l, k):
- total += 1
- print (total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement