From 2dcd833e6a80764bbadc610aa2b55366c1cb1baf Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 7 Dec 2023 07:56:43 +0100 Subject: [PATCH] Day 7: not bad --- 2023/13.pl | 42 ++++++++++++++++++++++++++++++++++++++++ 2023/14.pl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100755 2023/13.pl create mode 100755 2023/14.pl diff --git a/2023/13.pl b/2023/13.pl new file mode 100755 index 0000000..b5bd859 --- /dev/null +++ b/2023/13.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl -w + +use v5.38; + +my %cards; +while (<>) { + chomp; + my ($card, $bid) = split /\s+/; + $cards{$card} = $bid; + say " $card => $bid $_ " . val($card); +} + +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; + return 7; +} + +sub val { + my $val = shift; + $val =~ y/AKQJT98765432/ABCDEFGHIJKLM/; + return rank($val).$val; +} + +my $i = 1; +my $sum; +for my $card (sort { val($b) cmp val($a) } keys %cards) { + say "$card $i ", val($card); + $sum += $cards{$card} * $i++; +} + +say $sum; + diff --git a/2023/14.pl b/2023/14.pl new file mode 100755 index 0000000..ac13982 --- /dev/null +++ b/2023/14.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl -w + +use v5.38; + +my %cards; +while (<>) { + chomp; + my ($card, $bid) = split /\s+/; + $cards{$card} = $bid; + say " $card => $bid $_ " . val($card); +} + +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; + return 7; +} + +sub rank2 { + my $card = shift; + my @cards = split //, $card; + my $max_rank = rank($card); + for my $c (@cards) { + my $c1 = $card; + $c1 =~ s/J/$c/g; + my $r = rank($c1); + $max_rank = $r if $r < $max_rank; + } + return $max_rank; +} + +sub val { + my $val = shift; + my $r = rank2($val); + $val =~ y/AKQT98765432J/ABCDEFGHIJKLM/; + return "$r$val"; +} + +my $i = 1; +my $sum; +for my $card (sort { val($b) cmp val($a) } keys %cards) { + say "$card $i ", val($card); + $sum += $cards{$card} * $i++; +} + +say $sum; + -- 2.43.5