Last active
December 14, 2022 14:30
-
-
Save CapacitorSet/8904ad75a5510cd2ae9331f4fa87aa9d to your computer and use it in GitHub Desktop.
Proof of concept - Advent of Code 2022, day 14 part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
solids = set() | |
solids_y = dict() | |
part1 = False | |
def parse_coord(coord): | |
x, y = coord.split(",") | |
return int(x), int(y) | |
with open("input") as file: | |
for line in file: | |
coords = [parse_coord(c) for c in line.rstrip().split(" -> ")] | |
for i in range(len(coords) - 1): | |
pair = sorted(coords[i:i+2]) | |
for x in range(pair[0][0], pair[1][0]+1): | |
for y in range(pair[0][1], pair[1][1]+1): | |
solids.add((x, y)) | |
if y in solids_y: | |
solids_y[y].append(x) | |
else: | |
solids_y[y] = [x] | |
grid_height = max([pos[1] for pos in solids]) | |
if not part1: | |
grid_height += 2 | |
row = 0 | |
old_row = set([500]) | |
ret = 1 | |
while row < grid_height - 1: | |
row += 1 | |
new_row = set(old_row) | |
for item in old_row: | |
new_row.add(item-1) | |
new_row.add(item+1) | |
if row in solids_y: | |
for solid in solids_y[row]: | |
if solid in new_row: | |
new_row.remove(solid) | |
ret += len(new_row) | |
old_row = new_row | |
print(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment