-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
40 lines (29 loc) · 848 Bytes
/
day1.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
from advent import elf
"""
Prefix Sums FTW!
"""
def main():
test_lines = elf.read_lines(__file__, parser=elf.safe_atoi, test=True)
print("Part 1 (test):", part1(test_lines))
lines = elf.read_lines(__file__, parser=elf.safe_atoi)
print("Part 1:", part1(lines))
print("Part 2 (test):", part2(test_lines))
print("Part 2:", part2(lines))
def part1(lines):
count = 0
for i, e in enumerate(lines):
if i == 0:
continue
if e > lines[i - 1]:
count += 1
return count
def part2(lines):
# Using prefix sums to make the sliding sum operations a single operation
p = elf.prefix_sums(lines)
count = 0
for i in range(4, len(p)):
if (p[i] - p[i - 3]) > (p[i - 1] - p[i - 4]):
count += 1
return count
if __name__ == '__main__':
main()