mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
54
adventofcode/2020/day9/day9.py
Normal file
54
adventofcode/2020/day9/day9.py
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
import itertools
|
||||
|
||||
|
||||
def part1(lines):
|
||||
preamble_size = 25
|
||||
for nums in window(lines, preamble_size + 1):
|
||||
candidate = nums[-1]
|
||||
if not test_number(candidate, nums[:-1]):
|
||||
return candidate
|
||||
return -1
|
||||
|
||||
|
||||
def window(seq, n):
|
||||
it = iter(seq)
|
||||
result = tuple(itertools.islice(it, n))
|
||||
if len(result) == n:
|
||||
yield result
|
||||
for elem in it:
|
||||
result = result[1:] + (elem,)
|
||||
yield result
|
||||
|
||||
|
||||
def test_number(num, previous):
|
||||
sums = set(sum(x) for x in itertools.combinations(previous, 2))
|
||||
return num in sums
|
||||
|
||||
|
||||
def part2(lines, target: int):
|
||||
total = 0
|
||||
visited = []
|
||||
for index, _ in enumerate(lines):
|
||||
i = index
|
||||
while total < target:
|
||||
total += lines[i]
|
||||
visited.append(lines[i])
|
||||
i += 1
|
||||
if total == target:
|
||||
return max(visited) + min(visited)
|
||||
visited.clear()
|
||||
total = 0
|
||||
|
||||
|
||||
def main(f):
|
||||
lines = [int(l.rstrip()) for l in f]
|
||||
invalid_number = part1(lines)
|
||||
print("part1 ", invalid_number)
|
||||
print("part2 ", part2(lines, invalid_number))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import fileinput
|
||||
main(fileinput.input())
|
||||
|
Reference in New Issue
Block a user