Skip to content

Commit

Permalink
day12
Browse files Browse the repository at this point in the history
  • Loading branch information
Zehvogel committed Dec 12, 2021
1 parent aba466a commit ab8817b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
22 changes: 22 additions & 0 deletions day12/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
um-end
pk-um
FE-il
ay-FE
pk-start
end-jt
um-FE
RO-il
xc-ay
il-end
start-EZ
pk-FE
xc-start
jt-FE
EZ-um
pk-xc
xc-EZ
pk-ay
il-ay
jt-EZ
jt-om
pk-EZ
57 changes: 57 additions & 0 deletions day12/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import time

def prepare_graph(input):
adjacencies = {}
lines = (l.strip() for l in input)
for l in lines:
a, b = l.split("-")
if a in adjacencies.keys():
adjacencies[a].append(b)
else:
adjacencies[a] = [b]
if b in adjacencies.keys():
adjacencies[b].append(a)
else:
adjacencies[b] = [a]
return adjacencies

def visit(node, visited, adj, twice):
paths = []
visited.append(node)
if node == "end":
paths.append(visited)
else:
for n in adj[node]:
if n.islower() and n in visited:
if n != "start" and not twice:
paths += visit(n, visited.copy(), adj, True)
else:
continue
else:
paths += visit(n, visited.copy(), adj, twice)
return paths


def part1(input):
adj = prepare_graph(input)
paths = visit("start", [], adj, True)
return len(paths)

def part2(input):
adj = prepare_graph(input)
paths = visit("start", [], adj, False)
return len(paths)

def main():
with open("day12/input.txt") as f:
input = f.readlines()
t0 = time.perf_counter_ns()
print(f"solution for part 1 is: {part1(input)}")
t1 = time.perf_counter_ns()
print(f"part 1 took {t1-t0} ns")
print(f"solution for part 2 is: {part2(input)}")
t2 = time.perf_counter_ns()
print(f"part 2 took {t2-t1} ns")

if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions day12/test_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end
10 changes: 10 additions & 0 deletions day12/test_input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc
18 changes: 18 additions & 0 deletions day12/test_input3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

0 comments on commit ab8817b

Please sign in to comment.