]> www.fi.muni.cz Git - aoc2021.git/commitdiff
Day 3: bitops or strings? subroutine or code copy?
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 3 Dec 2021 05:20:46 +0000 (06:20 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 3 Dec 2021 05:21:15 +0000 (06:21 +0100)
Definitely not a Perl Golf.

05.pl [new file with mode: 0755]
06.pl [new file with mode: 0755]

diff --git a/05.pl b/05.pl
new file mode 100755 (executable)
index 0000000..7fd647b
--- /dev/null
+++ b/05.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my ($c, @s);
+
+while (<>) {
+       $c++;
+       chomp;
+       my $i = 0;
+       for my $b (split//) {
+               $s[$i++] += $b;
+       }
+}
+
+my ($e, $g);
+for my $b (@s) {
+       $e .= ($b > $c/2) ? '1' : '0';
+       $g .= ($b > $c/2) ? '0' : '1';
+}
+
+say oct("0b$e")*oct("0b$g");
+
diff --git a/06.pl b/06.pl
new file mode 100755 (executable)
index 0000000..ed4aed2
--- /dev/null
+++ b/06.pl
@@ -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;
+