From e5aab31ddde8e15c83f2d148b2a81818e1420ba4 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 3 Dec 2021 06:20:46 +0100 Subject: [PATCH] Day 3: bitops or strings? subroutine or code copy? Definitely not a Perl Golf. --- 05.pl | 23 +++++++++++++++++++++++ 06.pl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 05.pl create mode 100755 06.pl diff --git a/05.pl b/05.pl new file mode 100755 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 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; + -- 2.43.5