+#!/usr/bin/perl
+use v5.38;
+use warnings FATAL => 'all';
+no autovivification; # libautovivification-perl
+use utf8;
+use open ':std', ':encoding(UTF-8)';
+use File::Glob ':bsd_glob';
+# Note meta chars: \ [] * ?, test with =~ /[\\[\]*?]/ . avoid glob() it splits pattern on spaces.
+sub glo {
+ my ($pattern, $flags) = @_;
+ $flags //= GLOB_ERR | GLOB_NOSORT | GLOB_QUOTE;
+ my @results = bsd_glob($pattern, $flags);
+ if (GLOB_ERROR) { die "GLOB_ERROR: " . GLOB_ERROR . " pattern:'$pattern', \$!:$!"; }
+ return @results;
+}
+
+sub process_number {
+ my ($num) = @_;
+
+ # Remove decimal part and convert to integer
+ $num = int($num);
+ $num = int($num / 10_000);
+
+ return "0" if $num <= 0;
+
+ # Keep only first 2 digits, set rest to 0
+ my $str = "$num";
+ my $len = length($str);
+
+ if ($len > 2) {
+ my $first_two = substr($str, 0, 2);
+ my $zeros = "0" x ($len - 2);
+ $num = int($first_two . $zeros);
+ }
+ #say "debug num $num"
+
+ # Add commas
+ my $formatted = reverse $num;
+ $formatted =~ s/(\d{3})(?=\d)/$1,/g;
+ $formatted = reverse $formatted;
+
+ return $formatted;
+}
+
+my $input_file = $ARGV[0];
+my $output_file = $ARGV[1];
+
+my $input_fh;
+my $output_fh;
+
+if ($input_file) {
+ open($input_fh, '<', $input_file) or die "Cannot open input file '$input_file': $!";
+} else {
+ $input_fh = \*STDIN;
+}
+
+if ($output_file) {
+ open($output_fh, '>', $output_file) or die "Cannot open output file '$output_file': $!";
+} else {
+ $output_fh = \*STDOUT;
+}
+
+my @lines = ();
+my @cost_values = ();
+
+print $output_fh "/* iankqmark\n";
+# Process each line
+while (my $line = <$input_fh>) {
+
+ my $original_line = $line;
+ my $second_cost = 0;
+
+ # Extract second cost value before processing
+ if ($line =~ /cost=\d+(?:\.\d+)?\.\.(\d+(?:\.\d+)?)/) {
+ $second_cost = $1;
+ # Apply the same reduction rules to get the sorting value
+ $second_cost = int($second_cost);
+ $second_cost = int($second_cost / 10_000);
+ $second_cost = 0 if $second_cost <= 0;
+ }
+
+
+ # cost=number..number or cost=number
+ $line =~ s/cost=(\d+(?:\.\d+)?)\.\.(\d+(?:\.\d+)?)/
+ "cost=" . process_number($1) . ".." . process_number($2)/ge;
+
+ #$line =~ s/cost=(\d+(?:\.\d+)?)(?!\.)/"cost=" . process_number($1)/ge;
+
+ # rows=number
+ $line =~ s/rows=(\d+(?:\.\d+)?)/"rows=" . process_number($1)/ge;
+
+ # width=number
+ #$line =~ s/width=(\d+(?:\.\d+)?)/"width=" . process_number($1)/ge;
+
+ # Match loops patterns: loops=number
+ $line =~ s/loops=(\d+(?:\.\d+)?)/"loops=" . process_number($1)/ge;
+
+ push @lines, {
+ processed_line => $line,
+ second_cost => $second_cost,
+ original_index => scalar(@lines)
+ };
+
+ push @cost_values, $second_cost;
+
+}
+
+my @sorted_indices = sort {
+ $lines[$b]->{second_cost} <=> $lines[$a]->{second_cost} ||
+ $a <=> $b # Secondary sort by original line order for ties
+} 0..$#lines;
+
+my %rank_map = ();
+my $rank = 1;
+for my $i (0..$#sorted_indices) {
+ last if $rank > 9;
+ my $idx = $sorted_indices[$i];
+ if ($lines[$idx]->{second_cost} > 0) {
+ $rank_map{$idx} = $rank;
+ $rank++;
+ }
+}
+
+# Output lines in original order with rank replacements
+for my $i (0..$#lines) {
+ my $output_line = $lines[$i]->{processed_line};
+
+ # Replace first character with rank if this line is in top 9
+ if (exists $rank_map{$i}) {
+ substr($output_line, 0, 1) = $rank_map{$i};
+ }
+
+ print $output_fh $output_line;
+}
+
+print $output_fh "\n*/\n";
+
+# Close file handles
+close($input_fh) if $input_file;
+close($output_fh) if $output_file;