]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 13
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 13 Dec 2020 14:50:03 +0000 (15:50 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 13 Dec 2020 14:50:03 +0000 (15:50 +0100)
25.pl [new file with mode: 0755]
26.pl [new file with mode: 0755]

diff --git a/25.pl b/25.pl
new file mode 100755 (executable)
index 0000000..60b72ca
--- /dev/null
+++ b/25.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $timestamp = <>;
+my @buses = grep { /\d+/ } split /,/, <>;
+
+my ($min_del, $min_bus);
+for my $bus (@buses) {
+       my $delay = $bus*(1+int(($timestamp-1)/$bus))-$timestamp;
+       print "Bus $bus delay $delay\n";
+       if (!defined $min_del || $min_del > $delay) {
+               $min_bus = $bus;
+               $min_del = $delay;
+       }
+}
+
+print "$min_bus * $min_del = ", $min_bus * $min_del, "\n";
+
diff --git a/26.pl b/26.pl
new file mode 100755 (executable)
index 0000000..90d07ae
--- /dev/null
+++ b/26.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $timestamp = <>;
+my @buses = split /,/, <>;
+
+my $mins = 1;
+my %bus_time;
+my $first = shift @buses;
+for my $bus (@buses) {
+       if ($bus =~ /\d/) {
+               $bus_time{$bus} = $mins % $bus;
+               print "Bus $bus at t+$mins ($bus_time{$bus})\n";
+       }
+       $mins++;
+}
+
+@buses = grep /\d/, @buses;
+
+my $t = $first;
+my $add = $first;
+
+for my $bus (keys %bus_time) {
+       print "bus $bus at $bus_time{$bus}\n";
+       while (1) {
+               print "t=$t, add=$add\n";
+               last if ($t + $bus_time{$bus}) % $bus == 0;
+               $t += $add;
+       }
+       $add *= $bus;
+}
+print "t=$t\n";
+