Wednesday, December 11, 2024

HTML table year calendar

I wrote a simple new program in Perl, yeartable.pl, another one like this other old calendar program, but now I try to visualize all the days of a given year in one page, suitable to be copied to your favourite word processor and then printed, to get a calendar like this:

Calendar table view in spanish

To run it you need to provide the year, the names of the months and the names of the week days, for example:

$ perl yeartable.pl
Use: perl yeartable.pl YEAR "Jan,Feb,..." "Sun,*Mon,..."
(put * in the week-day to be the first row in the table)
$ perl yeartable.pl 2025 "Jan,Feb,Mar,Apr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic" "S,*M,T,W,T,F,S" > 2025.html

And then you can open the generated file with an internet browser, copy the table directly to a word processor and adjust the format. Enjoy!

Using jugs of known capacities to get certain quantity

Suppose you only have a few containers of known capacities for measuring quantities, for example two jugs of 5 and 7 litres of capacity respectively, and you want to use them to obtain exactly 1 litre, for example. Can you do it?

jars 7 5 litres

To solve this kind of problems, I wrote a simple Perl program, jugs.pl, that explores the solutions using a brute-force backtracking algorithm limiting its recursivity level for finding the solutions with minimum steps.

There are two versions of these problems, one having a limited quantity of the substance to be moved from one jug to another, and other having an unlimited quantity available to fill any jug and discard it. My program only supports the first version, since the second one can be simulated by creating one jug filled with a large quantity that represents the source and sink of the substance, for example:

jars 20 7 5 litres

This way you can use the program to print a solution for the problem:

$ perl jugs.pl 20,7,5 20,0,0 1
(12) (7) (1)    1->3 3->2 1->3 3->2 2->1 3->2 1->3 3->2
$ perl jugs.pl
Use: perl jugs.pl M1,M2,...,MX I1,I2,...,IX N [NUMSTEPS]
Minimum steps to obtain N units in one of X jugs
given the Maximum and Initial quantity for each jug.

This way you can explore different configurations to learn more from the problem, even setting the number of steps of the solutions. Enjoy!

Sunday, August 4, 2024

Simulating a dice of any number of sides by rolling a different dice

Suppose for example that you have a dice of 6 sides and you want to obtain a random result between 1 and 8, how could you do it?

Well, one way is running my Perl script anydice.pl, rolling the dice the indicated number of times and then checking what is the result indicated by the tables printed by the program.

The method used by the program is explained in the paper "Simulating a dice with a dice" by B. Kloeckner.

$ perl anydice.pl
Use: anydice.pl [-d N] [-w] MAX
Print tables to get a random number from 1 to MAX by rolling a dice.
  -d N, -dN    Use a dice of N sides, 6 by default
  -w           Print the tables in HTML instead of ASCII
Important: When rolling a dice many times, if you get the result ?
then you must repeat ALL the rolls to get a correct probability.
$ perl anydice.pl -d6 8
A\B| 1 2 3 4 5 6
---+------------
 1 | 1 1 1 1 2 2
 2 | 2 2 3 3 3 3
 3 | 4 4 4 4 5 5
 4 | 5 5 6 6 6 6
 5 | 7 7 7 7 8 8
 6 | 8 8 ? ? ? ?

In this case, you must roll the D6 two times, A and B, and then look at the table to see what is the result. Note that if you get the result "?" when rolling the dice many times, then you must repeat all the rolls from the beginning, otherwise you will get certain results with a probability not equal to the others.

Monday, July 15, 2024

Cistercian numerals in ASCII Art

So you want to convert any number to the Cistercian numerals in your terminal? Well, in that case please download my cisternum.pl Perl script to do it and enjoy how the monks from the XIII century used to write numbers!

$ perl cisternum.pl
Use: cisternum.pl [-h|-v] NUMBER ...
Converts numbers to Cistercian numerals represented in ASCII.

 NUMBER Number encoded using Arabic numerals from 0 to 9999
 -h     Uses the horizontal representation, the default
 -v     Uses the vertical representation instead of horizontal

$ perl cisternum.pl 2024 07 16
           _         _      
 _\____   |______    ______ 
  |  |              |       

$ perl cisternum.pl -v 2024 07 16
          _    _   
 _|/     | |    | |
  |      |      |  
 _|      |      |  
  |      |      |