aoc2015

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

commit 1002e1f221a260e1ede56b5b105639e89d09c322
parent 1911bccc1c4935ba0812db0b7a4a613e8e6716d8
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 27 Oct 2022 20:21:46 +0000

solve part 1 of day08

Diffstat:
Mday08/Day08.pm | 9+++++++--
Mday08/day08.pl | 2+-
2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/day08/Day08.pm b/day08/Day08.pm @@ -7,6 +7,7 @@ use v5.32; use Exporter qw( import ); our @EXPORT = qw( count_characters ); +open my $fh, '>', 'myoutput.txt' or die "error: $!"; sub count_characters { my $string = shift; @@ -14,6 +15,9 @@ sub count_characters { # if ( $string =~ m/\\\"/ ) { # say "I found a '\\\"' in $string"; # } + $string =~ s/^"//; # remove beginning quote + #$string =~ s| + $string =~ s/"$//; # remove trailing quote my $backslash = qr/\\\\/; my $double_quote = qr/\\\"/; my $ascii = qr/\\x[0-9a-f]{2}/; @@ -23,10 +27,11 @@ sub count_characters { my $backslash_count = () = $string =~ m/$backslash/g; my $double_quote_count = () = $string =~ m/$double_quote/g; my $ascii_count = () = $string =~ m/$ascii/gi; - say "$string\t", length $string, "\t$backslash_count\t$double_quote_count\t$ascii_count"; - my $string_characters = length $string; + my $string_characters = 2 + length $string; my $memory_characters = $string_characters - 2 - $backslash_count - $double_quote_count - (3 * $ascii_count); + printf "%50s%5d%5d%5d%5d%5d%5d\n", $string, $string_characters, $backslash_count, $double_quote_count, $ascii_count, $memory_characters, ($string_characters-$memory_characters); + print $fh "$string\t$string_characters\t$memory_characters\n"; return $string_characters - $memory_characters; } diff --git a/day08/day08.pl b/day08/day08.pl @@ -13,7 +13,7 @@ chomp( my $input = do { local $/; <> } ); my @strings = split /\n/, $input; my $part1; -say "string\tlength\tslash\tquote\tascii"; +printf "%50s%5s%5s%5s%5s%5s%5s\n", "str", "len", "\\", "\"", "\\x", "mem", "dif"; foreach ( @strings ) { $part1 += count_characters( $_ ); }