Spaces:
Running
Running
File size: 2,030 Bytes
a4da721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
def parse_schematic(lines):
heights = []
max_height = len(lines)
# Find the width of the schematic
width = len(lines[0])
for col in range(width):
# For each column, count from top or bottom depending on type
if lines[0][col] == '#': # Lock (count from top)
height = 0
for row in range(max_height):
if lines[row][col] == '#':
height += 1
else:
break
heights.append(height)
else: # Key (count from bottom)
height = 0
for row in range(max_height-1, -1, -1):
if lines[row][col] == '#':
height += 1
else:
break
heights.append(height)
return heights
def are_compatible(lock, key):
if len(lock) != len(key):
return False
# Check if any column's combined height exceeds available space
for l, k in zip(lock, key):
if l + k > 6: # 7 rows (0-6), so max height is 6
return False
return True
def solve_part1(filename):
with open(filename, 'r') as f:
content = f.read().strip()
# Split into individual schematics
schematics = content.split('\n\n')
locks = []
keys = []
# Parse each schematic
for schematic in schematics:
lines = schematic.split('\n')
# If top row has #, it's a lock
if lines[0].count('#') > 0:
locks.append(parse_schematic(lines))
else:
keys.append(parse_schematic(lines))
# Count compatible pairs
compatible_pairs = 0
for lock in locks:
for key in keys:
if are_compatible(lock, key):
compatible_pairs += 1
return str(compatible_pairs)
def solve_part2(filename):
# Part 2 has no computation requirement
return "Merry Christmas!"
# Print solutions
print(solve_part1("./input.txt"))
print(solve_part2("./input.txt")) |