summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2021/day03.pl17
1 files changed, 17 insertions, 0 deletions
diff --git a/2021/day03.pl b/2021/day03.pl
new file mode 100644
index 0000000..8a62021
--- /dev/null
+++ b/2021/day03.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+my @nums;
+while (<>) {
+	chomp;
+	push @nums, oct("0b".$_);
+}
+my $gamma = 0;
+for (my $i = 0; $i < 12; $i++) {
+	my $ones = 0;
+	foreach (@nums) {
+		$ones++ if $_ & (1 << $i);
+	}
+	$gamma |= 1 << $i if $ones > @nums / 2;
+}
+my $epsilon = ~$gamma & 0xFFF;
+print $gamma * $epsilon, "\n";