-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |