diff options
| author | June McEnroe <june@causal.agency> | 2021-12-03 09:41:42 -0500 |
|---|---|---|
| committer | June McEnroe <june@causal.agency> | 2021-12-03 09:41:42 -0500 |
| commit | 616e6045a02ec228a3091a9b09a418d144ce95be (patch) | |
| tree | 85ddf1070564cf4d6363d6da525498d521aa180f /2021 | |
| parent | Solve day 2 part 2 (diff) | |
| download | aoc-616e6045a02ec228a3091a9b09a418d144ce95be.tar.gz aoc-616e6045a02ec228a3091a9b09a418d144ce95be.zip | |
Solve day 3 part 1
Diffstat (limited to '2021')
| -rw-r--r-- | 2021/day03.pl | 17 |
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"; |