#!/usr/local/bin/perl # day 2015-01 use strict; use warnings; use v5.32; @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my @instructions = split //, $input; my $floor = 0; my $position = -1; while ( $floor > -1 ) { $position += 1; $floor += 1 if $instructions[$position] eq '('; $floor -= 1 if $instructions[$position] eq ')'; } # see https://perldoc.perl.org/perlfaq4#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string? say "part 1: ", (( $input =~ tr/(// ) - ( $input =~ tr/)// )); say "part 2: ", $position + 1;