Day 10 - Take two
This commit is contained in:
parent
491dc3c5fa
commit
9aa8450af7
1 changed files with 27 additions and 102 deletions
129
2020/10/main.py
129
2020/10/main.py
|
@ -1,120 +1,45 @@
|
|||
import copy
|
||||
from collections import Counter
|
||||
|
||||
|
||||
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
|
||||
r = sorted([int(x) for x in f])
|
||||
r.insert(0, 0)
|
||||
r.append(r[-1] + 3)
|
||||
return r
|
||||
|
||||
|
||||
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
|
||||
c = Counter()
|
||||
for i in range(len(adapters) - 1):
|
||||
d = adapters[i+1] - adapters[i]
|
||||
c[d] += 1
|
||||
return c
|
||||
|
||||
|
||||
def is_compatible(a, b):
|
||||
return abs(a - b) <= 3
|
||||
def calc_total(adapters):
|
||||
c = Counter()
|
||||
c[0] = 1
|
||||
for a in adapters:
|
||||
for i in range(1, 4):
|
||||
c[a] += c[a-i]
|
||||
return c[adapters[-1]]
|
||||
|
||||
|
||||
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
|
||||
c = calc_diffs(adapters)
|
||||
assert (c[1], c[3]) == (7, 5)
|
||||
assert calc_total(adapters) == 8
|
||||
|
||||
adapters = load("test2.txt")
|
||||
d1, d2, d3 = calc_diffs(adapters)
|
||||
assert d1 == 22
|
||||
assert d3 == 10
|
||||
print(d1, d2, d3)
|
||||
c = calc_diffs(adapters)
|
||||
assert (c[1], c[3]) == (22, 10)
|
||||
assert calc_total(adapters) == 19208
|
||||
|
||||
adapters = load("data.txt")
|
||||
d1, _, d3 = calc_diffs(adapters)
|
||||
print(f"1: Answer is {d1 * d3}")
|
||||
c = calc_diffs(adapters)
|
||||
print(f"1: Answer is {c[1] * c[3]}")
|
||||
|
||||
##############################################################################
|
||||
|
||||
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.")
|
||||
c = calc_total(adapters)
|
||||
print(f"2: Answer is {c}")
|
||||
|
|
Loading…
Reference in a new issue