diff --git a/12/input01.txt b/12/input01.txt new file mode 100644 index 0000000..ca1ab67 --- /dev/null +++ b/12/input01.txt @@ -0,0 +1,23 @@ +he-JK +wy-KY +pc-XC +vt-wy +LJ-vt +wy-end +wy-JK +end-LJ +start-he +JK-end +pc-wy +LJ-pc +at-pc +xf-XC +XC-he +pc-JK +vt-XC +at-he +pc-he +start-at +start-XC +at-LJ +vt-JK \ No newline at end of file diff --git a/12/input01_sample01.txt b/12/input01_sample01.txt new file mode 100644 index 0000000..898cd56 --- /dev/null +++ b/12/input01_sample01.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end \ No newline at end of file diff --git a/12/input01_sample02.txt b/12/input01_sample02.txt new file mode 100644 index 0000000..ef30b81 --- /dev/null +++ b/12/input01_sample02.txt @@ -0,0 +1,10 @@ +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc \ No newline at end of file diff --git a/12/input01_sample03.txt b/12/input01_sample03.txt new file mode 100644 index 0000000..da6e083 --- /dev/null +++ b/12/input01_sample03.txt @@ -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 \ No newline at end of file diff --git a/12/solve01.py b/12/solve01.py new file mode 100644 index 0000000..3f3c1c9 --- /dev/null +++ b/12/solve01.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import copy +graph = dict() + +def calculate_paths(graph, current, small_seen, path): + found = 0 + for n in graph.get(current): + if n == 'start': + continue + if n in small_seen: + continue + p = copy.copy(path) + p.append(n) + #print(p) + #print("%s -> %s" % (current, n)) + if n == 'end': + found += 1 + continue + sm = copy.copy(small_seen) + if n.lower() == n: + sm.append(n) + found += calculate_paths(graph, n, sm, p) + return found + +with open("input01.txt","r") as f: + for line in f: + (x,y) = line.strip().split("-",2) + if x not in graph: + graph[x] = [] + graph[x].append(y) + if y not in graph: + graph[y] = [] + graph[y].append(x) + +#print(graph) +print(calculate_paths(graph, 'start', [], ['start'])) diff --git a/12/solve02.py b/12/solve02.py new file mode 100644 index 0000000..39e4dc9 --- /dev/null +++ b/12/solve02.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import copy +from collections import defaultdict + +graph = dict() + +def calculate_paths(graph, current, small_seen, path): + if current == 'end': + return 1 + found = 0 + for n in sorted(graph.get(current)): + if n == 'start': + continue + # already seen, check if there is any with count + if small_seen[n] > 0: + tmp = max(small_seen.values()) + if tmp > 1: + continue + p = copy.copy(path) + p.append(n) + #print("%s -> %s" % (current, n)) + sm = copy.copy(small_seen) + if n.lower() == n: + sm[n] += 1 + found += calculate_paths(graph, n, sm, p) + return found + +with open("input01.txt","r") as f: + for line in f: + (x,y) = line.strip().split("-",2) + if x not in graph: + graph[x] = [] + graph[x].append(y) + if y not in graph: + graph[y] = [] + graph[y].append(x) + +#print(graph) +small_seen = defaultdict(int) +print(calculate_paths(graph, 'start', small_seen, ['start']))