aoc2021

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

commit dbfd5f7ae4f3f8512afd0dbf741ef05469ca90bc
parent 5cf51724c9f526c08f84321c671ce5b364683cb8
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Sat,  4 Dec 2021 01:31:46 +0000

refactored code to remove the initial while statement which wasn't needed

Diffstat:
Mday03-2.pl | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 59 insertions(+), 20 deletions(-)

diff --git a/day03-2.pl b/day03-2.pl @@ -25,32 +25,71 @@ foreach (@input) { # create array copy to store oxygen generator rating candidates my @ogr = @numbers; -while ( scalar ( @ogr ) > 1 ) { # while we still have more than one ogr - foreach my $index (0 .. $len) { # iterate through each bit - # bit counters - my %count_0 = (); - my %count_1 = (); - foreach my $num (@ogr) { # iterate through each ogr candidate - if ($num->[$index]) { # bit at position index is 1 - $count_1{$index}++; - } else { - $count_0{$index}++; - } +#while ( scalar ( @ogr ) > 1 ) { # while we still have more than one ogr +foreach my $index (0 .. $len) { # iterate through each bit + # bit counters + my %count_0 = (); + my %count_1 = (); + foreach my $num (@ogr) { # iterate through each ogr candidate + if ($num->[$index]) { # bit at position index is 1 + $count_1{$index}++; + } else { + $count_0{$index}++; } - my $most_freq_bit = - ($count_1{$index} >= $count_0{$index}) ? 1 : 0; - my @temp_ogr; # temp array to hold elements whose bits match the - # most frequently occuring one + } + my $most_freq_bit = + ($count_1{$index} >= $count_0{$index}) ? 1 : 0; + my @temp_ogr; # temp array to hold elements whose bits match the + # most frequently occuring one # go through each remaining element and only keep those where the bit at # index matches the most frequently occurring one and put in temporary # array - foreach my $num (@ogr) { - if ($num->[$index] == $most_freq_bit) { - push( @temp_ogr, $num); - } + foreach my $num (@ogr) { + if ($num->[$index] == $most_freq_bit) { + push( @temp_ogr, $num); } - @ogr = @temp_ogr; # replace real array with temporary array } + @ogr = @temp_ogr; # replace real array with temporary array } +#} say @{$ogr[0]}; + +# ++++++++++++++++++++++++++++++++++++++++++++++++ + +# create array copy to store CO2 scrubber rating candidates +my @co2sr= @numbers; + +#while ( scalar ( @co2sr ) > 1 ) { # while we still have more than one co2sr +say "size of co2sr is ", scalar(@co2sr); +foreach my $index (0 .. $len) { # iterate through each bit + # bit counters + my %count_0 = (); + my %count_1 = (); + foreach my $num (@co2sr) { # iterate through each co2sr candidate + if ($num->[$index]) { # bit at position index is 1 + $count_1{$index}++; + } else { + $count_0{$index}++; + } + } + my $most_freq_bit = + ($count_1{$index} >= $count_0{$index}) ? 1 : 0; + say "most frequent bit at index $index is $most_freq_bit"; + my @temp_co2sr; # temp array to hold elements whose bits match the + # most frequently occuring one +# go through each remaining element and only keep those where the bit at +# index matches the most frequently occurring one and put in temporary +# array + foreach my $num (@co2sr) { + unless ($num->[$index] == $most_freq_bit) { + say "keeping @$num"; + push( @temp_co2sr, $num); + } + } + @co2sr = @temp_co2sr; # replace real array with temporary array + say "size of co2sr down here is now ", scalar(@co2sr); +} +#} + +say @{$co2sr[0]};