summary refs log tree commit diff homepage
path: root/2021/day03.pl
blob: 8a6202193b46a68ca1b09e4e923028877024e20e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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";