adventofcode/2020/10/main.py

46 lines
861 B
Python
Raw Normal View History

2020-12-10 17:29:23 +00:00
import copy
2020-12-10 20:11:23 +00:00
from collections import Counter
2020-12-10 17:29:23 +00:00
def load(filename):
with open(filename, "r", encoding="utf-8") as f:
2020-12-10 20:11:23 +00:00
r = sorted([int(x) for x in f])
r.insert(0, 0)
r.append(r[-1] + 3)
return r
2020-12-10 17:29:23 +00:00
def calc_diffs(adapters):
2020-12-10 20:11:23 +00:00
c = Counter()
for i in range(len(adapters) - 1):
d = adapters[i+1] - adapters[i]
c[d] += 1
return c
2020-12-10 17:29:23 +00:00
2020-12-10 20:11:23 +00:00
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]]
2020-12-10 17:29:23 +00:00
adapters = load("test1.txt")
2020-12-10 20:11:23 +00:00
c = calc_diffs(adapters)
assert (c[1], c[3]) == (7, 5)
assert calc_total(adapters) == 8
2020-12-10 17:29:23 +00:00
adapters = load("test2.txt")
2020-12-10 20:11:23 +00:00
c = calc_diffs(adapters)
assert (c[1], c[3]) == (22, 10)
assert calc_total(adapters) == 19208
2020-12-10 17:29:23 +00:00
adapters = load("data.txt")
2020-12-10 20:11:23 +00:00
c = calc_diffs(adapters)
print(f"1: Answer is {c[1] * c[3]}")
2020-12-10 17:29:23 +00:00
2020-12-10 20:11:23 +00:00
c = calc_total(adapters)
print(f"2: Answer is {c}")