summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-03 09:41:42 -0500
committerJune McEnroe <june@causal.agency>2021-12-03 09:41:42 -0500
commit273fff2812d97c808dd7dca0560aa5aceab3b632 (patch)
tree6e033c714ee3aa998ded4ae1009d8ccfb35209a5
parentSolve day 2 part 2 (diff)
downloadaoc-273fff2812d97c808dd7dca0560aa5aceab3b632.tar.gz
aoc-273fff2812d97c808dd7dca0560aa5aceab3b632.zip
Solve day 3 part 1
-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";