This commit is contained in:
Søren Rasmussen 2020-12-09 08:25:26 +01:00
parent 5960b94a7a
commit 0355f55a25
3 changed files with 1074 additions and 0 deletions

1000
2020/9/data.txt Normal file

File diff suppressed because it is too large Load diff

54
2020/9/main.py Normal file
View file

@ -0,0 +1,54 @@
def preamble_generator(dataset):
preamble_length = len(dataset)
for i in range(0, preamble_length):
for j in range(i + 1, preamble_length):
yield dataset[i] + dataset[j]
def is_contained_in(preamble_dataset, value):
for x in preamble_generator(preamble_dataset):
if value == x:
return True
return False
def get_first_noncompliant_no(dataset, preamble_length):
for i in range(preamble_length, len(dataset)):
preamble = dataset[i - preamble_length:i]
value = dataset[i]
if not is_contained_in(preamble, value):
return value
assert False, "Wtf?"
def get_range(nc, dataset):
l = len(dataset)
for i in range(0, l):
for j in range(i, l):
ds = dataset[i:j]
s = sum(ds)
if s > nc:
break
if nc == s:
return ds
assert False, "Wtf?"
def process(filename, preamble_length):
with open(filename, "r", encoding="utf-8") as f:
nums = [int(x) for x in f]
nc = get_first_noncompliant_no(nums, preamble_length)
print(f"1: First non-compliant no. in {filename} is {nc}.")
r = get_range(nc, nums)
a = min(r) + max(r)
print(f"2: Answer is {a}.")
process("test.txt", 5)
print("-"*80)
process("data.txt", 25)

20
2020/9/test.txt Normal file
View file

@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576