Skip to content

Commit 6660f6d

Browse files
committed
Solve 2022 Day 1 in python
1 parent c680ed7 commit 6660f6d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

2022/Day1/python/solve.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def part1(lines):
2+
inventories = []
3+
current = []
4+
for line in lines:
5+
if line == '\n':
6+
inventories.append(current)
7+
current = []
8+
continue
9+
else:
10+
current.append(int(line))
11+
12+
max_calories = 0
13+
14+
for inventory in inventories:
15+
s = sum(inventory)
16+
if s > max_calories:
17+
max_calories = s
18+
19+
return max_calories
20+
21+
def part2(lines):
22+
inventories = []
23+
current = []
24+
for line in lines:
25+
if line == '\n':
26+
inventories.append(current)
27+
current = []
28+
continue
29+
else:
30+
current.append(int(line))
31+
32+
calories = []
33+
for inventory in inventories:
34+
calories.append(sum(inventory))
35+
36+
calories.sort(reverse=True)
37+
return sum(calories[0:3])
38+
39+
with open('../input.txt') as f:
40+
lines = f.readlines()
41+
42+
print(part1(lines))
43+
print(part2(lines))

0 commit comments

Comments
 (0)