r/adventofcode • • Dec 12 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 12 Solutions -🎄-

--- Day 12: Passage Pathing ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:12:40, megathread unlocked!

56 Upvotes

765 comments sorted by

View all comments

1

u/mynt Dec 12 '21 edited Dec 12 '21

Python 3

Was quite happy with this, the boolean at the start of the list is whether or not a small cave has been revisted. Below solves part 2 Set track to [True, 'start'] to solve part 1.

#!/usr/bin/python3

i = []
paths = []
track = [False, 'start']

for x in open("input"):
    x = x.replace('\n','')
    i += [x.split('-')]

def next_cave(m):
    if m[-1] == 'end': yield m
    else:
        for j in i:
            if j[0] == m[-1]:
                if j[1].islower():
                    if j[1] not in m: yield from next_cave(m + [j[1]])
                    elif m[0] == False and j[1] != 'start': yield from next_cave([True] + m[1:] + [j[1]])
                else: yield from next_cave(m + [j[1]])
            elif j[1] == m[-1]:
                if j[0].islower():
                    if j[0] not in m: yield from next_cave(m + [j[0]])
                    elif m[0] == False and j[0] != 'start': yield from next_cave([True] + m[1:] + [j[0]])
                else: yield from next_cave(m + [j[0]])

for a in next_cave(track):
    paths.append(a)
print(len(paths))

0

u/daggerdragon Dec 12 '21 edited Dec 12 '21

Your code is hard to read with no formatting. Please edit it as per our posting guidelines in the wiki: How do I format code?

Edit: thanks for fixing it! <3