From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2021/day12/12a.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2021/day12/12a.py (limited to '2021/day12/12a.py') diff --git a/2021/day12/12a.py b/2021/day12/12a.py new file mode 100644 index 0000000..5d1adba --- /dev/null +++ b/2021/day12/12a.py @@ -0,0 +1,34 @@ +from collections import deque + +def parseInput(contents): + edges = [l.split("-") for l in contents.strip().split("\n")] + edges_set = set([]) + for (f, t) in edges: + edges_set.add((f, t)) + if t != "end" and f != "start": + edges_set.add((t, f)) + + return edges_set + +def visitableNeighbours(edges, loc): + fr = loc[0] + banned = [e for e in loc if e.islower()] + for (f, t) in edges: + if f == fr and t not in banned: + yield t + + +edges = parseInput(open("./input").read()) + +locs = deque() # each element is a list of all the visited nodes +finished = set() +locs.append(["start"]) +while len(locs) > 0: + l = locs.pop() + if l[0] == "end": + finished.add("-".join(l)) + else: + for neighbour in visitableNeighbours(edges, l): + locs.append([neighbour] + l) + +print(len(finished)) -- cgit v1.2.3