121 lines
3.7 KiB
Python
121 lines
3.7 KiB
Python
|
import copy
|
||
|
|
||
|
|
||
|
def load(filename):
|
||
|
with open(filename, "r", encoding="utf-8") as f:
|
||
|
return [int(x) for x in f]
|
||
|
|
||
|
|
||
|
def calc_builtin(adapters):
|
||
|
return max(adapters) + 3
|
||
|
|
||
|
|
||
|
def calc_diffs(adapters):
|
||
|
wall = 0
|
||
|
builtin = calc_builtin(adapters)
|
||
|
adapters = sorted(adapters)
|
||
|
adapters.insert(0, 0)
|
||
|
adapters.append(builtin)
|
||
|
adapters += [wall, builtin]
|
||
|
|
||
|
d1 = 0
|
||
|
d2 = 0
|
||
|
d3 = 0
|
||
|
sa = sorted(adapters)
|
||
|
for x in range(0, len(sa) - 1):
|
||
|
a = sa[x]
|
||
|
b = sa[x+1]
|
||
|
d = b - a
|
||
|
if d == 1:
|
||
|
d1 += 1
|
||
|
elif d == 2:
|
||
|
d2 += 1
|
||
|
elif d == 3:
|
||
|
d3 += 1
|
||
|
|
||
|
return d1, d2, d3
|
||
|
|
||
|
|
||
|
def is_compatible(a, b):
|
||
|
return abs(a - b) <= 3
|
||
|
|
||
|
|
||
|
def calc_arrangements(adapters, wall, builtin):
|
||
|
return _calc(sorted(adapters), [wall], builtin)
|
||
|
|
||
|
def _calc(adapters, arranged, builtin):
|
||
|
a = arranged[-1]
|
||
|
b = adapters.pop(0)
|
||
|
while is_compatible(a, b):
|
||
|
if not adapters:
|
||
|
if is_compatible(b, builtin):
|
||
|
yield arranged + [b, builtin]
|
||
|
return
|
||
|
|
||
|
cadapters = copy.deepcopy(adapters)
|
||
|
carranged = copy.deepcopy(arranged)
|
||
|
carranged.append(b)
|
||
|
yield from _calc(cadapters, carranged, builtin)
|
||
|
|
||
|
b = adapters.pop(0)
|
||
|
|
||
|
|
||
|
assert calc_builtin([3, 6, 9]) == 12
|
||
|
|
||
|
adapters = load("test1.txt")
|
||
|
d1, _, d3 = calc_diffs(adapters)
|
||
|
assert d1 == 7
|
||
|
assert d3 == 5
|
||
|
|
||
|
adapters = load("test2.txt")
|
||
|
d1, d2, d3 = calc_diffs(adapters)
|
||
|
assert d1 == 22
|
||
|
assert d3 == 10
|
||
|
print(d1, d2, d3)
|
||
|
|
||
|
adapters = load("data.txt")
|
||
|
d1, _, d3 = calc_diffs(adapters)
|
||
|
print(f"1: Answer is {d1 * d3}")
|
||
|
|
||
|
##############################################################################
|
||
|
|
||
|
assert d1*d3 == 2450, "Task 1"
|
||
|
|
||
|
adapters = load("test1.txt")
|
||
|
bi = calc_builtin(adapters)
|
||
|
arrangements = list(calc_arrangements(adapters, 0, bi))
|
||
|
assert [0, 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 5, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 6, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||
|
assert [0, 1, 4, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||
|
|
||
|
adapters = load("test2.txt")
|
||
|
bi = calc_builtin(adapters)
|
||
|
arrangements = list(calc_arrangements(adapters, 0, bi))
|
||
|
assert [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] in arrangements
|
||
|
assert [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] in arrangements
|
||
|
assert [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] in arrangements
|
||
|
assert [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] in arrangements
|
||
|
assert [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] in arrangements
|
||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 46, 48, 49, 52] in arrangements
|
||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 46, 49, 52] in arrangements
|
||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 47, 48, 49, 52] in arrangements
|
||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 47, 49, 52] in arrangements
|
||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 48, 49, 52] in arrangements
|
||
|
assert len(arrangements) == 19208
|
||
|
|
||
|
#adapters = load("data.txt")
|
||
|
#bi = calc_builtin(adapters)
|
||
|
#
|
||
|
#arrangements = calc_arrangements(adapters, 0, bi)
|
||
|
#c = 0
|
||
|
#for x in arrangements:
|
||
|
# print(x)
|
||
|
# c += 1
|
||
|
#
|
||
|
#print(f"2:Found {c} ways to connect.")
|