Skip to content

Commit 59c7f37

Browse files
committed
day01
1 parent 7807929 commit 59c7f37

File tree

4 files changed

+2362
-0
lines changed

4 files changed

+2362
-0
lines changed

2022/01/__init__.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
def cast_input(inputs: str):
2+
return [int(x) if x else None for x in inputs.splitlines()]
3+
4+
5+
def part1(inputs):
6+
result = 0
7+
acc = 0
8+
for calory in inputs:
9+
if calory is None:
10+
result = max(result, acc)
11+
acc = 0
12+
else:
13+
acc += calory
14+
return result
15+
16+
17+
class Top:
18+
def __init__(self, k: int = 1):
19+
self._k = k
20+
self._top = [0] * k # left > right
21+
22+
def push(self, val: int):
23+
self._top.append(val)
24+
for i in range(self._k - 1, -1, -1):
25+
if self._top[i] >= self._top[i + 1]:
26+
break
27+
self._top[i : i + 2] = self._top[i + 1], self._top[i]
28+
self._top.pop()
29+
30+
@property
31+
def top(self):
32+
return self._top[: self._k]
33+
34+
35+
def part2(inputs):
36+
t = Top(3)
37+
acc = 0
38+
for calory in inputs:
39+
if calory is None:
40+
t.push(acc)
41+
acc = 0
42+
continue
43+
acc += calory
44+
t.push(acc)
45+
return sum(t.top)
46+
47+
48+
def new_part1(inputs):
49+
t = Top(1)
50+
acc = 0
51+
for calory in inputs:
52+
if calory is None:
53+
t.push(acc)
54+
acc = 0
55+
continue
56+
acc += calory
57+
t.push(acc)
58+
return sum(t.top)

0 commit comments

Comments
 (0)