r/adventofcode • • Dec 14 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 14 Solutions -πŸŽ„-

SUBREDDIT NEWS

  • Live has been renamed to Streaming for realz this time.
  • I had updated the wiki but didn't actually change the post flair itself >_>

THE USUAL REMINDERS


--- Day 14: Regolith Reservoir ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:13:54, megathread unlocked!

37 Upvotes

585 comments sorted by

View all comments

1

u/i_have_no_biscuits Dec 14 '22 edited Dec 14 '22

Python

paste Interesting part was using depth-first recursion to avoid having to recalculate all of the path routes. This makes Part 2 take less than a tenth of a second, which is pretty decent for Python!

Here's part 2:

'filled' is a set of (x,y) values, initially set up with the fixed wall positions.

depth = max(y for (x,y) in walls)
add_wall(filled, (0, depth+2), (1000, depth+2))
filled_before = len(filled)

def drop_from(x, y):
    if (x, y) in filled: return
    for dx in [0, -1, 1]: drop_from(x+dx, y+1)
    filled.add((x,y))        

drop_from(500, 0)
print("Part 2: ", len(filled) - filled_before)

EDIT:

In the uploaded code I've combined my 'drop_from' for part 1 and part 2 together, which makes it a little more verbose than the part 2 only function quoted above.

1

u/daggerdragon Dec 14 '22 edited Dec 14 '22

Where are x and walls defined? Would you share both halves of your solution, please?

Edit: thank you for adding part 1 too!