From: Jan "Yenya" Kasprzak Date: Thu, 7 Dec 2023 07:13:32 +0000 (+0100) Subject: Day 7: a bit polished version X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=8ddbceff25c1fb56111e738248e5e43fde487002;p=aoc.git Day 7: a bit polished version --- diff --git a/2023/13.pl b/2023/13.pl index b5bd859..acba42c 100755 --- a/2023/13.pl +++ b/2023/13.pl @@ -2,26 +2,19 @@ use v5.38; -my %cards; -while (<>) { - chomp; - my ($card, $bid) = split /\s+/; - $cards{$card} = $bid; - say " $card => $bid $_ " . val($card); -} +my %cards = map { /(\S+)/g } <>; sub rank { - my $card = shift; - my @cards = split //, $card; my %hist; - $hist{$_}++ for @cards; - return 1 if grep { $hist{$_} == 5 } @cards; - return 2 if grep { $hist{$_} == 4 } @cards; - return 3 if grep { $hist{$_} == 3 } @cards - and grep { $hist{$_} == 2 } @cards; - return 4 if grep { $hist{$_} == 3 } @cards; - return 5 if (grep { $hist{$_} == 2 } @cards) == 4; - return 6 if grep { $hist{$_} == 2 } @cards; + $hist{$_}++ for split //, shift; + my %rh; + $rh{$_}++ for values %hist; + return 1 if $rh{5}; + return 2 if $rh{4}; + return 3 if $rh{3} && $rh{2}; + return 4 if $rh{3}; + return 5 if $rh{2} && $rh{2} == 2; + return 6 if $rh{2}; return 7; } @@ -31,11 +24,11 @@ sub val { return rank($val).$val; } -my $i = 1; my $sum; +my $i; for my $card (sort { val($b) cmp val($a) } keys %cards) { - say "$card $i ", val($card); - $sum += $cards{$card} * $i++; + # say "$card $i ", val($card); + $sum += $cards{$card} * ++$i; } say $sum; diff --git a/2023/14.pl b/2023/14.pl index ac13982..bc98e76 100755 --- a/2023/14.pl +++ b/2023/14.pl @@ -2,34 +2,33 @@ use v5.38; -my %cards; -while (<>) { - chomp; - my ($card, $bid) = split /\s+/; - $cards{$card} = $bid; - say " $card => $bid $_ " . val($card); -} +my %cards = map { /(\S+)/g } <>; sub rank { - my $card = shift; - my @cards = split //, $card; my %hist; - $hist{$_}++ for @cards; - return 1 if grep { $hist{$_} == 5 } @cards; - return 2 if grep { $hist{$_} == 4 } @cards; - return 3 if grep { $hist{$_} == 3 } @cards - and grep { $hist{$_} == 2 } @cards; - return 4 if grep { $hist{$_} == 3 } @cards; - return 5 if (grep { $hist{$_} == 2 } @cards) == 4; - return 6 if grep { $hist{$_} == 2 } @cards; + $hist{$_}++ for split //, shift; + my %rh; + $rh{$_}++ for values %hist; + return 1 if $rh{5}; + return 2 if $rh{4}; + return 3 if $rh{3} && $rh{2}; + return 4 if $rh{3}; + return 5 if $rh{2} && $rh{2} == 2; + return 6 if $rh{2}; return 7; } +sub val { + my $val = shift; + my $r = rank2($val); + $val =~ y/AKQT98765432J/ABCDEFGHIJKLM/; + return $r.$val; +} + sub rank2 { my $card = shift; - my @cards = split //, $card; my $max_rank = rank($card); - for my $c (@cards) { + for my $c (split //, $card) { my $c1 = $card; $c1 =~ s/J/$c/g; my $r = rank($c1); @@ -38,18 +37,11 @@ sub rank2 { return $max_rank; } -sub val { - my $val = shift; - my $r = rank2($val); - $val =~ y/AKQT98765432J/ABCDEFGHIJKLM/; - return "$r$val"; -} - -my $i = 1; my $sum; +my $i; for my $card (sort { val($b) cmp val($a) } keys %cards) { - say "$card $i ", val($card); - $sum += $cards{$card} * $i++; + $sum += $cards{$card} * ++$i; + # say "$card $i ", val($card); } say $sum;