diff --git a/10/10.py b/10/10.py new file mode 100644 index 0000000..8433ab5 --- /dev/null +++ b/10/10.py @@ -0,0 +1,59 @@ +def read_file(): + with open("input.txt","r") as f: + return [int(x) for x in f.read().split('\n')] + +def part1(adapters): + jolts = [0] + sorted(adapters) # j(port) = 0 + d1 = 0 + d3 = 1 # j(device) = max + 3 + i = 0 + while i <= len(adapters): + difference = jolts[i] - jolts[i-1] + if difference == 1: + d1 += 1 + elif difference == 2: + d2 += 1 + elif difference == 3: + d3 += 1 + i += 1 + return d1 * d3 + +""" +def find_all_paths(graph, start, end, path=[]): + path = path + [start] + if start == end: + return [path] + if start not in graph: + return [] + paths = [] + for node in graph[start]: + if node not in path: + newpaths = find_all_paths(graph, node, end, path) + for newpath in newpaths: + paths.append(newpath) + return paths +""" + +def part2(adapters): + jolts = [0] + sorted(adapters) + from collections import Counter + dp = Counter() + dp[0] = 1 + jolts = sorted(adapters) + jolts.append(jolts[-1] + 3) + for j in jolts: + dp[j] = dp[j-1] + dp[j-2] + dp[j-3] + return dp[jolts[-1]] + """ + graph = {} + for i, joltage in enumerate(jolts): + graph[joltage] = [x for x in jolts[i+1:i+4] if 0 < x - joltage <= 3] # scan next 3, assuming diff of +1 +1 +1 at most + return len( find_all_paths( graph, 0, max(jolts) ) ) + """ + +def main(): + adapters = read_file() + print(part1(adapters), part2(adapters)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/10/input.txt b/10/input.txt new file mode 100644 index 0000000..03d17ee --- /dev/null +++ b/10/input.txt @@ -0,0 +1,98 @@ +35 +111 +135 +32 +150 +5 +106 +154 +41 +7 +27 +117 +109 +63 +64 +21 +138 +98 +40 +71 +144 +13 +66 +48 +12 +55 +119 +103 +54 +78 +65 +112 +39 +128 +53 +140 +77 +34 +28 +81 +151 +125 +85 +124 +2 +99 +131 +59 +60 +6 +94 +33 +42 +93 +14 +141 +92 +38 +104 +9 +29 +100 +52 +19 +147 +49 +74 +70 +84 +113 +120 +91 +97 +17 +45 +139 +90 +116 +149 +129 +87 +69 +20 +24 +148 +18 +58 +123 +76 +118 +130 +132 +75 +110 +105 +1 +8 +86 \ No newline at end of file diff --git a/10/input_sample.txt b/10/input_sample.txt new file mode 100644 index 0000000..cd1b40b --- /dev/null +++ b/10/input_sample.txt @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 \ No newline at end of file