]> www.fi.muni.cz Git - aoc.git/blobdiff - 2021/06.pl
Moved 2021 to a subdir
[aoc.git] / 2021 / 06.pl
diff --git a/2021/06.pl b/2021/06.pl
new file mode 100755 (executable)
index 0000000..ed4aed2
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my ($c, @s);
+
+chomp (my @nums = <>);
+$c = @nums;
+
+my @n = @nums;
+my $pos = 0;
+while (@n > 1) {
+       my $ones = grep { substr ($_, $pos, 1) eq '1' } @n;
+       if ($ones >= @n/2) {
+               @n = grep { substr ($_, $pos, 1) eq '1' } @n;
+       } else { 
+               @n = grep { substr ($_, $pos, 1) eq '0' } @n;
+       }
+       $pos++;
+}
+my $ox = oct "0b".$n[0];
+
+@n = @nums;
+$pos = 0;
+while (@n > 1) {
+       my $ones = grep { substr ($_, $pos, 1) eq '0' } @n;
+       if ($ones > @n/2) {
+               @n = grep { substr ($_, $pos, 1) eq '1' } @n;
+       } else { 
+               @n = grep { substr ($_, $pos, 1) eq '0' } @n;
+       }
+       $pos++;
+}
+my $co = oct "0b".$n[0];
+
+say $ox * $co;
+