aoc2015

Advent of Code 2015 solutions in Perl.
git clone git://git.samirparikh.com/aoc2015
Log | Files | Refs | README

day01.pl (583B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2015-01
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 
      8 @ARGV = "input" unless @ARGV;
      9 
     10 chomp( my $input = do { local $/; <> } );
     11 
     12 my @instructions = split //, $input;
     13 
     14 my $floor    = 0;
     15 my $position = -1;
     16 while ( $floor > -1 ) {
     17     $position += 1;
     18     $floor += 1 if $instructions[$position] eq '(';
     19     $floor -= 1 if $instructions[$position] eq ')';
     20 }
     21 
     22 # see https://perldoc.perl.org/perlfaq4#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string?
     23 say "part 1: ", (( $input =~ tr/(// ) - ( $input =~ tr/)// ));
     24 say "part 2: ", $position + 1;