-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.py
66 lines (48 loc) · 1.17 KB
/
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
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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_elves_from_lines(lines):
elves = []
elf = []
for line in lines:
if line:
elf.append(int(line))
else:
elves.append(elf)
elf = []
if elf:
elves.append(elf)
return elves
def get_elves_from_file(file_path=top_dir + "resources/year2022_day1_input.txt"):
with open(file_path) as f:
return get_elves_from_lines([l.strip() for l in f])
def max_elf(elves, nb=1):
calories = sorted(sum(elf) for elf in elves)
top = calories[-nb:]
return sum(top)
def run_tests():
elves = get_elves_from_lines(
"""1000
2000
3000
4000
5000
6000
7000
8000
9000
10000""".splitlines()
)
assert max_elf(elves, 1) == 24000
assert max_elf(elves, 3) == 45000
def get_solutions():
elves = get_elves_from_file()
print(max_elf(elves, 1) == 68467)
print(max_elf(elves, 3) == 203420)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)