275 tokens in Python for part 1 by LiquidFun
Download solution
from
collections
import
defaultdict
lines
=
open
(
0
)
.
read
(
)
.
splitlines
(
)
gear_to_num
=
defaultdict
(
list
)
s1
=
0
is_valid
=
lambda
y
,
x
:
0
<=
y
<
len
(
lines
)
and
0
<=
x
<
len
(
lines
[
0
]
)
for
y
in
range
(
len
(
lines
)
)
:
for
x_start
in
range
(
len
(
lines
[
y
]
)
)
:
x_end
=
x_start
if
lines
[
y
]
[
x_start
]
.
isdigit
(
)
and
(
not
is_valid
(
y
,
x_start
-
1
)
or
not
lines
[
y
]
[
x_start
-
1
]
.
isdigit
(
)
)
:
while
is_valid
(
y
,
x_end
+
1
)
and
lines
[
y
]
[
x_end
+
1
]
.
isdigit
(
)
:
x_end
+=
1
good
=
False
for
yd
in
range
(
y
-
1
,
y
+
2
)
:
for
xd
in
range
(
x_start
-
1
,
x_end
+
2
)
:
if
is_valid
(
yd
,
xd
)
and
lines
[
yd
]
[
xd
]
not
in
'
.
0
1
2
3
4
5
6
7
8
9
'
:
good
=
True
if
lines
[
yd
]
[
xd
]
in
'
*
'
:
gear_to_num
[
(
yd
,
xd
)
]
.
append
(
int
(
lines
[
y
]
[
x_start
:
x_end
+
1
]
)
)
s1
+=
good
*
int
(
lines
[
y
]
[
x_start
:
x_end
+
1
]
)
print
(
s1
)