2020-12-10 - Adapter Array
(original .ipynb)
Day 10 is a sequence of integers (mine is here) which represent some output "joltage" of a given adapter, which it can successfully produce if it receives an input "joltage" of at most 3 less than the output. Part 1 involves sorting the joltage of each adapter, finding the differences between each, collecting the occurence of each difference and multiplying the number of those with difference 1 with those of difference 3. Part 2 involves finding the number of valid combinations of each adapter, such that we can be taken from the beginning joltage of 0 to the final output one.
from collections import defaultdict
puzzle_input_str = open("puzzle_input/day10.txt").read()
test_input_str = """16
10
15
5
1
11
7
19
6
12
4"""
large_test_input_str = """28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3"""
def parse_adapter_chain(input_str):
input_values = [int(l) for l in input_str.split("\n")]
return [n for n in sorted(input_values)]
def part1(input_str):
adapter_chain = parse_adapter_chain(input_str)
current_joltage = 0
joltage_differences = defaultdict(lambda: 0)
for adapter in adapter_chain:
diff = adapter - current_joltage
joltage_differences[diff] += 1
current_joltage = adapter
joltage_differences[3] += 1
return joltage_differences[1] * joltage_differences[3]
assert 35 == part1(test_input_str)
assert 220 == part1(large_test_input_str)
part1(puzzle_input_str)
2482
def part2(input_str):
adapter_chain = [0] + parse_adapter_chain(input_str)
rev_adapter_chain = list(reversed(adapter_chain))
lookup = defaultdict(lambda: 1)
for i, adapter in enumerate(rev_adapter_chain):
next_i = i - 1
can_connect = []
while next_i >= 0:
next_adapter = rev_adapter_chain[next_i]
if next_adapter - adapter > 3:
break
can_connect.append(next_adapter)
next_i -= 1
if(len(can_connect) > 0):
lookup[adapter] = sum(lookup[a] for a in can_connect)
return lookup[0]
assert 8 == part2(test_input_str)
assert 19208 == part2(large_test_input_str)
part2(puzzle_input_str)
96717311574016