Solution to problem 10 part 2 in Python

This commit is contained in:
David Doblas Jiménez 2021-12-01 21:23:16 +01:00
parent 3081b12c03
commit 2efc195d5a

View File

@ -1,3 +1,5 @@
from functools import lru_cache
import numpy as np
# --- Day 10: Adapter Array ---
@ -120,11 +122,11 @@ import numpy as np
with open("files/P10.txt", "r") as f:
inputs = [int(num) for num in f.read().strip().split("\n")]
# Add zero (charging outlet) and max+3 (device own built-in adapter)
joltages = sorted(inputs + [0, max(inputs) + 3])
def part_1() -> None:
# Add zero (charging outlet) and max+3 (device own built-in adapter)
joltages = sorted(inputs + [0, max(inputs) + 3])
joltages_diffs = np.diff(joltages)
print(
@ -132,5 +134,94 @@ def part_1() -> None:
)
# --- Part Two ---
# To completely determine whether you have enough adapters, you'll need to
# figure out how many different ways they can be arranged. Every arrangement
# needs to connect the charging outlet to your device. The previous rules about
# when adapters can successfully connect still apply.
# The first example above (the one that starts with 16, 10, 15) supports the
# following arrangements:
# (0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22)
# (0), 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, (22)
# (0), 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, (22)
# (0), 1, 4, 5, 7, 10, 12, 15, 16, 19, (22)
# (0), 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, (22)
# (0), 1, 4, 6, 7, 10, 12, 15, 16, 19, (22)
# (0), 1, 4, 7, 10, 11, 12, 15, 16, 19, (22)
# (0), 1, 4, 7, 10, 12, 15, 16, 19, (22)
# (The charging outlet and your device's built-in adapter are shown in
# parentheses.) Given the adapters from the first example, the total number of
# arrangements that connect the charging outlet to your device is 8.
# The second example above (the one that starts with 28, 33, 18) has many
# arrangements. Here are a few:
# (0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
# 32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 48, 49, (52)
# (0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
# 32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 49, (52)
# (0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
# 32, 33, 34, 35, 38, 39, 42, 45, 46, 48, 49, (52)
# (0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
# 32, 33, 34, 35, 38, 39, 42, 45, 46, 49, (52)
# (0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
# 32, 33, 34, 35, 38, 39, 42, 45, 47, 48, 49, (52)
# (0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
# 46, 48, 49, (52)
# (0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
# 46, 49, (52)
# (0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
# 47, 48, 49, (52)
# (0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
# 47, 49, (52)
# (0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
# 48, 49, (52)
# In total, this set of adapters can connect the charging outlet to your device
# in 19208 distinct arrangements.
# You glance back down at your bag and try to remember why you brought so many
# adapters; there must be more than a trillion valid ways to arrange them!
# Surely, there must be an efficient way to count the arrangements.
# What is the total number of distinct ways you can arrange the adapters to
# connect the charging outlet to your device?
@lru_cache(maxsize=256)
def recursion(node: int) -> int:
if node == len(joltages) - 1:
return 1
# consider last three nodes (or the end of the list, whichever is first),
# and for any that are valid jumps (value is no more than three more),
# find the number of paths from that node to the end.
return sum(
[
recursion(current_node)
for current_node in range(node + 1, min(node + 4, len(joltages)))
if joltages[current_node] - joltages[node] <= 3
]
)
def part_2() -> None:
total_num = recursion(0)
print(f"The total number of distint ways is {total_num}")
if __name__ == "__main__":
part_1()
part_2()