--- /dev/null
+#!/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";
+
--- /dev/null
+#!/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";
+