344 tokens in C for part 1 by dbasden
Download solution
#include
<stdio.h>
char
grid
[
40960
]
int
replanted_grid
[
40960
]
int
i
,
j
,
width
long
int
total
int
area
[
1024
]
,
perimeter
[
1024
]
static
void
replant
(
int
pos
,
int
from
,
int
to
)
{
if
(
grid
[
pos
]
!=
from
)
return
replanted_grid
[
pos
]
=
to
grid
[
pos
]
+=
32
replant
(
pos
-
144
,
from
,
to
)
replant
(
pos
-
1
,
from
,
to
)
replant
(
pos
+
144
,
from
,
to
)
replant
(
pos
+
1
,
from
,
to
)
}
static
int
replant_everything
(
)
{
int
next_colour
=
1
for
(
i
=
145
i
<
40815
++
i
)
{
int
ch
=
grid
[
i
]
if
(
ch
>=
'
A
'
&&
ch
<=
'
Z
'
)
replant
(
i
,
ch
,
next_colour
++
)
}
return
next_colour
}
int
main
(
)
{
char
*
p
=
&
(
grid
[
1
]
)
while
(
scanf
(
"
%
s
%
n
"
,
(
char
*
)
(
p
+=
144
)
,
&
width
)
==
1
)
int
max_ch
=
replant_everything
(
)
for
(
i
=
144
+
1
i
<
40960
-
144
-
1
++
i
)
{
int
ch
=
replanted_grid
[
i
]
if
(
replanted_grid
[
i
]
)
{
area
[
ch
]
++
perimeter
[
ch
]
+=
(
replanted_grid
[
i
-
144
]
!=
ch
)
+
(
replanted_grid
[
i
-
1
]
!=
ch
)
+
(
replanted_grid
[
i
+
144
]
!=
ch
)
+
(
replanted_grid
[
i
+
1
]
!=
ch
)
}
}
for
(
int
ch
=
1
ch
<
max_ch
ch
++
)
if
(
area
[
ch
]
)
total
+=
area
[
ch
]
*
perimeter
[
ch
]
printf
(
"
%
l
d
\
n
"
,
total
)
}