commit 5cf51724c9f526c08f84321c671ce5b364683cb8
parent 2e35b4be3a9d6c289e36b6a60cd0421961c6d18b
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 3 Dec 2021 21:25:49 +0000
update day03-2 to remove additional unnecessary print statements and logic checking code
Diffstat:
1 file changed, 4 insertions(+), 11 deletions(-)
diff --git a/day03-2.pl b/day03-2.pl
@@ -26,10 +26,8 @@ foreach (@input) {
my @ogr = @numbers;
while ( scalar ( @ogr ) > 1 ) { # while we still have more than one ogr
- say "\@ogr array has ", scalar(@ogr), " elements";
foreach my $index (0 .. $len) { # iterate through each bit
# bit counters
- say "evaluating index $index";
my %count_0 = ();
my %count_1 = ();
foreach my $num (@ogr) { # iterate through each ogr candidate
@@ -41,22 +39,17 @@ while ( scalar ( @ogr ) > 1 ) { # while we still have more than one ogr
}
my $most_freq_bit =
($count_1{$index} >= $count_0{$index}) ? 1 : 0;
- my $mfb_count;
- if ($most_freq_bit) {
- $mfb_count = $count_1{$index};
- } else {
- $mfb_count = $count_0{$index};
- }
- say "there are more $most_freq_bit bits ($mfb_count)";
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) {
- say "keeping @$num";
push( @temp_ogr, $num);
}
}
- @ogr = @temp_ogr;
+ @ogr = @temp_ogr; # replace real array with temporary array
}
}