Add solution for 2021 day 03
This commit is contained in:
parent
dff5d17dc8
commit
23d5fd6468
@ -1,29 +1,29 @@
|
||||
# [Advent of Code 2021](https://adventofcode.com/2021)
|
||||
|
||||
| Solved | Day | Language |
|
||||
| :----: | :-: | :------- |
|
||||
| ✓ | 01 | Ruby |
|
||||
| ✓ | 02 | Ada |
|
||||
| | 03 | |
|
||||
| | 04 | |
|
||||
| | 05 | |
|
||||
| | 06 | |
|
||||
| | 07 | |
|
||||
| | 08 | |
|
||||
| | 09 | |
|
||||
| | 10 | |
|
||||
| | 11 | |
|
||||
| | 12 | |
|
||||
| | 13 | |
|
||||
| | 14 | |
|
||||
| | 15 | |
|
||||
| | 16 | |
|
||||
| | 17 | |
|
||||
| | 18 | |
|
||||
| | 19 | |
|
||||
| | 20 | |
|
||||
| | 21 | |
|
||||
| | 22 | |
|
||||
| | 23 | |
|
||||
| | 24 | |
|
||||
| | 25 | |
|
||||
| Day | Part 1 | Part 2 | Language |
|
||||
| :-: | :----: | :----: | :------- |
|
||||
| 01 | ✓ | ✓ | Ruby |
|
||||
| 02 | ✓ | ✓ | Ada |
|
||||
| 03 | ✓ | ✓ | Perl |
|
||||
| 04 | | | |
|
||||
| 05 | | | |
|
||||
| 06 | | | |
|
||||
| 07 | | | |
|
||||
| 08 | | | |
|
||||
| 09 | | | |
|
||||
| 10 | | | |
|
||||
| 11 | | | |
|
||||
| 12 | | | |
|
||||
| 13 | | | |
|
||||
| 14 | | | |
|
||||
| 15 | | | |
|
||||
| 16 | | | |
|
||||
| 17 | | | |
|
||||
| 18 | | | |
|
||||
| 19 | | | |
|
||||
| 20 | | | |
|
||||
| 21 | | | |
|
||||
| 22 | | | |
|
||||
| 23 | | | |
|
||||
| 24 | | | |
|
||||
| 25 | | | |
|
||||
|
24
2021/day-03/common.pm
Normal file
24
2021/day-03/common.pm
Normal file
@ -0,0 +1,24 @@
|
||||
package common;
|
||||
|
||||
use strict;
|
||||
|
||||
sub read_file {
|
||||
my $input_path = $_[0];
|
||||
|
||||
open(FH, "<", $input_path) or die $!;
|
||||
|
||||
my @lines;
|
||||
|
||||
while(<FH>) {
|
||||
my $line = $_;
|
||||
$line =~ s/\s+$//; # rtrim
|
||||
my @chars = split('', $line);
|
||||
push(@lines, \@chars);
|
||||
}
|
||||
|
||||
close(FH);
|
||||
|
||||
return @lines;
|
||||
}
|
||||
|
||||
1; # module needs to return a "true value", because reasons
|
1000
2021/day-03/inputs/puzzle.txt
Normal file
1000
2021/day-03/inputs/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
12
2021/day-03/inputs/sample.txt
Normal file
12
2021/day-03/inputs/sample.txt
Normal file
@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
43
2021/day-03/part_one.pl
Executable file
43
2021/day-03/part_one.pl
Executable file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
# use warnings;
|
||||
use strict;
|
||||
use lib "./";
|
||||
use common;
|
||||
use Data::Dumper;
|
||||
|
||||
sub part_one {
|
||||
my $numbers = $_[0];
|
||||
my $count = @$numbers;
|
||||
my $digits = @{$numbers->[0]};
|
||||
my @ones = ((0) x $digits);
|
||||
|
||||
for (@$numbers) {
|
||||
my @row = @$_;
|
||||
for my $idx (0 .. $#row) {
|
||||
@ones[$idx] += 1 if @row[$idx] == "1";
|
||||
}
|
||||
}
|
||||
|
||||
my @gamma_rate = ();
|
||||
my @epsilon_rate = ();
|
||||
|
||||
for (@ones) {
|
||||
my $zeroes = $count - $_;
|
||||
if ($zeroes < $_) {
|
||||
push(@gamma_rate, 1);
|
||||
push(@epsilon_rate, 0);
|
||||
} else {
|
||||
push(@gamma_rate, 0);
|
||||
push(@epsilon_rate, 1);
|
||||
}
|
||||
}
|
||||
my $gamma_rate = oct("0b" . join("", @gamma_rate));
|
||||
my $epsilon_rate = oct("0b" . join("", @epsilon_rate));
|
||||
|
||||
return $gamma_rate * $epsilon_rate;
|
||||
}
|
||||
|
||||
my @numbers = common::read_file($ARGV[0]);
|
||||
my $result = part_one(\@numbers);
|
||||
print $result, "\n";
|
66
2021/day-03/part_two.pl
Executable file
66
2021/day-03/part_two.pl
Executable file
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
# use warnings;
|
||||
use strict;
|
||||
use lib "./";
|
||||
use common;
|
||||
use Data::Dumper;
|
||||
|
||||
sub tally_digit_column {
|
||||
my $numbers = $_[0];
|
||||
my $digit = $_[1];
|
||||
|
||||
my @ones;
|
||||
my @zeroes;
|
||||
|
||||
for my $num (@$numbers) {
|
||||
if ($num->[$digit] eq "1") {
|
||||
push(@ones, $num);
|
||||
} else {
|
||||
push(@zeroes, $num);
|
||||
}
|
||||
}
|
||||
|
||||
return (\@ones, \@zeroes);
|
||||
}
|
||||
|
||||
sub part_two {
|
||||
my $numbers = $_[0];
|
||||
my $digits = @{$numbers->[0]};
|
||||
|
||||
my $highest = $numbers;
|
||||
my $lowest = $numbers;
|
||||
|
||||
for my $idx (0..$digits) {
|
||||
if (scalar(@$highest) != 1) {
|
||||
my @results = tally_digit_column($highest, $idx);
|
||||
my $ones = @results[0];
|
||||
my $zeroes = @results[1];
|
||||
if (scalar(@$ones) >= scalar(@$zeroes)) {
|
||||
$highest = $ones;
|
||||
} else {
|
||||
$highest = $zeroes;
|
||||
}
|
||||
}
|
||||
|
||||
if (scalar(@$lowest) != 1) {
|
||||
my @results = tally_digit_column($lowest, $idx);
|
||||
my $ones = @results[0];
|
||||
my $zeroes = @results[1];
|
||||
if (scalar(@$zeroes) <= scalar(@$ones)) {
|
||||
$lowest = $zeroes;
|
||||
} else {
|
||||
$lowest = $ones;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
my $oxygen_rating = oct("0b" . join("", @{$highest->[0]}));
|
||||
my $scrubber_rating = oct("0b" . join("", @{$lowest->[0]}));
|
||||
|
||||
return $oxygen_rating * $scrubber_rating;
|
||||
}
|
||||
|
||||
my @numbers = common::read_file($ARGV[0]);
|
||||
my $result = part_two(\@numbers);
|
||||
print $result, "\n";
|
Loading…
Reference in New Issue
Block a user