-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.py
57 lines (44 loc) · 1.15 KB
/
day01.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
import os
from itertools import cycle
import logging as log
import pytest
LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG").upper()
log.basicConfig(level=LOGLEVEL)
def part1(iterable):
"""Return sum of the iterable
>>> part1([1, 2, 3])
6
"""
return sum(iterable)
def part2(iterable):
s = 0
seen = [0]
for line in cycle(iterable):
s += int(line)
# log.debug(s)
if s in seen:
return s
else:
seen.append(s)
@pytest.mark.parametrize(
"input, result",
[([+1, -2, +3, +1], 3), ([+1, +1, +1], 3), ([+1, +1, -2], 0), ([-1, -2, -3], -6)],
)
def test_part1(input, result):
assert part1(input) == result
@pytest.mark.parametrize(
"input, result",
[
([+1, -1], 0),
([+3, +3, +4, -2, -4], 10),
([-6, +3, +8, +5, -6], 5),
([+7, +7, -2, -7, -4], 14),
],
)
def test_part2(input, result):
assert part2(input) == result
if __name__ == "__main__":
with open("day01_input.txt") as file:
list_of_ints = [int(x) for x in file.readlines()]
print(part1(list_of_ints)) # 439
print(part2(list_of_ints)) # 124645