aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol2.pl (989B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 open (my $fh, '<', 'input') or die "$!";
      6 
      7 my @items;
      8 {
      9     local $/;
     10     my $content = <$fh>;
     11     @items = split("\n", $content);
     12 }
     13 
     14 # HACK: don't have to compute ID for all of them--sort then compute ID
     15 #
     16 # BUT: we'll brute force it...
     17 my @ids = map { id($_) } @items;
     18 @ids = sort { $b <=> $a } @ids;
     19 
     20 # NOTE: not first or last
     21 for (my $i = 1; $i < $#ids; $i++ ) {
     22     if ($ids[$i-1] - $ids[$i] > 1) {
     23         print $ids[$i] + 1 . "\n"; # 2 seat gap, print middle
     24         exit(0);
     25     }
     26 }
     27 exit(1);
     28 
     29 
     30 sub id {
     31     my ($v) = @_;
     32     my $id;
     33     # row
     34     for (my $i = 0; $i < 7; $i++) {
     35         if (substr($v, $i, 1) eq 'B') {
     36             $id += (2 ** (6 - $i)) * 8;
     37         } else {
     38             # F -- lower bound stays same, no addition
     39         }
     40     }
     41     # col
     42     for (my $i = 7; $i < 10; $i++) {
     43         if (substr($v, $i, 1) eq 'R') {
     44             $id += 2 ** (9 - $i);
     45         } else {
     46             # R
     47         }
     48     }
     49     return $id;
     50 }