diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-10 09:19:09 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-10 09:21:37 +0100 |
| commit | 9f1080e091423531fab21ec0224368622580e92a (patch) | |
| tree | 8131343e8f79d2e19e4cd55bd2c4b66af7dc51f4 /20/py | |
| parent | 84dc757be6390492a5706756e45a17ab0a9d2b1d (diff) | |
| download | aoc-9f1080e091423531fab21ec0224368622580e92a.tar.gz | |
day 10
Diffstat (limited to '20/py')
| -rw-r--r-- | 20/py/d10.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/20/py/d10.py b/20/py/d10.py new file mode 100644 index 0000000..2b3bc2e --- /dev/null +++ b/20/py/d10.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import aoc20 +import functools +import sys + + +def pt1(_in): + jolts = [int(n.strip()) for n in _in] + jolts.append(max(jolts)+3) + jolts = sorted(jolts) + jolt = 0 + diffs = [0, 0, 0] + for j in jolts: + diffs[j-jolt-1] += 1 + jolt = j + return diffs[0] * diffs[2] + + +def pt2(_in): + jolts = [int(n.strip()) for n in _in] + jolts.append(max(jolts)+3) + jolts = sorted(jolts) + jolts.insert(0, 0) + jolt = 0 + reach = {} + for i in range(len(jolts)): + reachable = [] + j = i+1 + while j < len(jolts) and jolts[j] - jolts[i] <= 3: + reachable.append(jolts[j]) + j += 1 + reach[jolts[i]] = tuple(reachable) + + @functools.cache + def ways(start, dest): + if start == dest: + return 1 + return sum(ways(child, dest) for child in reach[start]) + + return ways(jolts[0], jolts[-1]) + + +if __name__ == "__main__": + input = aoc20.read_input(sys.argv[1:], 10) + print(pt1(input)) + print(pt2(input)) |
