From 23d5fd6468182bbc7717d86341e0b6fc88a46a22 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Fri, 3 Dec 2021 16:45:57 +0100 Subject: [PATCH] Add solution for 2021 day 03 --- 2021/README.md | 54 +- 2021/day-03/common.pm | 24 + 2021/day-03/inputs/puzzle.txt | 1000 +++++++++++++++++++++++++++++++++ 2021/day-03/inputs/sample.txt | 12 + 2021/day-03/part_one.pl | 43 ++ 2021/day-03/part_two.pl | 66 +++ 6 files changed, 1172 insertions(+), 27 deletions(-) create mode 100644 2021/day-03/common.pm create mode 100644 2021/day-03/inputs/puzzle.txt create mode 100644 2021/day-03/inputs/sample.txt create mode 100755 2021/day-03/part_one.pl create mode 100755 2021/day-03/part_two.pl diff --git a/2021/README.md b/2021/README.md index 9469830..7783688 100644 --- a/2021/README.md +++ b/2021/README.md @@ -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 | | | | diff --git a/2021/day-03/common.pm b/2021/day-03/common.pm new file mode 100644 index 0000000..1a31e80 --- /dev/null +++ b/2021/day-03/common.pm @@ -0,0 +1,24 @@ +package common; + +use strict; + +sub read_file { + my $input_path = $_[0]; + + open(FH, "<", $input_path) or die $!; + + my @lines; + + while() { + 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 diff --git a/2021/day-03/inputs/puzzle.txt b/2021/day-03/inputs/puzzle.txt new file mode 100644 index 0000000..ae7cba8 --- /dev/null +++ b/2021/day-03/inputs/puzzle.txt @@ -0,0 +1,1000 @@ +101010000100 +100001010100 +111100000101 +010000000010 +001101100010 +100110110101 +110100101101 +101010000001 +110011110000 +110000011011 +010111010100 +000101101001 +110001101100 +110100111101 +000101101100 +011111101000 +001011101100 +001011000110 +000011110110 +101110010111 +010011101001 +011111100101 +000001110101 +100001010010 +001110010101 +111010000110 +100111001111 +110010000101 +001111100001 +000110010101 +111001111011 +110000111111 +011101110001 +111001000011 +100110101011 +101101100010 +110100100101 +101011111000 +001101110011 +011000010011 +101010011111 +101011000110 +011111101001 +000011100000 +101111000101 +010000010111 +010110111000 +101110111100 +000001010011 +011011000001 +110101101011 +011110101100 +010001011100 +110111000011 +010101101001 +110011011111 +111101111101 +110011010111 +100100100110 +011100101111 +100111010000 +110000110111 +111000010111 +100001010001 +101010001111 +010101100111 +001010101111 +000001011011 +111100011110 +101110100010 +011111011110 +111110111011 +001110110111 +010011010000 +111011011101 +101110110100 +111010011111 +011011011101 +110001110100 +000010011010 +011101010101 +100000001000 +000100100001 +011110000000 +111100101010 +001100100010 +101001101110 +111010001110 +101011111101 +010011100101 +110111110101 +111000111010 +010000000111 +001101111111 +101101001100 +111001000100 +001100000000 +011011100100 +010011110000 +000001111110 +110011001010 +001001000110 +011011001101 +001111111111 +001010101010 +011110110100 +110101111111 +001111011101 +001101001111 +000000111100 +001101101010 +011000001001 +100100101101 +000000111011 +011101110011 +101011110010 +010110000010 +000101000000 +010100111011 +000111010011 +111011111000 +011110010000 +000011101001 +101001110000 +011101111101 +100010010100 +011100001011 +110011011000 +100010001110 +100101010111 +100101101110 +010010000011 +101011000111 +100100100111 +001000001000 +110010100011 +111100001011 +011010001000 +000011010000 +000111111011 +110100101110 +011110000011 +100101000011 +001100100001 +000010100011 +111101111011 +001010101101 +101110110010 +101011011010 +111010101011 +001110010001 +111110101011 +111111001011 +011100011101 +111100111001 +101011000000 +100000010111 +000101111101 +001011001110 +111101101010 +110001000000 +101111110100 +010111010110 +111011100001 +111100010100 +000100011100 +000011011011 +010000001001 +100100011001 +001010011110 +110001110011 +100010000110 +001110001101 +111001111111 +100100000011 +001001101011 +100101010011 +010111100010 +001110101100 +000110111111 +010011101101 +011101011011 +101000111100 +101100001011 +000011100011 +111110110011 +100011001101 +000100100011 +110011111010 +011001100111 +000011100110 +011001001110 +000111011110 +010101111101 +101100101011 +111110101111 +101101011010 +000101110111 +101011010010 +101100101010 +111101100100 +011100101000 +100111100010 +000110110011 +000000100001 +011110001110 +000111110010 +001100011001 +000001111001 +001111011111 +110011001111 +101010110101 +011010110100 +100010110011 +111111111101 +010001110011 +001101110000 +001110100011 +110010101011 +001010001000 +101010010000 +000101011111 +111000111101 +011101010110 +100111010011 +010010010000 +111000101101 +110000110010 +010110010001 +011101001110 +001010011111 +110011010001 +100000100001 +110110001100 +111110101101 +101110011011 +100101011011 +001010011010 +111101000110 +100010011000 +001101101001 +010001010100 +011100110010 +011010100110 +101111011110 +100011010111 +000011111110 +000110101011 +000011111000 +101110101100 +101100011110 +011010101011 +010001110111 +101010100001 +011011011001 +110011101011 +110001011111 +000010101100 +100000011101 +010000101000 +001111101100 +111100011100 +101110101101 +110111110100 +010101000001 +011110000001 +111100010101 +011110010001 +001101001100 +001011011100 +010110101111 +000100101011 +001100001010 +000101101111 +010111110110 +100010111011 +000110001011 +010111010011 +101001000101 +011000111101 +001001101001 +010100010000 +111111001000 +011110100011 +011000101100 +111101110100 +101010010110 +111101010111 +011001100100 +001110001110 +111110110000 +110100011111 +000101101110 +011000110011 +111001111110 +011110101110 +010011001101 +000011011111 +111001010111 +000000111101 +100101100100 +100001110101 +111100100101 +001110000110 +011100001101 +100100110011 +001100000100 +010011001110 +111111110011 +101110111111 +100111011000 +111110101110 +100010101100 +010101010000 +011101100001 +010010011101 +001111010111 +011100010101 +111101110101 +010100001110 +011000111000 +100110100101 +110101001111 +000011011010 +110101011010 +110010111100 +110111000101 +100111000100 +110101011111 +110110011101 +010111101010 +111001010011 +001001100100 +001101101101 +011100001100 +000110010011 +010101010110 +000011001101 +101001001100 +111011100011 +011011110100 +001110100001 +001000101001 +001100010111 +110000001101 +100010111101 +000110111101 +111001101101 +101110100000 +101001100101 +101100011001 +111010101101 +010101011100 +111100011000 +101111110101 +110111110011 +000000101100 +010110101011 +100111011011 +010111011000 +000101110100 +001010110101 +000010111011 +001110000001 +010000010000 +101110010101 +000000111110 +110100011000 +110110111100 +100010110100 +001000101110 +011001010101 +000100010001 +100011100010 +100011100000 +100010101001 +100101111000 +110000011110 +011000011101 +101101101101 +100110010110 +111110001000 +111001011010 +001110110001 +100000010101 +111101001111 +000010010100 +101100110001 +100101011000 +010101001010 +000111100000 +100010101000 +011101110100 +110110001110 +010001000110 +100101001100 +110000010111 +011000101111 +001111001000 +001110010010 +000010101101 +011000010000 +011011101010 +001010000101 +001010110010 +100001100000 +110111010000 +100010001101 +010011001011 +110011001110 +110111000110 +000001110010 +110010001001 +111100100100 +000101110110 +000100011001 +111111010101 +011010100000 +110111000100 +011101011010 +100011111110 +100001001101 +100111000001 +100001011111 +010010000010 +010111101011 +000001111100 +001100110111 +100111011100 +101000101100 +100111111111 +101101000110 +101111001111 +011010110101 +111000110000 +100010101101 +011110100100 +001101110101 +011101011000 +010101011000 +010110110011 +110101011110 +111010101100 +110111000000 +101011110110 +100111101010 +110001111110 +001000111001 +110001111011 +011010000100 +000101001010 +010111000001 +100000101000 +011010111001 +010011101110 +011011010101 +010110101100 +000010100110 +011111101011 +101111100110 +110010001100 +100000110100 +100111100011 +110111101010 +110010010000 +000011010010 +111000000111 +100101100101 +100011101110 +111110110110 +111001011000 +010100111101 +111110001100 +000110011101 +000010110010 +101000111011 +101000010100 +001001110001 +111010101111 +100011000101 +111011001100 +000000011100 +101010111001 +110111101000 +011000110001 +100100110010 +011101011101 +001011111111 +101011100110 +101011010111 +100001011010 +100100010100 +000010000010 +101101010111 +001011001101 +101100111010 +110101000001 +101101110100 +100100110111 +110001101001 +010101010001 +110110001111 +000011011101 +111111110001 +011110110101 +111100110101 +011111011011 +101001111110 +110101101010 +111001000001 +010100110001 +111011100101 +101011101101 +010010010001 +111110001001 +100100111000 +111110111100 +001000000000 +111110111101 +110010101101 +100110111110 +001010100010 +101011001001 +111101010100 +110011100000 +100111000111 +000011000110 +101010100000 +101011011000 +000010110110 +111100000011 +000000010010 +101010110111 +001111111000 +100011110101 +001001000010 +111011101110 +001111011100 +111011101010 +011011000101 +000001000010 +001000100100 +001110110011 +010101011011 +100011011010 +111000001110 +110010000000 +100101100010 +100111110011 +010011100001 +010001111010 +110101010101 +101111000001 +001001011100 +011110000110 +111001011110 +111001111010 +110100010011 +001000010010 +111110000010 +001000011001 +010100011101 +101111101011 +010111001111 +111011011010 +111101111010 +001111001111 +111110100000 +100000011000 +110100111100 +001011101000 +010011011001 +010000010011 +100010011010 +110001111111 +011010111011 +101001111100 +111110111000 +110000001100 +010000100101 +110101100111 +110011100010 +110001001101 +011101111011 +011010101101 +011000011111 +101011101001 +010100000010 +110100100010 +101010101101 +010000111101 +110100110110 +101100001100 +001011100011 +011110011001 +000000001010 +000010111001 +001000001010 +100101001010 +010000000011 +101011101100 +011101010111 +100100101001 +110100100100 +110101001101 +010100010111 +011001001100 +101100111001 +100001001011 +101100011111 +001111101101 +010011110101 +011100010011 +000010011101 +011110001101 +010001010011 +001001111001 +100111110100 +111011000111 +101010110000 +010010111011 +100010110111 +110011111110 +101111000111 +111011000000 +110001110001 +001100100011 +001011100111 +101001110010 +111001001101 +000010000011 +001011010010 +110111101110 +001000010110 +000111000111 +010001101010 +011001110101 +011000110000 +000111100100 +101010111101 +010010010010 +101111001001 +001100100111 +100111111000 +100000010011 +101101111001 +000110011110 +100001111101 +110000100001 +101110110110 +111010000010 +101010001010 +000001010000 +101011110100 +001101010110 +100011010011 +101011101110 +011101101101 +100101010110 +101011100111 +100010010000 +000101011101 +011000001000 +111011111110 +101001101111 +101000111001 +100010100111 +011111000101 +100010001111 +110011011101 +111101100111 +111101100000 +010111000100 +111010010010 +001011100100 +001000110100 +100110010111 +111010011100 +100101001101 +101001110110 +011001110010 +000111010110 +010000011101 +101100100001 +101001001000 +000111100011 +100110110000 +110011001001 +001011001001 +110101011100 +010001100101 +000000101010 +001001100001 +101001100011 +110001110101 +111001010010 +111011011000 +101101011000 +101001000010 +010110101110 +001000101101 +111001100111 +011110110000 +100100101100 +010100011111 +111000000000 +001000010111 +000000111010 +111011111001 +000110100011 +011110100110 +011011101100 +000000001101 +100101111011 +101110111001 +100111001110 +101110000101 +001110001111 +001000110010 +111101000011 +000100011011 +010111010101 +001100100000 +100101101011 +010111000011 +010101111010 +011000000111 +011011000110 +101001011100 +101101100000 +101110101001 +111000111110 +110010000010 +110111111001 +100100111011 +111011010011 +100001101000 +111110001110 +110100011010 +010101000111 +000110100111 +001111000101 +000111000011 +110001001001 +011000100010 +101001111011 +110001000101 +110111010011 +011100011011 +011011001011 +100101011111 +000000101000 +100001100110 +000100110101 +110100100000 +000101000001 +101111001100 +111101011100 +010001110110 +000001001100 +100100001110 +000100100111 +001101000001 +100011100110 +110110110010 +110001011001 +110001001000 +001011100010 +111011001011 +100101111110 +001001111011 +110101111010 +101100111100 +101111101000 +111110100101 +011110111101 +010110111101 +111010100010 +111100001100 +000001000111 +111011000100 +101000111000 +011101100100 +011000100000 +110001111101 +111010100011 +110010001010 +000101001111 +010110001001 +101101101010 +101100110010 +010010000111 +111110100010 +011011000011 +111111011011 +001011001000 +010001011101 +101101110010 +010001001101 +101000110101 +100100010011 +101010010001 +101010110011 +101110011110 +001010000110 +000011000000 +101111011000 +100001010110 +010111101101 +001110011011 +100100011110 +100001011001 +010100111111 +011101000110 +111010111011 +000010100100 +010011100111 +110001000011 +000100111010 +100111001000 +010110011000 +101101110101 +110101001011 +000101101010 +011110101111 +110111100111 +111100111011 +001000101011 +000010000101 +110101101101 +010000100111 +100110001011 +011001000110 +111101001000 +000010011000 +110101100001 +100100101111 +110111001010 +111011111011 +010111001000 +011011110011 +111011110010 +100011000111 +011001100011 +001111000000 +101001101010 +111111011101 +000100110001 +111010010000 +011111100000 +101110100101 +100011011110 +001000010001 +110110100000 +100011111011 +100011000001 +000011010100 +110101110110 +001100010010 +111011011001 +010101101111 +110111100100 +101000110100 +110001010100 +100010010111 +000111110111 +101101101100 +110100110001 +011011100000 +010011000000 +110101101000 +100011110001 +001011111101 +011000110110 +011101101010 +000011111100 +010110110111 +111101111001 +110111011101 +010001011000 +100110000101 +011101100101 +111101011000 +011110001000 +001011001100 +110110011111 +010111011100 +001111001110 +110010110110 +111011110110 +100000101100 +111100101011 +000100011110 +010111110000 +110011010101 +110000111110 +011100100011 +000101011000 +101110011100 +010111111000 +110010111101 +000001011010 +100010101011 +110001011100 +100100001100 +010101101010 +001101011110 +011011101111 +011100110011 +100011110000 +011110011111 +001011011101 +011010110010 +101000110111 +001001100111 +010111101111 +110110010001 +110001100000 +011001100101 +000110110111 +010010001100 +101100001010 +101010011110 +001010010110 +101001001001 +100110000010 +011101101111 +011010011001 +010111000010 +101001111111 +000010001001 +101000100011 +001110100100 +011011010000 +100111110101 +100110100000 +000101011001 +001100010110 +110011110010 +011000100001 +001100010101 +110010001101 +100011101001 +001100011110 +110101111101 +111111101000 +000011110001 +010000010100 +001101011001 +100111000000 +011100000101 +110110000000 +110011011001 +001100010011 +110011110110 +010010011111 +100001101001 +011100111100 +001011110001 +001010010011 +010000000101 +000100010011 +111101011001 +111100110110 +111000011100 +111101101110 +000101111011 +010101110101 +011110111111 +100000000011 +111000000011 +000010100000 +001110100010 +011001000010 +100001011110 +010100100111 +000111110011 +110100110101 +011001111100 +111101110110 +001101010101 +110110000010 +010101001101 +110100110000 +000100010101 +111011101001 +110111111111 +000000110010 +100111110110 +001000011111 +100100000101 +011001110110 +010010100110 +000101010111 +101001100000 +110011101110 +010001100001 +011100000100 +010100100000 +100000100100 diff --git a/2021/day-03/inputs/sample.txt b/2021/day-03/inputs/sample.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/2021/day-03/inputs/sample.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/2021/day-03/part_one.pl b/2021/day-03/part_one.pl new file mode 100755 index 0000000..36023a3 --- /dev/null +++ b/2021/day-03/part_one.pl @@ -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"; diff --git a/2021/day-03/part_two.pl b/2021/day-03/part_two.pl new file mode 100755 index 0000000..101a7e9 --- /dev/null +++ b/2021/day-03/part_two.pl @@ -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";