Monday, April 14, 2014

Sorting disk usage, Perl to the rescue.

My MacBook Air was close to full up after a lot of music editting (see results at http://www.stmaryssingers.com/recordings.html ). In the past I would run

du -s * | sort -rn

to identify the biggest users of disk space giving for example:
14104880        iTunes
683304  Noël Français
682592  sibs
221488  DancingDay
93664   MissaAlmePater
27480   ChristmasLullaby
17680   JesuJoy
13656   MissaBenedicamus
12256   MassInHonorOfSaintJoseph
8760    pdfs
3584    ChrissyCarols
2456    LookingAtTheStars
1352    BriggsMass
408     Bach-Jesu
264     40_The_First_Nowell.sib
64      Thou-knowest-Lord-Z-58b.pdf
24      StMS20140412
24      StMS20140322
24      StMS20140309
24      StMS20140208
24      StMS20131214
0       GarageBand


and the numbers are in 512-byte blocks. Nowadays that's a lot of digits to decipher in the listing. So I started using the 'h' ('human readable') option:

du -sh * | sort -rn

giving:
676K    BriggsMass
334M    Noël Français
333M    sibs
204K    Bach-Jesu
132K    40_The_First_Nowell.sib
108M    DancingDay
 46M    MissaAlmePater
 32K    Thou-knowest-Lord-Z-58b.pdf
 13M    ChristmasLullaby
 12K    StMS20140412
 12K    StMS20140322
 12K    StMS20140309
 12K    StMS20140208
 12K    StMS20131214
8.6M    JesuJoy
6.7M    MissaBenedicamus
6.7G    iTunes
6.0M    MassInHonorOfSaintJoseph
4.3M    pdfs
1.8M    ChrissyCarols
1.2M    LookingAtTheStars

  0B    GarageBand
Unfortunately sort doesn't know how to sort the unit suffixes. But Perl does. It's a while since I used the Schwartzian Transform but it seems perfect for the task. I copied the Wiki code into a file, dusort.pl, which I placed in a directory in my PATH variable (~/bin in this case) and modified the regex extraction to make it sort by unit suffix first and then by number giving this:

#!/usr/bin/env perl 
use 5.010;

my $size = {P => 6, T => 5, G => 4, M => 3, K => 2, B => 1};
print
  map { $_->[0] }
  sort {
  $size->{$b->[2]} <=> $size->{$a->[2]}
                  ||
        $b->[1] <=> $a->[1]
  }
  map { [$_, /^([ \.0-9]{3,4})([PTGMKB])\t/] }
  <>;

and now when I run the command(s):
du -sh * | dusort.pl
I get the result:
6.7G    iTunes
334M    Noël Français
333M    sibs
108M    DancingDay
 46M    MissaAlmePater
 13M    ChristmasLullaby
8.6M    JesuJoy
6.7M    MissaBenedicamus
6.0M    MassInHonorOfSaintJoseph
4.3M    pdfs
1.8M    ChrissyCarols
1.2M    LookingAtTheStars
676K    BriggsMass
204K    Bach-Jesu
132K    40_The_First_Nowell.sib
 32K    Thou-knowest-Lord-Z-58b.pdf
 12K    StMS20131214
 12K    StMS20140208
 12K    StMS20140309
 12K    StMS20140322
 12K    StMS20140412
  0B    GarageBand
Obviously the next thing to do is to make a shell alias:
alias dus='du -sh * | dusort.pl'
and now I even save a few keystrokes in my task to pinpoint the Biggest (L)User.



Monday, April 7, 2014

Threes! and Threesus A.I.

I decided to try the Threes! app/game on my iPhone. I got to around 21,000 just playing by myself but I went Googling to see if there were ways to improve my score. Found quite a few "tips" articles had discovered most of what I was doing. (No-one mentions how to speed up the score totalling at the end of the game. (Swipe the screen a second or third time. Saves seconds per game.))

But then I discovered Threesus, watched the video for nearly 30 minutes in total awe and decided I would download Threesus and use the Assistant to help me get my score higher.

And once again it became another exercise in yak-shaving...

Threesus is written in C#, Microsoft's attempt at Java+Objective-C+... and is apparently pretty-well compulsory if you are programming on Windows. The author states that one can install Visual Studio Express for Windows Desktop and Threesus should compile and run in commandline Assistant mode.

Well, I don't have a Win box sitting around but I do have Parallels on my MacBookAir so I downloaded and installed Express and was able to fire up ThreesusAssistant very quickly. I had to read the source code to work out the commands but they are simple enough.

What intrigued me was the mention of the 'bonus' cards and how they are indicated by a '+' sign on the card. I'd never seen the '+' sign. The bonus cards were just like the normal '3' card on my version of Threes. Walt Destler has also blogged a high-level overview of Threesus and he refers to a detailed description of the Threes! algorithm and that's when I realised my copy of Threes! was out of date. Version 1.0.3 introduced the '+' sign on bonus cards.

So after a quick update I was able to use the Assistant to get my score upto around 28,000.

But disaster hit! The Assistant failed with a null reference error before I could get a higher score. I didn't fancy attempting to debug a run-time error in a language I don't know using an operating system I don't know.

Destler also mentions Xamarin Studio as a possible alternative for Mac users. I duly installed it and spent quite a few hours attempting to get Threesus to compile and run. Lots of googling for obscure errors, settings etc. etc. The usual yak-shaving for any new software.

At last I got Threesus to start in Xamarin. I input the initial grid data and it commenced "thinking". About 5 minutes later it returned the next move. Then it took 10 minutes for the next move. No way is Xamarin/Mono usable on Mac OSX. The Assistant is already slow enough in Visual Studio on Parallels but it seems to be 100s of times slower in Xamarin on Mac.

So now I have a problem. Any sensible person would give up at this stage and find something useful to do.

But not me. I threw a few dollars into the RPerl Kickstarter project last year in the hope that they could get further than a faster bubblesort and it seems to be coming along nicely. Perhaps it's ready to take on a Threes! playing A.I.? Once again a lot of yak-shaving to get the source downloaded and installed but it appears to be working. Now to see if I can translate/recode C# code into working Perl and then possibly speeding it up with RPerl. RPerl uses the Perl Inline module which also allows raw C or C++ to be in-lined into Perl code. That might save a lot of hassle.

I also realised that this could be a nice project to try out OCRing the iPad screen of the Threes! game grid. I don't want to build a full game-playing robot but the slowest and most error-prone part of using the Assistant is inputting the changes. If I could add a screen-reading section so that the Assistant merely has to output Left, Right, Up or Down it would speed manual play greatly. And while I'm at it, why not use a text to speech converter so I don't even have to look at the Assistant's output. (Time to start lobbying the game developer to add speech input!)  I've got an old iPhone 3GS to supply video input and there are some great articles on OCRing Sudoku grids from newspapers etc. The Threes! grid is very similar. So this might turn out to be a very elaborate but maybe quite fun project. Or a total disaster and time-sink...