/
kanji-floodfill
executable file
·97 lines (77 loc) · 2.2 KB
/
kanji-floodfill
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env perl
use 5.14.0;
use warnings;
use utf8::all;
use JSON;
use File::Slurp 'slurp', 'write_file';
use Lingua::JA::Heisig 'kanji', 'heisig_number';
my $rtk3 = 0;
my $status_file = "$ENV{HOME}/devel/vocabulary/.kanji-poster";
my %kanji_status = %{ from_json(slurp($status_file), { utf8 => 1 } ) };
my @status;
my @row;
for my $i (0 .. ($rtk3 ? 3029 : 2041)) {
my $kanji = (kanji)[$i];
my $status = 0;
$status = 1 if $kanji_status{$kanji}
&& $kanji_status{$kanji} ne 'card';
push @row, $status;
if ($i == 3006) {
push @row, 0, 0, 0, 0;
}
if (
($i < 2041 && $i % 32 == 31)
|| ($i > 2041 && $i % 32 == 25)
|| ($i == 2041 || $i == 3029)
) {
push @status, [splice @row];
}
}
my @colored;
my $color = 0;
my %count_of;
for (my $y = 0; $y < @status; ++$y) {
for (my $x = 0; $x < 32; ++$x) {
next if !$status[$y][$x] || defined($colored[$y][$x]);
++$color;
my @queue = [$x, $y];
while (my $try = shift @queue) {
my ($x, $y) = @$try;
next if defined $colored[$y][$x];
if (!$status[$y][$x]) {
$colored[$y][$x] = 0;
next;
}
$colored[$y][$x] = $color;
++$count_of{$color};
for my $next ([$x-1, $y+0], [$x+1, $y+0], [$x+0, $y-1], [$x+0, $y+1]) {
my ($x, $y) = @$next;
next if defined $colored[$y][$x];
next if $y < 0 || $y >= @status
|| $x < 0 || $x >= 32;
push @queue, $next;
}
}
}
}
my %real_color_of;
$color = 30;
for my $index (sort { $count_of{$b} <=> $count_of{$a} } keys %count_of) {
$real_color_of{$index} = ++$color;
last if $color == 37;
}
my ($x, $y) = (0, 0);
for my $i (0 .. ($rtk3 ? 3029 : 2041)) {
my $kanji = (kanji)[$i];
if ($colored[$y][$x]) {
my $color = $real_color_of{$colored[$y][$x]} || 37;
print "\e[1;${color}m";
}
else {
print "\e[1;30m";
}
print $kanji;
$x = 0, $y++, print "\n" if ++$x == 32 || $i == 2041 || $i == 3029;
$x += 4, print " " if $i == 3006;
print "\e[m";
}