From 273fff2812d97c808dd7dca0560aa5aceab3b632 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 3 Dec 2021 09:41:42 -0500 Subject: Solve day 3 part 1 --- 2021/day03.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2021/day03.pl (limited to '2021') 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"; -- cgit 1.4.1