]> www.fi.muni.cz Git - aoc2020.git/commitdiff
Tasks 1-12
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 6 Dec 2020 10:16:14 +0000 (11:16 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 6 Dec 2020 10:16:14 +0000 (11:16 +0100)
12 files changed:
1.pl [new file with mode: 0755]
10.pl [new file with mode: 0755]
11.pl [new file with mode: 0755]
12.pl [new file with mode: 0755]
2.pl [new file with mode: 0755]
3.pl [new file with mode: 0755]
4.pl [new file with mode: 0755]
5.pl [new file with mode: 0755]
6.pl [new file with mode: 0755]
7.pl [new file with mode: 0755]
8.pl [new file with mode: 0755]
9.pl [new file with mode: 0755]

diff --git a/1.pl b/1.pl
new file mode 100755 (executable)
index 0000000..2052140
--- /dev/null
+++ b/1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my @nums = sort { $a <=> $b } <>;
+chomp @nums;
+
+while (@nums > 1) {
+       print "Nums: ", join(' ', @nums), "\n";
+       my $cmp = $nums[0] + $nums[-1] <=> 2020;
+       if ($cmp < 0) {
+               shift @nums;
+       } elsif ($cmp == 0) {
+               print "$nums[0]*$nums[-1]=", $nums[0]*$nums[-1], "\n";
+               last;
+       } else {
+               pop @nums;
+       }
+}
+
diff --git a/10.pl b/10.pl
new file mode 100755 (executable)
index 0000000..2f7ade1
--- /dev/null
+++ b/10.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my @seats;
+while (<>) {
+       chomp;
+       y/FBRL/0110/;
+       my $seat =  eval "0b$_";
+       $seats[$seat] = 1;
+}
+
+for (1 .. $#seats-1) {
+       print $_, "\n" if $seats[$_-1] && $seats[$_+1]
+               && !$seats[$_];
+}
diff --git a/11.pl b/11.pl
new file mode 100755 (executable)
index 0000000..256c0ec
--- /dev/null
+++ b/11.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+local $/ = "\n\n";
+
+my $sum = 0;
+
+while (<>) {
+       s/\n//g;
+       my %q = map { $_ => 1 } split //;
+       $sum += keys %q;
+}
+
+print "$sum\n";
+
diff --git a/12.pl b/12.pl
new file mode 100755 (executable)
index 0000000..c24e26c
--- /dev/null
+++ b/12.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Data::Dumper;
+
+local $/ = "\n\n";
+
+my $sum = 0;
+
+while (<>) {
+       my $nper = 0;
+       my %q;
+       for my $per (split /\n/) {
+               $nper++;
+               $q{$_}++ for grep { /\S/ } split(//, $per);
+       }
+       my $count = grep { $q{$_} == $nper } keys %q;
+       $sum += $count;
+}
+
+print "$sum\n";
+
diff --git a/2.pl b/2.pl
new file mode 100755 (executable)
index 0000000..d950fae
--- /dev/null
+++ b/2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my @nums = sort { $a <=> $b } <>;
+chomp @nums;
+
+my %is_listed = map { $_ => 1 } @nums;
+
+for my $i (0 .. $#nums-1) {
+       for my $j ($i .. $#nums) {
+               my $rest = 2020-$nums[$i]-$nums[$j];
+               if ($rest > 0 && $is_listed{$rest} && $rest != $nums[$i]
+                       && $rest != $nums[$j]) {
+                       print "$nums[$i]*$nums[$j]*$rest=", $nums[$i]*$nums[$j]*$rest, "\n";
+                       last;
+               }
+       }
+}
+
diff --git a/3.pl b/3.pl
new file mode 100755 (executable)
index 0000000..2865016
--- /dev/null
+++ b/3.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $count = 0;
+
+while (<>) {
+       my ($min, $max, $letter, $pass) = /\A(\d+)-(\d+)\s+(\S):\s+(\S+)\s*\z/;
+       if (!defined $pass) {
+               print "Divny radek $_\n";
+               next;
+       }
+
+       
+       my $chars =()= ($pass =~ /$letter/g);
+
+       # print "$min-$max $letter: [$pass] - $chars\n";
+       $count++ if $chars >= $min && $chars <= $max;
+}
+
+print $count, "\n";
+
diff --git a/4.pl b/4.pl
new file mode 100755 (executable)
index 0000000..d16915a
--- /dev/null
+++ b/4.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $count = 0;
+
+while (<>) {
+       my ($min, $max, $letter, $pass) = /\A(\d+)-(\d+)\s+(\S):\s+(\S+)\s*\z/;
+       if (!defined $pass) {
+               print "Divny radek $_\n";
+               next;
+       }
+
+       no warnings 'substr';
+       # no warnings 'uninitialized';
+       $count++ if !!(substr($pass, $min-1, 1) eq $letter)
+               + !!(substr($pass, $max-1, 1) eq $letter)
+               == 1;
+}
+
+print $count, "\n";
+
diff --git a/5.pl b/5.pl
new file mode 100755 (executable)
index 0000000..2a7b22d
--- /dev/null
+++ b/5.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $field = do { local $/; <> };
+my $rows = $field =~ s/\s//g;
+my $flen = length $field;
+my $cols = $flen/$rows;
+
+print "Field has $flen bytes, in $rows rows and $cols cols\n";
+
+my $step = $cols + 2;
+
+my $col = 0;
+my $sum = 0;
+for my $row (0 .. $rows-1) {
+       $sum++ if substr($field, $row*$cols + $col, 1) eq '#';
+       $col += 3;
+       $col %= $cols;
+}
+print "sum=$sum\n";
+
+       
+
diff --git a/6.pl b/6.pl
new file mode 100755 (executable)
index 0000000..6749d06
--- /dev/null
+++ b/6.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $field = do { local $/; <> };
+my $rows = $field =~ s/\s//g;
+my $flen = length $field;
+my $cols = $flen/$rows;
+
+print "Field has $flen bytes, in $rows rows and $cols cols\n";
+
+sub slope {
+       my ($colstep, $rowstep) = @_;
+
+       $colstep += $cols;
+
+       my $col = 0;
+       my $sum = 0;
+       my $row = 0;
+       while ($row < $rows) {
+               print "row $row\n";
+               $sum++ if substr($field, $row*$cols + $col, 1) eq '#';
+               $col += $colstep;
+               $col %= $cols;
+               $row += $rowstep;
+       }
+       print "$colstep x $rowstep, sum=$sum\n";
+       return $sum;
+}
+
+print "Total: ",
+       slope(1, 1)
+       * slope(3, 1)
+       * slope(5, 1)
+       * slope(7, 1)
+       * slope(1, 2),
+       "\n";
+
diff --git a/7.pl b/7.pl
new file mode 100755 (executable)
index 0000000..d6147c3
--- /dev/null
+++ b/7.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+$/ = "\n\n";
+
+my $valid = 0;
+while (<>) {
+       $valid++ if (
+               grep { /^(?:byr|iyr|eyr|hgt|hcl|ecl|pid):/ }
+               split /\s+/
+       ) == 7;
+}
+
+print "Valid: $valid\n";
+
diff --git a/8.pl b/8.pl
new file mode 100755 (executable)
index 0000000..c8e2a20
--- /dev/null
+++ b/8.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+$/ = "\n\n";
+
+my $valid = 0;
+while (<>) {
+       my $correct = 0;
+       for my $field (split /\s+/) {
+               print "$field";
+               $correct++
+                       if $field =~ /\Abyr:(\d{4})\z/
+                       && $1 >= 1920 && $1 <= 2002;
+               $correct++
+                       if $field =~ /\Aiyr:(\d{4})\z/
+                       && $1 >= 2010 && $1 <= 2020;
+               $correct++
+                       if $field =~ /\Aeyr:(\d{4})\z/
+                       && $1 >= 2020 && $1 <= 2030;
+               $correct++
+                       if $field =~ /\Ahgt:(\d+)(cm|in)\z/
+                       && ($2 eq 'cm' && $1 >= 150 && $1 <= 193
+                       || $2 eq 'in' && $1 >= 59 && $1 <= 76);
+               $correct++
+                       if $field =~ /\Ahcl:#[a-f0-9]{6}\z/;
+               $correct++
+                       if $field =~ /\Aecl:(?:amb|blu|brn|gry|grn|hzl|oth)\z/;
+               $correct++
+                       if $field =~ /\Apid:(?:\d{9})\z/;
+               print "\t$correct\n";
+       }
+       $valid++ if $correct >= 7;
+       print "\n";
+}
+
+print "Valid: $valid\n";
+
diff --git a/9.pl b/9.pl
new file mode 100755 (executable)
index 0000000..dcdc4ff
--- /dev/null
+++ b/9.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $max = 0;
+while (<>) {
+       chomp;
+       y/FBRL/0110/;
+       my $seat =  eval "0b$_";
+       $max = $seat if $max < $seat;
+}
+
+print $max, "\n";