-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-01.py
72 lines (57 loc) · 1.62 KB
/
day-01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
import sys
# Get the day from the filename e.g. day-1.py
DAY = re.match(r"day\-(\d+)\.py", sys.argv[0]).groups()[0]
def part_1(path):
"""Part 1/Star 1"""
# Open the file
with open(f"{path}/day-{DAY}.txt", "r") as f:
values = [int(line.strip()) for line in f.readlines()]
# Complete the task
answer = 0
current = values[0]
for this_line in values[1:]:
if this_line > current:
answer += 1
current = this_line
# Print out the response
print(f"Task 1 Answer: {answer}")
def part_2(path):
"""Part 2/Star 2"""
# Open the file
with open(f"{path}/day-{DAY}.txt", "r") as f:
values = [int(line.strip()) for line in f.readlines()]
# Complete the task
answer = 0
current = sum(values[:3])
for i in range(1, len(values) - 2):
this_section = sum(values[i : i + 3])
if this_section > current:
answer += 1
current = this_section
# Print out the response
print(f"Task 2 Answer: {answer}")
if __name__ == "__main__":
"""
Run using e.g.
`python day-1.py -test`
`python day-1.py`
`python day-1.py -test -2`
`python day-1.py -2`
`python day-1.py -test -both`
`python day-1.py -both`
"""
# Identify the folder that the input is in
test = "-test" in sys.argv
if test:
path = "input-tests"
else:
path = "inputs"
# Identify which one to run - 1 is default
if "-2" in sys.argv:
part_2(path)
elif "-both" in sys.argv:
part_1(path)
part_2(path)
else:
part_1(path)