Advertisement
mrob27

Hollerith punch card emulator for an MMH 2016 puzzle

Jan 19th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.71 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #                                               Robert Munafo
  3. #                                        Test Solution Please Ignore
  4. #                                         MIT IAP Mystery Hunt, 2016
  5. #      MMH 2016 "Huntception" (aka "Muttstery Hunt") : Randolph Carter : The Gibbous, Non-Euclidean Program
  6.  
  7. my @cardrows = ();
  8.  
  9. sub clearcard
  10. {
  11.   my($c) = @_;
  12.   for ($i=0; $i<12; $i++) {
  13.     $cardrows[$i] = ("0" x 80);
  14.   }
  15.   $col = $c;
  16.   $page = $page << 1;
  17. }
  18.  
  19. # $page starts at 1, then 2 then 4.
  20. # we punch by ORing in the current page bit
  21. # if the same char appears in the same position on two pages
  22. # the holes in that column will have the page values ORd together
  23. # if a hole is 7, all three pages' cards have a hole in that position.
  24. sub newpage
  25. {
  26.   my($c) = @_;
  27.   $col = $c;
  28.   $page = $page << 1;
  29. }
  30.  
  31. # show the card as 3-bit values 01234567
  32. sub display
  33. {
  34.   my($i);
  35.   for ($i=0; $i<12; $i++) {
  36.     print "$cardrows[$i]\n";
  37.   }
  38. }
  39.  
  40. # don't show 0's
  41. sub disp1
  42. {
  43.   my($i, $t);
  44.   for ($i=0; $i<12; $i++) {
  45.     $t = $cardrows[$i];
  46.     $t =~ s/0/./g;
  47.     print "$t\n";
  48.   }
  49. }
  50.  
  51. # show only the 7's
  52. sub disp2
  53. {
  54.   my($i, $t);
  55.   for ($i=0; $i<12; $i++) {
  56.     $t = $cardrows[$i];
  57.     $t =~ s/[0-6]/./g;
  58.     print "$t\n";
  59.   }
  60. }
  61.  
  62. # punch a hole at the gven column and row
  63. sub punch_col_row
  64. {
  65.   my($c, $r) = @_;
  66.   my (@dots);
  67.   my($t);
  68.   @dots = split(//, $cardrows[$r]);
  69.   $t = @dots[$c];
  70.   $t = ord($t) - ord('0');
  71.   $t |= $page;
  72.   @dots[$c] = chr(ord('0') + $t);
  73.   $cardrows[$r] = join('', @dots);
  74. }
  75.  
  76. # IBM Hollerith code
  77. # from homepage.cs.uiowa.edu/~jones/cards/codes.html
  78. #   "The IBM model 029 keypunch, introduced around 1964, was the most
  79. #   common keypunch of the late 1960's and early 1970's"
  80. %hcodes = (
  81.   "0" => "..O.........",
  82.   "1" => "...O........",
  83.   "2" => "....O.......",
  84.   "3" => ".....O......",
  85.   "4" => "......O.....",
  86.   "5" => ".......O....",
  87.   "6" => "........O...",
  88.   "7" => ".........O..",
  89.   "8" => "..........O.",
  90.   "9" => "...........O",
  91.   "A" => "O..O........",
  92.   "B" => "O...O.......",
  93.   "C" => "O....O......",
  94.   "D" => "O.....O.....",
  95.   "E" => "O......O....",
  96.   "F" => "O.......O...",
  97.   "G" => "O........O..",
  98.   "H" => "O.........O.",
  99.   "I" => "O..........O",
  100.   "J" => ".O.O........",
  101.   "K" => ".O..O.......",
  102.   "L" => ".O...O......",
  103.   "M" => ".O....O.....",
  104.   "N" => ".O.....O....",
  105.   "O" => ".O......O...",
  106.   "P" => ".O.......O..",
  107.   "Q" => ".O........O.",
  108.   "R" => ".O.........O",
  109.   "/" => "..OO........",
  110.   "S" => "..O.O.......",
  111.   "T" => "..O..O......",
  112.   "U" => "..O...O.....",
  113.   "V" => "..O....O....",
  114.   "W" => "..O.....O...",
  115.   "X" => "..O......O..",
  116.   "Y" => "..O.......O.",
  117.   "Z" => "..O........O",
  118. );
  119.  
  120. # Punch a character at the current column $col (a global)
  121. sub pchar
  122. {
  123.   my($c) = @_;
  124.   my(@dots);
  125.   my($j);
  126.   # print "pchar('$c')\n";
  127.   if ($hcodes{$c} ne '') {
  128.     @dots = split(//, $hcodes{$c});
  129.     # print "$c -> $dots[0] $dots[1] $dots[2] ...\n";
  130.     for($j=0; $j<12; $j++) {
  131.       if ($dots[$j] eq 'O') {
  132.         &punch_col_row($col, $j);
  133.       }
  134.     }
  135.   } else {
  136.     die "need to know how to punch a '$c'\n";
  137.   }
  138. }
  139.  
  140. # First interpretation of "OVERPUNCH TYPOS AND CORRECTIONS, THEN OVERLAY"
  141. # Punch the typo and its correction into whatever column it appears in the
  142. # source text. Overlay the 3 cards on top of each other. For light to get
  143. # through there has to be a hole in the same position on all 3 cards.
  144. open ($IN, "errata.txt");
  145. &clearcard(0);
  146. $page = 1;
  147. while($l = <$IN>) {
  148.   chomp $l;
  149.   if ($l =~ m/eject/) {
  150.     # We're done punching this card
  151.     &disp1();
  152.     &newpage(0);
  153.   } else {
  154.     ($col, $fr, $to) = split(/\t/, $l);
  155.     &pchar($fr);
  156.     &pchar($to);
  157.     print sprintf("%2d   $fr   $to\n", $col);
  158.   }
  159. }
  160. &disp2();
  161. close($IN);
  162.  
  163. # In this version we ignore what columns the typos occurred in, and instead
  164. # just punch the first typo/correction onto column 1, the next onto col 2, etc.
  165. open ($IN, "errata.txt");
  166. &clearcard(6);
  167. $page = 1;
  168. while($l = <$IN>) {
  169.   chomp $l;
  170.   if ($l =~ m/eject/) {
  171.     # We're done punching this card
  172.     &disp1();
  173.     &newpage(6);
  174.   } else {
  175.     ($x, $fr, $to) = split(/\t/, $l);
  176.     &pchar($fr);
  177.     &pchar($to);
  178.     $col++;
  179.     print sprintf("%2d   $fr   $to\n", $x);
  180.   }
  181. }
  182. close($IN);
  183.  
  184. # Display just the spots where light could shine through all 3 cards.
  185. print "\n";
  186. &disp2();
  187.  
  188. # Now display these again along with the lines they are laid over.
  189. print "\n";
  190. open ($IN, "v2.f");
  191. $i = 0; $active = 0;
  192. while ($l = <$IN>) {
  193.   if ($l =~ m/JET=FFOS/) {
  194.     $active = 1;
  195.   }
  196.   if ($active && ($i < 12)) {
  197.     $t = $cardrows[$i];
  198.     $t =~ s/[0-6]/./g;
  199.     print "$t\n";
  200.     print "$l\n";
  201.     $i++;
  202.   }
  203. }
  204. close $IN;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement