Skip to content

Instantly share code, notes, and snippets.

@CapacitorSet
Last active December 14, 2022 14:30
Show Gist options
  • Save CapacitorSet/8904ad75a5510cd2ae9331f4fa87aa9d to your computer and use it in GitHub Desktop.
Save CapacitorSet/8904ad75a5510cd2ae9331f4fa87aa9d to your computer and use it in GitHub Desktop.

Revisions

  1. CapacitorSet revised this gist Dec 14, 2022. No changes.
  2. CapacitorSet created this gist Dec 14, 2022.
    42 changes: 42 additions & 0 deletions aoc_d14.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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)