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
|
import copy
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
|
||||||
def load(filename):
|
def load(filename):
|
||||||
with open(filename, "r", encoding="utf-8") as f:
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
return [int(x) for x in f]
|
r = sorted([int(x) for x in f])
|
||||||
|
r.insert(0, 0)
|
||||||
|
r.append(r[-1] + 3)
|
||||||
def calc_builtin(adapters):
|
return r
|
||||||
return max(adapters) + 3
|
|
||||||
|
|
||||||
|
|
||||||
def calc_diffs(adapters):
|
def calc_diffs(adapters):
|
||||||
wall = 0
|
c = Counter()
|
||||||
builtin = calc_builtin(adapters)
|
for i in range(len(adapters) - 1):
|
||||||
adapters = sorted(adapters)
|
d = adapters[i+1] - adapters[i]
|
||||||
adapters.insert(0, 0)
|
c[d] += 1
|
||||||
adapters.append(builtin)
|
return c
|
||||||
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):
|
def calc_total(adapters):
|
||||||
return abs(a - b) <= 3
|
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")
|
adapters = load("test1.txt")
|
||||||
d1, _, d3 = calc_diffs(adapters)
|
c = calc_diffs(adapters)
|
||||||
assert d1 == 7
|
assert (c[1], c[3]) == (7, 5)
|
||||||
assert d3 == 5
|
assert calc_total(adapters) == 8
|
||||||
|
|
||||||
adapters = load("test2.txt")
|
adapters = load("test2.txt")
|
||||||
d1, d2, d3 = calc_diffs(adapters)
|
c = calc_diffs(adapters)
|
||||||
assert d1 == 22
|
assert (c[1], c[3]) == (22, 10)
|
||||||
assert d3 == 10
|
assert calc_total(adapters) == 19208
|
||||||
print(d1, d2, d3)
|
|
||||||
|
|
||||||
adapters = load("data.txt")
|
adapters = load("data.txt")
|
||||||
d1, _, d3 = calc_diffs(adapters)
|
c = calc_diffs(adapters)
|
||||||
print(f"1: Answer is {d1 * d3}")
|
print(f"1: Answer is {c[1] * c[3]}")
|
||||||
|
|
||||||
##############################################################################
|
c = calc_total(adapters)
|
||||||
|
print(f"2: Answer is {c}")
|
||||||
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.")
|
|
||||||
|
|
Loading…
Reference in a new issue