--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+
+my @start = map { chomp; $_ } split /,/, <>;
+
+my $turn = 0;
+my %nums;
+my $num;
+
+while ($turn < 2020) {
+ $turn++;
+ if (@start) {
+ $num = shift @start;
+ }
+ my $next;
+ if (defined $nums{$num}) {
+ $next = $turn - $nums{$num};
+ } else {
+ $next = 0;
+ }
+ $nums{$num} = $turn;
+ print "turn $turn, num=$num\n";
+ $num = $next;
+}
+
+
+
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+
+my @start = map { chomp; $_ } split /,/, <>;
+
+my $turn = 0;
+my %nums;
+my $num;
+
+while ($turn < 30000000) {
+ $turn++;
+ if (@start) {
+ $num = shift @start;
+ }
+ my $next;
+ if (defined $nums{$num}) {
+ $next = $turn - $nums{$num};
+ } else {
+ $next = 0;
+ }
+ $nums{$num} = $turn;
+ print "turn $turn, num=$num\n"
+ if $turn % 1_000_000 == 0;
+ $num = $next;
+}
+
+
+