Skip to content

Commit 5f94de2

Browse files
committed
Day 09 optimised
1 parent e4f0540 commit 5f94de2

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

2024/day_09/sol.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
def find_next_i(data):
2-
i = int(len(data) / 2) * 2
3-
while i > 0 and data[i] == 0:
4-
i -= 2
5-
return int(i)
6-
71
def sol(data):
82
res = 0
93
idx = 0
4+
k = int(len(data) / 2) * 2
105

116
for i in range(len(data)):
127
while data[i]:
138
if i % 2 == 0:
149
res += (idx * (i/2))
1510
if i % 2 == 1:
16-
# nit: find_next_i could be optimised
17-
k = find_next_i(data)
11+
while data[k] == 0 and k > 0:
12+
k -= 2
1813
if k <= i:
1914
break
2015
res += (idx * (k/2))

0 commit comments

Comments
 (0)