Solve 2017/01

This commit is contained in:
Joscha 2018-12-03 09:00:28 +00:00
parent 4532819424
commit 16d27d747b
2 changed files with 38 additions and 0 deletions

37
2017/01/solve.py Normal file
View file

@ -0,0 +1,37 @@
import sys
def load_line(filename):
with open(filename, "r") as f:
return list(map(int, f.read()[:-1]))
# PART 1
def sum_matching(digits):
offset = digits[1:] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
# PART 2
def sum_matching_2(digits):
offset = digits[len(digits)//2:] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
def main(filename):
digits = load_line(filename)
print(f"Solutions for {filename}")
total = sum_matching(digits)
print(f"Part 1: {total}")
total_2 = sum_matching_2(digits)
print(f"Part 2: {total_2}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)