]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 9 quick & dirty
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 9 Dec 2020 06:03:32 +0000 (07:03 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 9 Dec 2020 06:03:32 +0000 (07:03 +0100)
17.pl [new file with mode: 0755]
18.pl [new file with mode: 0755]

diff --git a/17.pl b/17.pl
new file mode 100755 (executable)
index 0000000..c972894
--- /dev/null
+++ b/17.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my @prev;
+my $len = 25;
+
+while (my $num = <>) {
+       chomp $num;
+       if (@prev >= $len) {
+               shift @prev if @prev > $len;
+               for my $i (0 .. $len-2) {
+                       for my $j ($i+1 .. $len-1) {
+                               if ($prev[$i]+$prev[$j] == $num) {
+                                       goto FOUND;
+                               }
+                       }
+               }
+               print "$num is not a sum of ", join(', ', @prev), "\n";
+               exit 0;
+       FOUND:
+       }
+       push @prev, $num;
+}
+
diff --git a/18.pl b/18.pl
new file mode 100755 (executable)
index 0000000..3801b4e
--- /dev/null
+++ b/18.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my @prev;
+
+my $target = 375054920;
+
+my $sum = 0;
+while (my $num = <>) {
+       chomp $num;
+
+       $sum += $num;
+       push @prev, $num;
+       
+       print "Adding $num, sum=$sum\n";
+
+       while ($sum > $target) {
+               my $n1 = shift @prev;
+               $sum -= $n1;
+               print "Removing $n1, sum=$sum\n";
+       }
+
+       if ($sum == $target) {
+               print "found $sum = ", join('+', @prev), "\n";
+               my ($min, $max);
+               for my $n1 (@prev) {
+                       $min = $n1 if !defined $min || $min > $n1;
+                       $max = $n1 if !defined $max || $max < $n1;
+               }
+               print "$min+$max=", $min+$max, "\n";
+               last;
+       }
+}
+