mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 00:11:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
33
adventofcode/2023/day9/day9.py
Normal file
33
adventofcode/2023/day9/day9.py
Normal file
@@ -0,0 +1,33 @@
|
||||
def parse_input(infile):
|
||||
with open(infile) as f:
|
||||
return [[int(x) for x in l.strip().split()] for l in f.readlines()]
|
||||
|
||||
|
||||
def process_line(line):
|
||||
if set(line) == {0}:
|
||||
return 0
|
||||
else:
|
||||
next_line = [cur - next for next, cur in zip(line, line[1:])]
|
||||
return line[-1] + process_line(next_line)
|
||||
|
||||
|
||||
def process_line_back(line):
|
||||
if set(line) == {0}:
|
||||
return 0
|
||||
else:
|
||||
next_line = [cur - next for next, cur in zip(line, line[1:])]
|
||||
return line[0] - process_line_back(next_line)
|
||||
|
||||
|
||||
def solve(data):
|
||||
print(f"Part 1: {sum(process_line(l) for l in data)}")
|
||||
print(f"Part 2: {sum(process_line_back(l) for l in data)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
import os
|
||||
SCRIPTPATH = os.path.dirname(os.path.realpath(__file__))
|
||||
infile = sys.argv[1] if len(sys.argv) == 2 else "example.txt"
|
||||
data = parse_input(os.path.join(SCRIPTPATH, infile))
|
||||
solve(data)
|
Reference in New Issue
Block a user