170 tokens in Python for part 2 by dbasden
Download solution
import
sys
pos
=
0
files
=
{
}
free
=
{
}
for
i
,
blocks
in
enumerate
(
map
(
int
,
sys
.
stdin
.
read
(
)
.
strip
(
)
)
)
:
if
i
&
1
:
free
[
pos
]
=
blocks
else
:
files
[
pos
]
=
blocks
,
i
//
2
pos
+=
blocks
for
filepos
,
(
fileblocks
,
fileid
)
in
list
(
reversed
(
files
.
items
(
)
)
)
:
for
holepos
,
holelen
in
sorted
(
free
.
items
(
)
)
:
if
holepos
+
fileblocks
<=
filepos
and
holelen
>=
fileblocks
:
files
[
holepos
]
=
files
.
pop
(
filepos
)
del
free
[
holepos
]
free
[
holepos
+
fileblocks
]
=
holelen
-
fileblocks
break
print
(
sum
(
fileid
*
sum
(
range
(
filepos
,
filepos
+
fileblocks
)
)
for
filepos
,
(
fileblocks
,
fileid
)
in
files
.
items
(
)
)
)